Brian O'Connor | 7cbbbb7 | 2016-04-09 02:13:23 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Brian O'Connor | 7cbbbb7 | 2016-04-09 02:13:23 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 16 | package org.onosproject.driver.pipeline.ofdpa; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 17 | |
| 18 | import com.google.common.cache.Cache; |
| 19 | import com.google.common.cache.CacheBuilder; |
| 20 | import com.google.common.cache.RemovalCause; |
| 21 | import com.google.common.cache.RemovalNotification; |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableList; |
| 23 | import com.google.common.collect.Lists; |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 24 | import com.google.common.collect.Sets; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 25 | import org.onlab.osgi.ServiceDirectory; |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 26 | import org.onlab.packet.IpPrefix; |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 27 | import org.onlab.packet.MacAddress; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 28 | import org.onlab.packet.MplsLabel; |
| 29 | import org.onlab.packet.VlanId; |
| 30 | import org.onosproject.core.ApplicationId; |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 31 | import org.onosproject.core.GroupId; |
Charles Chan | 3256252 | 2016-04-07 14:37:14 -0700 | [diff] [blame] | 32 | import org.onosproject.driver.extensions.OfdpaSetVlanVid; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 33 | import org.onosproject.net.DeviceId; |
| 34 | import org.onosproject.net.PortNumber; |
| 35 | import org.onosproject.net.behaviour.NextGroup; |
| 36 | import org.onosproject.net.behaviour.PipelinerContext; |
| 37 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 38 | import org.onosproject.net.flow.TrafficSelector; |
| 39 | import org.onosproject.net.flow.TrafficTreatment; |
| 40 | import org.onosproject.net.flow.criteria.Criterion; |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 41 | import org.onosproject.net.flow.criteria.TunnelIdCriterion; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 42 | import org.onosproject.net.flow.criteria.VlanIdCriterion; |
| 43 | import org.onosproject.net.flow.instructions.Instruction; |
| 44 | import org.onosproject.net.flow.instructions.Instructions; |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 45 | import org.onosproject.net.flow.instructions.Instructions.GroupInstruction; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 46 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
Yi Tseng | 47f82dc | 2017-03-05 22:48:39 -0800 | [diff] [blame] | 47 | import org.onosproject.net.flowobjective.DefaultNextObjective; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 48 | import org.onosproject.net.flowobjective.FlowObjectiveStore; |
| 49 | import org.onosproject.net.flowobjective.NextObjective; |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 50 | import org.onosproject.net.flowobjective.Objective.Operation; |
Yi Tseng | 47f82dc | 2017-03-05 22:48:39 -0800 | [diff] [blame] | 51 | import org.onosproject.net.flowobjective.ObjectiveContext; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 52 | import org.onosproject.net.flowobjective.ObjectiveError; |
| 53 | import org.onosproject.net.group.DefaultGroupBucket; |
| 54 | import org.onosproject.net.group.DefaultGroupDescription; |
| 55 | import org.onosproject.net.group.DefaultGroupKey; |
| 56 | import org.onosproject.net.group.Group; |
| 57 | import org.onosproject.net.group.GroupBucket; |
| 58 | import org.onosproject.net.group.GroupBuckets; |
| 59 | import org.onosproject.net.group.GroupDescription; |
| 60 | import org.onosproject.net.group.GroupEvent; |
| 61 | import org.onosproject.net.group.GroupKey; |
| 62 | import org.onosproject.net.group.GroupListener; |
| 63 | import org.onosproject.net.group.GroupService; |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 64 | import org.onosproject.store.service.AtomicCounter; |
| 65 | import org.onosproject.store.service.StorageService; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 66 | import org.slf4j.Logger; |
| 67 | |
| 68 | import java.util.ArrayDeque; |
| 69 | import java.util.ArrayList; |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 70 | import java.util.Arrays; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 71 | import java.util.Collection; |
| 72 | import java.util.Collections; |
| 73 | import java.util.Deque; |
| 74 | import java.util.List; |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 75 | import java.util.Objects; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 76 | import java.util.Set; |
| 77 | import java.util.concurrent.ConcurrentHashMap; |
| 78 | import java.util.concurrent.CopyOnWriteArrayList; |
| 79 | import java.util.concurrent.Executors; |
| 80 | import java.util.concurrent.ScheduledExecutorService; |
| 81 | import java.util.concurrent.TimeUnit; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 82 | import java.util.stream.Collectors; |
| 83 | |
| 84 | import static org.onlab.util.Tools.groupedThreads; |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 85 | import static org.onosproject.driver.pipeline.ofdpa.Ofdpa2Pipeline.*; |
| 86 | import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*; |
jayakumarthazhath | 655b9a8 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 87 | import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.L2_MULTICAST_TYPE; |
| 88 | import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.l2MulticastGroupKey; |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 89 | import static org.onosproject.net.flow.criteria.Criterion.Type.TUNNEL_ID; |
Pier Ventre | 140a894 | 2016-11-02 07:26:38 -0700 | [diff] [blame] | 90 | import static org.onosproject.net.flow.criteria.Criterion.Type.VLAN_VID; |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 91 | import static org.onosproject.net.group.GroupDescription.Type.ALL; |
Andreas Pantelopoulos | 7a8488c | 2018-01-22 16:00:28 -0800 | [diff] [blame] | 92 | import static org.onosproject.net.group.GroupDescription.Type.INDIRECT; |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 93 | import static org.onosproject.net.group.GroupDescription.Type.SELECT; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 94 | import static org.slf4j.LoggerFactory.getLogger; |
| 95 | |
| 96 | /** |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 97 | * Group handler that emulates Broadcom OF-DPA TTP. |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 98 | */ |
Charles Chan | 361154b | 2016-03-24 10:23:39 -0700 | [diff] [blame] | 99 | public class Ofdpa2GroupHandler { |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 100 | protected final Logger log = getLogger(getClass()); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 101 | // Services, Stores |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 102 | protected GroupService groupService; |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 103 | protected StorageService storageService; |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 104 | protected FlowObjectiveStore flowObjectiveStore; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 105 | // index number for group creation |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 106 | private AtomicCounter nextIndex; |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 107 | protected DeviceId deviceId; |
Saurav Das | 2f2c9d0 | 2018-04-07 16:51:09 -0700 | [diff] [blame] | 108 | Cache<GroupKey, List<OfdpaGroupHandlerUtility.OfdpaNextGroup>> pendingAddNextObjectives; |
| 109 | Cache<NextObjective, List<GroupKey>> pendingRemoveNextObjectives; |
| 110 | Cache<GroupKey, Set<OfdpaGroupHandlerUtility.GroupChainElem>> pendingGroups; |
| 111 | ConcurrentHashMap<GroupKey, Set<NextObjective>> pendingUpdateNextObjectives; |
Yi Tseng | 47f82dc | 2017-03-05 22:48:39 -0800 | [diff] [blame] | 112 | // local store for pending bucketAdds - by design there can be multiple |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 113 | // pending bucket for a group |
Yi Tseng | 47f82dc | 2017-03-05 22:48:39 -0800 | [diff] [blame] | 114 | protected ConcurrentHashMap<Integer, Set<NextObjective>> pendingBuckets = |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 115 | new ConcurrentHashMap<>(); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 116 | private ScheduledExecutorService groupCheckerExecutor = |
| 117 | Executors.newScheduledThreadPool(2, groupedThreads("onos/pipeliner", "ofdpa-%d", log)); |
Charles Chan | 40132b3 | 2017-01-22 00:19:37 -0800 | [diff] [blame] | 118 | /** |
| 119 | * Determines whether this pipeline support copy ttl instructions or not. |
| 120 | * |
| 121 | * @return true if copy ttl instructions are supported |
| 122 | */ |
| 123 | protected boolean supportCopyTtl() { |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Determines whether this pipeline support set mpls bos instruction or not. |
| 129 | * |
| 130 | * @return true if set mpls bos instruction is supported |
| 131 | */ |
| 132 | protected boolean supportSetMplsBos() { |
| 133 | return true; |
| 134 | } |
| 135 | |
Charles Chan | 0f43e47 | 2017-02-14 14:00:16 -0800 | [diff] [blame] | 136 | /** |
| 137 | * Determines whether this pipeline requires popping VLAN before pushing MPLS. |
| 138 | * <p> |
| 139 | * If required, pop vlan before push mpls and add an arbitrary vlan back afterward. |
| 140 | * MPLS interface group will substitute the arbitrary VLAN with expected VLAN later on. |
| 141 | * |
| 142 | * @return true if this pipeline requires popping VLAN before pushing MPLS |
| 143 | */ |
| 144 | protected boolean requireVlanPopBeforeMplsPush() { |
| 145 | return false; |
| 146 | } |
| 147 | |
Alex Yashchuk | 4caa8e8 | 2017-12-08 17:40:05 +0200 | [diff] [blame] | 148 | public void init(DeviceId deviceId, PipelinerContext context) { |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 149 | ServiceDirectory serviceDirectory = context.directory(); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 150 | this.deviceId = deviceId; |
| 151 | this.flowObjectiveStore = context.store(); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 152 | this.groupService = serviceDirectory.get(GroupService.class); |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 153 | this.storageService = serviceDirectory.get(StorageService.class); |
Madan Jampani | d5714e0 | 2016-04-19 14:15:20 -0700 | [diff] [blame] | 154 | this.nextIndex = storageService.getAtomicCounter("group-id-index-counter"); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 155 | |
Charles Chan | fc5c780 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 156 | pendingAddNextObjectives = CacheBuilder.newBuilder() |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 157 | .expireAfterWrite(20, TimeUnit.SECONDS) |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 158 | .removalListener((RemovalNotification<GroupKey, List<OfdpaNextGroup>> notification) -> { |
| 159 | if (notification.getCause() == RemovalCause.EXPIRED && |
| 160 | Objects.nonNull(notification.getValue())) { |
| 161 | notification.getValue() |
| 162 | .forEach(ofdpaNextGrp -> |
| 163 | fail(ofdpaNextGrp.nextObjective(), |
| 164 | ObjectiveError.GROUPINSTALLATIONFAILED)); |
Charles Chan | fc5c780 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 165 | } |
| 166 | }).build(); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 167 | |
Charles Chan | fc5c780 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 168 | pendingRemoveNextObjectives = CacheBuilder.newBuilder() |
| 169 | .expireAfterWrite(20, TimeUnit.SECONDS) |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 170 | .removalListener((RemovalNotification<NextObjective, List<GroupKey>> notification) -> { |
Charles Chan | fc5c780 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 171 | if (notification.getCause() == RemovalCause.EXPIRED) { |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 172 | fail(notification.getKey(), |
| 173 | ObjectiveError.GROUPREMOVALFAILED); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 174 | } |
| 175 | }).build(); |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 176 | pendingGroups = CacheBuilder.newBuilder() |
| 177 | .expireAfterWrite(20, TimeUnit.SECONDS) |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 178 | .removalListener((RemovalNotification<GroupKey, Set<GroupChainElem>> notification) -> { |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 179 | if (notification.getCause() == RemovalCause.EXPIRED) { |
| 180 | log.error("Unable to install group with key {} and pending GCEs: {}", |
| 181 | notification.getKey(), notification.getValue()); |
| 182 | } |
| 183 | }).build(); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 184 | pendingUpdateNextObjectives = new ConcurrentHashMap<>(); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 185 | GroupChecker groupChecker = new GroupChecker(this); |
| 186 | groupCheckerExecutor.scheduleAtFixedRate(groupChecker, 0, 500, TimeUnit.MILLISECONDS); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 187 | groupService.addListener(new InnerGroupListener()); |
| 188 | } |
| 189 | |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 190 | ////////////////////////////////////// |
| 191 | // Group Creation |
| 192 | ////////////////////////////////////// |
| 193 | |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 194 | /** |
| 195 | * Adds a list of group chain by given NextObjective. |
| 196 | * |
| 197 | * @param nextObjective the NextObjective |
| 198 | */ |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 199 | protected void addGroup(NextObjective nextObjective) { |
| 200 | switch (nextObjective.type()) { |
| 201 | case SIMPLE: |
| 202 | Collection<TrafficTreatment> treatments = nextObjective.next(); |
| 203 | if (treatments.size() != 1) { |
| 204 | log.error("Next Objectives of type Simple should only have a " |
| 205 | + "single Traffic Treatment. Next Objective Id:{}", |
| 206 | nextObjective.id()); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 207 | fail(nextObjective, ObjectiveError.BADPARAMS); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 208 | return; |
| 209 | } |
| 210 | processSimpleNextObjective(nextObjective); |
| 211 | break; |
| 212 | case BROADCAST: |
| 213 | processBroadcastNextObjective(nextObjective); |
| 214 | break; |
| 215 | case HASHED: |
Pier Ventre | 140a894 | 2016-11-02 07:26:38 -0700 | [diff] [blame] | 216 | if (!verifyHashedNextObjective(nextObjective)) { |
| 217 | log.error("Next Objectives of type hashed not supported. Next Objective Id:{}", |
| 218 | nextObjective.id()); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 219 | fail(nextObjective, ObjectiveError.BADPARAMS); |
Pier Ventre | 140a894 | 2016-11-02 07:26:38 -0700 | [diff] [blame] | 220 | return; |
| 221 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 222 | processHashedNextObjective(nextObjective); |
| 223 | break; |
| 224 | case FAILOVER: |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 225 | fail(nextObjective, ObjectiveError.UNSUPPORTED); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 226 | log.warn("Unsupported next objective type {}", nextObjective.type()); |
| 227 | break; |
| 228 | default: |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 229 | fail(nextObjective, ObjectiveError.UNKNOWN); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 230 | log.warn("Unknown next objective type {}", nextObjective.type()); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | /** |
jayakumarthazhath | 655b9a8 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 235 | * Similar to processBroadcastNextObjective but handles L2 Multicast Next Objectives. |
| 236 | * |
| 237 | * @param nextObj NextObjective of L2_MULTICAST with chained NextObjectives for single homed access ports |
| 238 | */ |
| 239 | private void processL2MulticastNextObjective(NextObjective nextObj) { |
| 240 | |
| 241 | VlanId assignedVlan = readVlanFromSelector(nextObj.meta()); |
| 242 | if (assignedVlan == null) { |
| 243 | log.warn("VLAN ID required by L2 multicast next objective is missing. Aborting group creation."); |
| 244 | fail(nextObj, ObjectiveError.BADPARAMS); |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | // Group info should contain only single homed hosts for a given vlanId |
| 249 | List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan); |
| 250 | createL2MulticastGroup(nextObj, assignedVlan, groupInfos); |
| 251 | } |
| 252 | |
| 253 | private void createL2MulticastGroup(NextObjective nextObj, VlanId vlanId, List<GroupInfo> groupInfos) { |
| 254 | // Realize & represent L2 multicast group in OFDPA driver layer |
| 255 | // TODO : Need to identify significance of OfdpaNextGroup. |
| 256 | Integer l2MulticastGroupId = L2_MULTICAST_TYPE | (vlanId.toShort() << 16); |
| 257 | final GroupKey l2MulticastGroupKey = l2MulticastGroupKey(vlanId, deviceId); |
| 258 | List<Deque<GroupKey>> l2MulticastAllGroup = Lists.newArrayList(); |
| 259 | groupInfos.forEach(groupInfo -> { |
| 260 | Deque<GroupKey> groupKeyChain = new ArrayDeque<>(); |
| 261 | groupKeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie()); |
| 262 | groupKeyChain.addFirst(l2MulticastGroupKey); |
| 263 | l2MulticastAllGroup.add(groupKeyChain); |
| 264 | }); |
| 265 | OfdpaNextGroup ofdpaL2MulticastGroup = new OfdpaNextGroup(l2MulticastAllGroup, nextObj); |
| 266 | updatePendingNextObjective(l2MulticastGroupKey, ofdpaL2MulticastGroup); |
| 267 | // Group Chain Hierarchy creation using group service and thus in device level |
| 268 | List<GroupBucket> l2McastBuckets = new ArrayList<>(); |
| 269 | groupInfos.forEach(groupInfo -> { |
| 270 | // Points to L2 interface group directly. |
| 271 | TrafficTreatment.Builder trafficTreatment = DefaultTrafficTreatment.builder(); |
| 272 | trafficTreatment.group(new GroupId(groupInfo.innerMostGroupDesc().givenGroupId())); |
| 273 | GroupBucket bucket = DefaultGroupBucket.createAllGroupBucket(trafficTreatment.build()); |
| 274 | l2McastBuckets.add(bucket); |
| 275 | }); |
| 276 | GroupDescription l2MulticastGroupDescription = |
| 277 | new DefaultGroupDescription( |
| 278 | deviceId, |
| 279 | ALL, |
| 280 | new GroupBuckets(l2McastBuckets), |
| 281 | l2MulticastGroupKey, |
| 282 | l2MulticastGroupId, |
| 283 | nextObj.appId()); |
| 284 | GroupChainElem l2MulticastGce = new GroupChainElem(l2MulticastGroupDescription, |
| 285 | groupInfos.size(), false, deviceId); |
| 286 | groupInfos.forEach(groupInfo -> { |
| 287 | updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), l2MulticastGce); |
| 288 | groupService.addGroup(groupInfo.innerMostGroupDesc()); |
| 289 | }); |
| 290 | } |
| 291 | |
| 292 | /** |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 293 | * As per the OFDPA 2.0 TTP, packets are sent out of ports by using |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 294 | * a chain of groups. The simple Next Objective passed in by the application |
| 295 | * is broken up into a group chain. The following chains can be created |
| 296 | * depending on the parameters in the Next Objective. |
| 297 | * 1. L2 Interface group (no chaining) |
| 298 | * 2. L3 Unicast group -> L2 Interface group |
| 299 | * 3. MPLS Interface group -> L2 Interface group |
| 300 | * 4. MPLS Swap group -> MPLS Interface group -> L2 Interface group |
| 301 | * 5. PW initiation group chain |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 302 | * |
| 303 | * @param nextObj the nextObjective of type SIMPLE |
| 304 | */ |
| 305 | private void processSimpleNextObjective(NextObjective nextObj) { |
| 306 | TrafficTreatment treatment = nextObj.next().iterator().next(); |
Andreas Pantelopoulos | 7a8488c | 2018-01-22 16:00:28 -0800 | [diff] [blame] | 307 | // determine if plain L2 or L3->L2 or MPLS Swap -> MPLS Interface -> L2 |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 308 | boolean plainL2 = true; |
Andreas Pantelopoulos | 7a8488c | 2018-01-22 16:00:28 -0800 | [diff] [blame] | 309 | boolean mplsSwap = false; |
| 310 | MplsLabel mplsLabel = null; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 311 | for (Instruction ins : treatment.allInstructions()) { |
| 312 | if (ins.type() == Instruction.Type.L2MODIFICATION) { |
| 313 | L2ModificationInstruction l2ins = (L2ModificationInstruction) ins; |
| 314 | if (l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_DST || |
| 315 | l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_SRC) { |
| 316 | plainL2 = false; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 317 | } |
Andreas Pantelopoulos | 7a8488c | 2018-01-22 16:00:28 -0800 | [diff] [blame] | 318 | // mpls label in simple next objectives is used only to indicate |
| 319 | // a MPLS Swap group before the MPLS Interface Group |
| 320 | if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_LABEL) { |
| 321 | mplsSwap = true; |
| 322 | mplsLabel = ((L2ModificationInstruction.ModMplsLabelInstruction) l2ins).label(); |
| 323 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 324 | } |
| 325 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 326 | if (plainL2) { |
| 327 | createL2InterfaceGroup(nextObj); |
| 328 | return; |
| 329 | } |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 330 | // In order to understand if it is a pseudowire related |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 331 | // next objective we look for the tunnel id in the meta. |
| 332 | boolean isPw = false; |
Pier Ventre | 140a894 | 2016-11-02 07:26:38 -0700 | [diff] [blame] | 333 | if (nextObj.meta() != null) { |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 334 | TunnelIdCriterion tunnelIdCriterion = (TunnelIdCriterion) nextObj |
| 335 | .meta() |
| 336 | .getCriterion(TUNNEL_ID); |
| 337 | if (tunnelIdCriterion != null) { |
| 338 | isPw = true; |
| 339 | } |
Pier Ventre | 140a894 | 2016-11-02 07:26:38 -0700 | [diff] [blame] | 340 | } |
Andreas Pantelopoulos | 25db42f | 2018-02-08 12:42:06 -0800 | [diff] [blame] | 341 | if (mplsSwap && !isPw) { |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 342 | log.debug("Creating a MPLS Swap -> MPLS Interface -> L2 Interface group chain."); |
Andreas Pantelopoulos | 7a8488c | 2018-01-22 16:00:28 -0800 | [diff] [blame] | 343 | // break up simple next objective to GroupChain objects |
| 344 | GroupInfo groupInfo = createL2L3Chain(treatment, nextObj.id(), |
| 345 | nextObj.appId(), true, |
| 346 | nextObj.meta()); |
| 347 | if (groupInfo == null) { |
| 348 | log.error("Could not process nextObj={} in dev:{}", nextObj.id(), deviceId); |
| 349 | fail(nextObj, ObjectiveError.BADPARAMS); |
| 350 | return; |
| 351 | } |
Andreas Pantelopoulos | 7a8488c | 2018-01-22 16:00:28 -0800 | [diff] [blame] | 352 | Deque<GroupKey> gkeyChain = new ArrayDeque<>(); |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 353 | gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie()); // l2 interface |
| 354 | gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie()); // mpls interface |
Andreas Pantelopoulos | 7a8488c | 2018-01-22 16:00:28 -0800 | [diff] [blame] | 355 | // creating the mpls swap group and adding it to the chain |
Andreas Pantelopoulos | 7a8488c | 2018-01-22 16:00:28 -0800 | [diff] [blame] | 356 | int nextGid = groupInfo.nextGroupDesc().givenGroupId(); |
| 357 | int index = getNextAvailableIndex(); |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 358 | GroupDescription swapGroupDescription = createMplsSwap( |
Andreas Pantelopoulos | 7a8488c | 2018-01-22 16:00:28 -0800 | [diff] [blame] | 359 | nextGid, |
| 360 | OfdpaMplsGroupSubType.MPLS_SWAP_LABEL, |
| 361 | index, |
| 362 | mplsLabel, |
| 363 | nextObj.appId() |
| 364 | ); |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 365 | // ensure swap group is added after L2L3 chain |
| 366 | GroupKey swapGroupKey = swapGroupDescription.appCookie(); |
| 367 | GroupChainElem swapChainElem = new GroupChainElem(swapGroupDescription, |
| 368 | 1, false, deviceId); |
| 369 | updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), swapChainElem); |
| 370 | gkeyChain.addFirst(swapGroupKey); |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 371 | // ensure nextObjective waits on the outermost groupKey |
| 372 | List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList(); |
| 373 | allGroupKeys.add(gkeyChain); |
| 374 | OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj); |
| 375 | updatePendingNextObjective(swapGroupKey, ofdpaGrp); |
Andreas Pantelopoulos | 7a8488c | 2018-01-22 16:00:28 -0800 | [diff] [blame] | 376 | // now we are ready to send the l2 groupDescription (inner), as all the stores |
| 377 | // that will get async replies have been updated. By waiting to update |
| 378 | // the stores, we prevent nasty race conditions. |
| 379 | groupService.addGroup(groupInfo.innerMostGroupDesc()); |
| 380 | } else if (!isPw) { |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 381 | boolean isMpls = false; |
| 382 | if (nextObj.meta() != null) { |
| 383 | isMpls = isNotMplsBos(nextObj.meta()); |
| 384 | } |
| 385 | log.debug("Creating a {} -> L2 Interface group chain.", |
| 386 | (isMpls) ? "MPLS Interface" : "L3 Unicast"); |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 387 | // break up simple next objective to GroupChain objects |
| 388 | GroupInfo groupInfo = createL2L3Chain(treatment, nextObj.id(), |
| 389 | nextObj.appId(), isMpls, |
| 390 | nextObj.meta()); |
| 391 | if (groupInfo == null) { |
| 392 | log.error("Could not process nextObj={} in dev:{}", nextObj.id(), deviceId); |
Andreas Pantelopoulos | 7a8488c | 2018-01-22 16:00:28 -0800 | [diff] [blame] | 393 | fail(nextObj, ObjectiveError.BADPARAMS); |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 394 | return; |
| 395 | } |
| 396 | // create object for local and distributed storage |
| 397 | Deque<GroupKey> gkeyChain = new ArrayDeque<>(); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 398 | gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie()); |
| 399 | gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie()); |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 400 | List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList(); |
| 401 | allGroupKeys.add(gkeyChain); |
| 402 | OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj); |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 403 | // store l3groupkey with the ofdpaNextGroup for the nextObjective that depends on it |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 404 | updatePendingNextObjective(groupInfo.nextGroupDesc().appCookie(), ofdpaGrp); |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 405 | // now we are ready to send the l2 groupDescription (inner), as all the stores |
| 406 | // that will get async replies have been updated. By waiting to update |
| 407 | // the stores, we prevent nasty race conditions. |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 408 | groupService.addGroup(groupInfo.innerMostGroupDesc()); |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 409 | } else { |
| 410 | // We handle the pseudo wire with a different a procedure. |
| 411 | // This procedure is meant to handle both initiation and |
| 412 | // termination of the pseudo wire. |
| 413 | processPwNextObjective(nextObj); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 414 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 415 | } |
| 416 | |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 417 | /** |
| 418 | * Creates a simple L2 Interface Group. |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 419 | * @param nextObj the next Objective |
| 420 | */ |
| 421 | private void createL2InterfaceGroup(NextObjective nextObj) { |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 422 | VlanId assignedVlan = readVlanFromSelector(nextObj.meta()); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 423 | if (assignedVlan == null) { |
| 424 | log.warn("VLAN ID required by simple next obj is missing. Abort."); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 425 | fail(nextObj, ObjectiveError.BADPARAMS); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 426 | return; |
| 427 | } |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 428 | List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 429 | // There is only one L2 interface group in this case |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 430 | GroupDescription l2InterfaceGroupDesc = groupInfos.get(0).innerMostGroupDesc(); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 431 | // Put all dependency information into allGroupKeys |
| 432 | List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList(); |
| 433 | Deque<GroupKey> gkeyChain = new ArrayDeque<>(); |
| 434 | gkeyChain.addFirst(l2InterfaceGroupDesc.appCookie()); |
| 435 | allGroupKeys.add(gkeyChain); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 436 | // Point the next objective to this group |
| 437 | OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj); |
| 438 | updatePendingNextObjective(l2InterfaceGroupDesc.appCookie(), ofdpaGrp); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 439 | // Start installing the inner-most group |
jayakumarthazhath | 655b9a8 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 440 | groupService.addGroup(l2InterfaceGroupDesc); } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 441 | |
| 442 | /** |
Andreas Pantelopoulos | 7a8488c | 2018-01-22 16:00:28 -0800 | [diff] [blame] | 443 | * Creates an Mpls group of type swap. |
| 444 | * |
| 445 | * @param nextGroupId the next group in the chain |
| 446 | * @param subtype the mpls swap label group subtype |
| 447 | * @param index the index of the group |
| 448 | * @param mplsLabel the mpls label to swap |
| 449 | * @param applicationId the application id |
| 450 | * @return the group description |
| 451 | */ |
| 452 | protected GroupDescription createMplsSwap(int nextGroupId, |
| 453 | OfdpaMplsGroupSubType subtype, |
| 454 | int index, |
| 455 | MplsLabel mplsLabel, |
| 456 | ApplicationId applicationId) { |
Andreas Pantelopoulos | 7a8488c | 2018-01-22 16:00:28 -0800 | [diff] [blame] | 457 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
Andreas Pantelopoulos | 7a8488c | 2018-01-22 16:00:28 -0800 | [diff] [blame] | 458 | treatment.setMpls(mplsLabel); |
Andreas Pantelopoulos | 7a8488c | 2018-01-22 16:00:28 -0800 | [diff] [blame] | 459 | // We point the group to the next group. |
| 460 | treatment.group(new GroupId(nextGroupId)); |
| 461 | GroupBucket groupBucket = DefaultGroupBucket |
| 462 | .createIndirectGroupBucket(treatment.build()); |
| 463 | // Finally we build the group description. |
| 464 | int groupId = makeMplsLabelGroupId(subtype, index); |
| 465 | GroupKey groupKey = new DefaultGroupKey( |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 466 | Ofdpa2Pipeline.appKryo.serialize(index)); |
Andreas Pantelopoulos | 7a8488c | 2018-01-22 16:00:28 -0800 | [diff] [blame] | 467 | return new DefaultGroupDescription( |
| 468 | deviceId, |
| 469 | INDIRECT, |
| 470 | new GroupBuckets(Collections.singletonList(groupBucket)), |
| 471 | groupKey, |
| 472 | groupId, |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 473 | applicationId); |
Andreas Pantelopoulos | 7a8488c | 2018-01-22 16:00:28 -0800 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | /** |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 477 | * Creates one of two possible group-chains from the treatment |
| 478 | * passed in. Depending on the MPLS boolean, this method either creates |
Charles Chan | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 479 | * an L3Unicast Group --> L2Interface Group, if mpls is false; |
| 480 | * or MPLSInterface Group --> L2Interface Group, if mpls is true; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 481 | * The returned 'inner' group description is always the L2 Interface group. |
| 482 | * |
| 483 | * @param treatment that needs to be broken up to create the group chain |
| 484 | * @param nextId of the next objective that needs this group chain |
| 485 | * @param appId of the application that sent this next objective |
| 486 | * @param mpls determines if L3Unicast or MPLSInterface group is created |
| 487 | * @param meta metadata passed in by the application as part of the nextObjective |
| 488 | * @return GroupInfo containing the GroupDescription of the |
| 489 | * L2Interface group(inner) and the GroupDescription of the (outer) |
| 490 | * L3Unicast/MPLSInterface group. May return null if there is an |
| 491 | * error in processing the chain |
| 492 | */ |
Charles Chan | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 493 | protected GroupInfo createL2L3Chain(TrafficTreatment treatment, int nextId, |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 494 | ApplicationId appId, boolean mpls, |
| 495 | TrafficSelector meta) { |
| 496 | return createL2L3ChainInternal(treatment, nextId, appId, mpls, meta, true); |
| 497 | } |
| 498 | |
| 499 | /** |
| 500 | * Internal implementation of createL2L3Chain. |
| 501 | * <p> |
| 502 | * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12. |
| 503 | * Since it is non-OF spec, we need an extension treatment for that. |
| 504 | * The useSetVlanExtension must be set to false for OFDPA i12. |
| 505 | * </p> |
| 506 | * |
| 507 | * @param treatment that needs to be broken up to create the group chain |
| 508 | * @param nextId of the next objective that needs this group chain |
| 509 | * @param appId of the application that sent this next objective |
| 510 | * @param mpls determines if L3Unicast or MPLSInterface group is created |
| 511 | * @param meta metadata passed in by the application as part of the nextObjective |
| 512 | * @param useSetVlanExtension use the setVlanVid extension that has is_present bit set to 0. |
| 513 | * @return GroupInfo containing the GroupDescription of the |
| 514 | * L2Interface group(inner) and the GroupDescription of the (outer) |
| 515 | * L3Unicast/MPLSInterface group. May return null if there is an |
| 516 | * error in processing the chain |
| 517 | */ |
| 518 | protected GroupInfo createL2L3ChainInternal(TrafficTreatment treatment, int nextId, |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 519 | ApplicationId appId, boolean mpls, |
| 520 | TrafficSelector meta, boolean useSetVlanExtension) { |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 521 | // for the l2interface group, get vlan and port info |
| 522 | // for the outer group, get the src/dst mac, and vlan info |
| 523 | TrafficTreatment.Builder outerTtb = DefaultTrafficTreatment.builder(); |
| 524 | TrafficTreatment.Builder innerTtb = DefaultTrafficTreatment.builder(); |
| 525 | VlanId vlanid = null; |
| 526 | long portNum = 0; |
| 527 | boolean setVlan = false, popVlan = false; |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 528 | MacAddress srcMac; |
| 529 | MacAddress dstMac; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 530 | for (Instruction ins : treatment.allInstructions()) { |
| 531 | if (ins.type() == Instruction.Type.L2MODIFICATION) { |
| 532 | L2ModificationInstruction l2ins = (L2ModificationInstruction) ins; |
| 533 | switch (l2ins.subtype()) { |
| 534 | case ETH_DST: |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 535 | dstMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac(); |
| 536 | outerTtb.setEthDst(dstMac); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 537 | break; |
| 538 | case ETH_SRC: |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 539 | srcMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac(); |
| 540 | outerTtb.setEthSrc(srcMac); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 541 | break; |
| 542 | case VLAN_ID: |
| 543 | vlanid = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId(); |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 544 | if (useSetVlanExtension) { |
| 545 | OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid); |
| 546 | outerTtb.extension(ofdpaSetVlanVid, deviceId); |
| 547 | } else { |
| 548 | outerTtb.setVlanId(vlanid); |
| 549 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 550 | setVlan = true; |
| 551 | break; |
| 552 | case VLAN_POP: |
| 553 | innerTtb.popVlan(); |
| 554 | popVlan = true; |
| 555 | break; |
| 556 | case DEC_MPLS_TTL: |
| 557 | case MPLS_LABEL: |
| 558 | case MPLS_POP: |
| 559 | case MPLS_PUSH: |
| 560 | case VLAN_PCP: |
| 561 | case VLAN_PUSH: |
| 562 | default: |
| 563 | break; |
| 564 | } |
| 565 | } else if (ins.type() == Instruction.Type.OUTPUT) { |
| 566 | portNum = ((Instructions.OutputInstruction) ins).port().toLong(); |
| 567 | innerTtb.add(ins); |
| 568 | } else { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 569 | log.debug("Driver does not handle this type of TrafficTreatment" |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 570 | + " instruction in l2l3chain: {} - {}", ins.type(), |
| 571 | ins); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 572 | } |
| 573 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 574 | if (vlanid == null && meta != null) { |
| 575 | // use metadata if available |
Pier Ventre | 140a894 | 2016-11-02 07:26:38 -0700 | [diff] [blame] | 576 | Criterion vidCriterion = meta.getCriterion(VLAN_VID); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 577 | if (vidCriterion != null) { |
| 578 | vlanid = ((VlanIdCriterion) vidCriterion).vlanId(); |
| 579 | } |
| 580 | // if vlan is not set, use the vlan in metadata for outerTtb |
| 581 | if (vlanid != null && !setVlan) { |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 582 | if (useSetVlanExtension) { |
| 583 | OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid); |
| 584 | outerTtb.extension(ofdpaSetVlanVid, deviceId); |
| 585 | } else { |
| 586 | outerTtb.setVlanId(vlanid); |
| 587 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 588 | } |
| 589 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 590 | if (vlanid == null) { |
| 591 | log.error("Driver cannot process an L2/L3 group chain without " |
| 592 | + "egress vlan information for dev: {} port:{}", |
| 593 | deviceId, portNum); |
| 594 | return null; |
| 595 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 596 | if (!setVlan && !popVlan) { |
| 597 | // untagged outgoing port |
| 598 | TrafficTreatment.Builder temp = DefaultTrafficTreatment.builder(); |
| 599 | temp.popVlan(); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 600 | innerTtb.build().allInstructions().forEach(temp::add); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 601 | innerTtb = temp; |
| 602 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 603 | // assemble information for ofdpa l2interface group |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 604 | int l2groupId = l2GroupId(vlanid, portNum); |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 605 | // a globally unique groupkey that is different for ports in the same device, |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 606 | // but different for the same portnumber on different devices. Also different |
| 607 | // for the various group-types created out of the same next objective. |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 608 | int l2gk = l2InterfaceGroupKey(deviceId, vlanid, portNum); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 609 | final GroupKey l2groupkey = new DefaultGroupKey(appKryo.serialize(l2gk)); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 610 | // assemble information for outer group |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 611 | GroupDescription outerGrpDesc; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 612 | if (mpls) { |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 613 | // outer group is MPLS Interface |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 614 | int mplsInterfaceIndex = getNextAvailableIndex(); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 615 | int mplsGroupId = MPLS_INTERFACE_TYPE | (SUBTYPE_MASK & mplsInterfaceIndex); |
| 616 | final GroupKey mplsGroupKey = new DefaultGroupKey( |
| 617 | appKryo.serialize(mplsInterfaceIndex)); |
Yi Tseng | fa394de | 2017-02-01 11:26:40 -0800 | [diff] [blame] | 618 | outerTtb.group(new GroupId(l2groupId)); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 619 | // create the mpls-interface group description to wait for the |
| 620 | // l2 interface group to be processed |
| 621 | GroupBucket mplsinterfaceGroupBucket = |
| 622 | DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build()); |
| 623 | outerGrpDesc = new DefaultGroupDescription( |
| 624 | deviceId, |
| 625 | GroupDescription.Type.INDIRECT, |
| 626 | new GroupBuckets(Collections.singletonList( |
| 627 | mplsinterfaceGroupBucket)), |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 628 | mplsGroupKey, |
| 629 | mplsGroupId, |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 630 | appId); |
| 631 | log.debug("Trying MPLS-Interface: device:{} gid:{} gkey:{} nextid:{}", |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 632 | deviceId, Integer.toHexString(mplsGroupId), |
| 633 | mplsGroupKey, nextId); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 634 | } else { |
| 635 | // outer group is L3Unicast |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 636 | int l3unicastIndex = getNextAvailableIndex(); |
| 637 | int l3groupId = L3_UNICAST_TYPE | (TYPE_MASK & l3unicastIndex); |
| 638 | final GroupKey l3groupkey = new DefaultGroupKey( |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 639 | appKryo.serialize(l3unicastIndex)); |
Yi Tseng | fa394de | 2017-02-01 11:26:40 -0800 | [diff] [blame] | 640 | outerTtb.group(new GroupId(l2groupId)); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 641 | // create the l3unicast group description to wait for the |
| 642 | // l2 interface group to be processed |
| 643 | GroupBucket l3unicastGroupBucket = |
| 644 | DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build()); |
| 645 | outerGrpDesc = new DefaultGroupDescription( |
| 646 | deviceId, |
| 647 | GroupDescription.Type.INDIRECT, |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 648 | new GroupBuckets(Collections.singletonList(l3unicastGroupBucket)), |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 649 | l3groupkey, |
| 650 | l3groupId, |
| 651 | appId); |
| 652 | log.debug("Trying L3Unicast: device:{} gid:{} gkey:{} nextid:{}", |
| 653 | deviceId, Integer.toHexString(l3groupId), |
| 654 | l3groupkey, nextId); |
| 655 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 656 | // store l2groupkey with the groupChainElem for the outer-group that depends on it |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 657 | GroupChainElem gce = new GroupChainElem(outerGrpDesc, 1, false, deviceId); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 658 | updatePendingGroups(l2groupkey, gce); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 659 | // create group description for the inner l2 interface group |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 660 | GroupBucket l2InterfaceGroupBucket = |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 661 | DefaultGroupBucket.createIndirectGroupBucket(innerTtb.build()); |
| 662 | GroupDescription l2groupDescription = |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 663 | new DefaultGroupDescription(deviceId, |
| 664 | GroupDescription.Type.INDIRECT, |
| 665 | new GroupBuckets(Collections.singletonList(l2InterfaceGroupBucket)), |
| 666 | l2groupkey, |
| 667 | l2groupId, |
| 668 | appId); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 669 | log.debug("Trying L2Interface: device:{} gid:{} gkey:{} nextId:{}", |
| 670 | deviceId, Integer.toHexString(l2groupId), |
| 671 | l2groupkey, nextId); |
| 672 | return new GroupInfo(l2groupDescription, outerGrpDesc); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | /** |
| 676 | * As per the OFDPA 2.0 TTP, packets are sent out of ports by using |
| 677 | * a chain of groups. The broadcast Next Objective passed in by the application |
| 678 | * has to be broken up into a group chain comprising of an |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 679 | * L2 Flood group or L3 Multicast group, whose buckets point to L2 Interface groups. |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 680 | * |
| 681 | * @param nextObj the nextObjective of type BROADCAST |
| 682 | */ |
| 683 | private void processBroadcastNextObjective(NextObjective nextObj) { |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 684 | VlanId assignedVlan = readVlanFromSelector(nextObj.meta()); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 685 | if (assignedVlan == null) { |
| 686 | log.warn("VLAN ID required by broadcast next obj is missing. Abort."); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 687 | fail(nextObj, ObjectiveError.BADPARAMS); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 688 | return; |
| 689 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 690 | |
jayakumarthazhath | 655b9a8 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 691 | // Handling L2 multicast cases. |
| 692 | MacAddress dstMac = readEthDstFromSelector(nextObj.meta()); |
| 693 | if (dstMac != null && dstMac.isMulticast()) { |
| 694 | processL2MulticastNextObjective(nextObj); |
Jayakumar Thazhath | 812aa76 | 2018-10-24 01:22:04 -0400 | [diff] [blame] | 695 | return; |
jayakumarthazhath | 655b9a8 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 696 | } |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 697 | |
jayakumarthazhath | 655b9a8 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 698 | List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 699 | IpPrefix ipDst = readIpDstFromSelector(nextObj.meta()); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 700 | if (ipDst != null) { |
| 701 | if (ipDst.isMulticast()) { |
| 702 | createL3MulticastGroup(nextObj, assignedVlan, groupInfos); |
| 703 | } else { |
| 704 | log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 705 | fail(nextObj, ObjectiveError.BADPARAMS); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 706 | } |
| 707 | } else { |
| 708 | createL2FloodGroup(nextObj, assignedVlan, groupInfos); |
| 709 | } |
| 710 | } |
| 711 | |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 712 | private List<GroupInfo> prepareL2InterfaceGroup(NextObjective nextObj, |
| 713 | VlanId assignedVlan) { |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 714 | ImmutableList.Builder<GroupInfo> groupInfoBuilder = ImmutableList.builder(); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 715 | // break up broadcast next objective to multiple groups |
| 716 | Collection<TrafficTreatment> buckets = nextObj.next(); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 717 | // each treatment is converted to an L2 interface group |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 718 | for (TrafficTreatment treatment : buckets) { |
| 719 | TrafficTreatment.Builder newTreatment = DefaultTrafficTreatment.builder(); |
| 720 | PortNumber portNum = null; |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 721 | VlanId egressVlan = null; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 722 | // ensure that the only allowed treatments are pop-vlan and output |
| 723 | for (Instruction ins : treatment.allInstructions()) { |
| 724 | if (ins.type() == Instruction.Type.L2MODIFICATION) { |
| 725 | L2ModificationInstruction l2ins = (L2ModificationInstruction) ins; |
| 726 | switch (l2ins.subtype()) { |
| 727 | case VLAN_POP: |
| 728 | newTreatment.add(l2ins); |
| 729 | break; |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 730 | case VLAN_ID: |
| 731 | egressVlan = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId(); |
| 732 | break; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 733 | default: |
| 734 | log.debug("action {} not permitted for broadcast nextObj", |
| 735 | l2ins.subtype()); |
| 736 | break; |
| 737 | } |
| 738 | } else if (ins.type() == Instruction.Type.OUTPUT) { |
| 739 | portNum = ((Instructions.OutputInstruction) ins).port(); |
| 740 | newTreatment.add(ins); |
| 741 | } else { |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 742 | log.debug("TrafficTreatment of type {} not permitted in " + |
| 743 | " broadcast nextObjective", ins.type()); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 744 | } |
| 745 | } |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 746 | if (portNum == null) { |
| 747 | log.warn("Can't find output port for the bucket {}.", treatment); |
| 748 | continue; |
| 749 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 750 | // assemble info for l2 interface group |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 751 | VlanId l2InterfaceGroupVlan = |
| 752 | (egressVlan != null && !assignedVlan.equals(egressVlan)) ? |
| 753 | egressVlan : assignedVlan; |
| 754 | int l2gk = l2InterfaceGroupKey(deviceId, l2InterfaceGroupVlan, portNum.toLong()); |
| 755 | final GroupKey l2InterfaceGroupKey = |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 756 | new DefaultGroupKey(appKryo.serialize(l2gk)); |
Charles Chan | 372b63e | 2017-02-07 12:10:53 -0800 | [diff] [blame] | 757 | int l2InterfaceGroupId = L2_INTERFACE_TYPE | |
| 758 | ((l2InterfaceGroupVlan.toShort() & THREE_BIT_MASK) << PORT_LEN) | |
| 759 | ((int) portNum.toLong() & FOUR_BIT_MASK); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 760 | GroupBucket l2InterfaceGroupBucket = |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 761 | DefaultGroupBucket.createIndirectGroupBucket(newTreatment.build()); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 762 | GroupDescription l2InterfaceGroupDescription = |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 763 | new DefaultGroupDescription(deviceId, |
| 764 | GroupDescription.Type.INDIRECT, |
| 765 | new GroupBuckets(Collections.singletonList( |
| 766 | l2InterfaceGroupBucket)), |
| 767 | l2InterfaceGroupKey, |
| 768 | l2InterfaceGroupId, |
| 769 | nextObj.appId()); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 770 | log.debug("Trying L2-Interface: device:{} gid:{} gkey:{} nextid:{}", |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 771 | deviceId, Integer.toHexString(l2InterfaceGroupId), |
| 772 | l2InterfaceGroupKey, nextObj.id()); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 773 | groupInfoBuilder.add(new GroupInfo(l2InterfaceGroupDescription, |
| 774 | l2InterfaceGroupDescription)); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 775 | } |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 776 | return groupInfoBuilder.build(); |
| 777 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 778 | |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 779 | private void createL2FloodGroup(NextObjective nextObj, VlanId vlanId, |
| 780 | List<GroupInfo> groupInfos) { |
| 781 | // assemble info for l2 flood group. Since there can be only one flood |
| 782 | // group for a vlan, its index is always the same - 0 |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 783 | Integer l2FloodGroupId = L2_FLOOD_TYPE | (vlanId.toShort() << 16); |
| 784 | final GroupKey l2FloodGroupKey = l2FloodGroupKey(vlanId, deviceId); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 785 | // collection of group buckets pointing to all the l2 interface groups |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 786 | List<GroupBucket> l2floodBuckets = generateNextGroupBuckets(groupInfos, ALL); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 787 | // create the l2flood group-description to wait for all the |
| 788 | // l2interface groups to be processed |
| 789 | GroupDescription l2floodGroupDescription = |
| 790 | new DefaultGroupDescription( |
| 791 | deviceId, |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 792 | ALL, |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 793 | new GroupBuckets(l2floodBuckets), |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 794 | l2FloodGroupKey, |
| 795 | l2FloodGroupId, |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 796 | nextObj.appId()); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 797 | log.debug("Trying L2-Flood: device:{} gid:{} gkey:{} nextid:{}", |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 798 | deviceId, Integer.toHexString(l2FloodGroupId), |
| 799 | l2FloodGroupKey, nextObj.id()); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 800 | // Put all dependency information into allGroupKeys |
| 801 | List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList(); |
| 802 | groupInfos.forEach(groupInfo -> { |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 803 | Deque<GroupKey> groupKeyChain = new ArrayDeque<>(); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 804 | // In this case we should have L2 interface group only |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 805 | groupKeyChain.addFirst(groupInfo.nextGroupDesc().appCookie()); |
| 806 | groupKeyChain.addFirst(l2FloodGroupKey); |
| 807 | allGroupKeys.add(groupKeyChain); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 808 | }); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 809 | // Point the next objective to this group |
| 810 | OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 811 | updatePendingNextObjective(l2FloodGroupKey, ofdpaGrp); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 812 | GroupChainElem gce = new GroupChainElem(l2floodGroupDescription, |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 813 | groupInfos.size(), false, deviceId); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 814 | groupInfos.forEach(groupInfo -> { |
| 815 | // Point this group to the next group |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 816 | updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), gce); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 817 | // Start installing the inner-most group |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 818 | groupService.addGroup(groupInfo.innerMostGroupDesc()); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 819 | }); |
| 820 | } |
| 821 | |
Pier Luigi | 21fffd2 | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 822 | private void createL3MulticastGroup(NextObjective nextObj, VlanId vlanId, |
| 823 | List<GroupInfo> groupInfos) { |
| 824 | // Let's create a new list mcast buckets |
| 825 | List<GroupBucket> l3McastBuckets = createL3MulticastBucket(groupInfos); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 826 | |
| 827 | int l3MulticastIndex = getNextAvailableIndex(); |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 828 | int l3MulticastGroupId = L3_MULTICAST_TYPE | |
| 829 | vlanId.toShort() << 16 | (TYPE_VLAN_MASK & l3MulticastIndex); |
| 830 | final GroupKey l3MulticastGroupKey = |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 831 | new DefaultGroupKey(appKryo.serialize(l3MulticastIndex)); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 832 | |
| 833 | GroupDescription l3MulticastGroupDesc = new DefaultGroupDescription(deviceId, |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 834 | ALL, |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 835 | new GroupBuckets(l3McastBuckets), |
| 836 | l3MulticastGroupKey, |
| 837 | l3MulticastGroupId, |
| 838 | nextObj.appId()); |
| 839 | |
| 840 | // Put all dependency information into allGroupKeys |
| 841 | List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList(); |
| 842 | groupInfos.forEach(groupInfo -> { |
| 843 | Deque<GroupKey> gkeyChain = new ArrayDeque<>(); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 844 | gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie()); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 845 | // Add L3 interface group to the chain if there is one. |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 846 | if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) { |
| 847 | gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie()); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 848 | } |
| 849 | gkeyChain.addFirst(l3MulticastGroupKey); |
| 850 | allGroupKeys.add(gkeyChain); |
| 851 | }); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 852 | // Point the next objective to this group |
| 853 | OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj); |
| 854 | updatePendingNextObjective(l3MulticastGroupKey, ofdpaGrp); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 855 | GroupChainElem outerGce = new GroupChainElem(l3MulticastGroupDesc, |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 856 | groupInfos.size(), false, deviceId); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 857 | groupInfos.forEach(groupInfo -> { |
| 858 | // Point this group (L3 multicast) to the next group |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 859 | updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), outerGce); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 860 | // Point next group to inner-most group, if any |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 861 | if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) { |
| 862 | GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc(), |
| 863 | 1, false, deviceId); |
| 864 | updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), innerGce); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 865 | } |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 866 | // Start installing the inner-most group |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 867 | groupService.addGroup(groupInfo.innerMostGroupDesc()); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 868 | }); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 869 | } |
| 870 | |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 871 | /** |
| 872 | * As per the OFDPA 2.0 TTP, packets are sent out of ports by using |
| 873 | * a chain of groups. The hashed Next Objective passed in by the application |
| 874 | * has to be broken up into a group chain comprising of an |
| 875 | * L3 ECMP group as the top level group. Buckets of this group can point |
| 876 | * to a variety of groups in a group chain, depending on the whether |
| 877 | * MPLS labels are being pushed or not. |
| 878 | * <p> |
| 879 | * NOTE: We do not create MPLS ECMP groups as they are unimplemented in |
| 880 | * OF-DPA 2.0 (even though it is in the spec). Therefore we do not |
| 881 | * check the nextObjective meta to see what is matching before being |
| 882 | * sent to this nextObjective. |
| 883 | * |
| 884 | * @param nextObj the nextObjective of type HASHED |
| 885 | */ |
Pier Ventre | 140a894 | 2016-11-02 07:26:38 -0700 | [diff] [blame] | 886 | protected void processHashedNextObjective(NextObjective nextObj) { |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 887 | // storage for all group keys in the chain of groups created |
| 888 | List<Deque<GroupKey>> allGroupKeys = new ArrayList<>(); |
| 889 | List<GroupInfo> unsentGroups = new ArrayList<>(); |
| 890 | createHashBucketChains(nextObj, allGroupKeys, unsentGroups); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 891 | // now we can create the outermost L3 ECMP group |
| 892 | List<GroupBucket> l3ecmpGroupBuckets = new ArrayList<>(); |
| 893 | for (GroupInfo gi : unsentGroups) { |
| 894 | // create ECMP bucket to point to the outer group |
| 895 | TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder(); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 896 | ttb.group(new GroupId(gi.nextGroupDesc().givenGroupId())); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 897 | GroupBucket sbucket = DefaultGroupBucket |
| 898 | .createSelectGroupBucket(ttb.build()); |
| 899 | l3ecmpGroupBuckets.add(sbucket); |
| 900 | } |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 901 | int l3ecmpIndex = getNextAvailableIndex(); |
| 902 | int l3ecmpGroupId = L3_ECMP_TYPE | (TYPE_MASK & l3ecmpIndex); |
| 903 | GroupKey l3ecmpGroupKey = new DefaultGroupKey( |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 904 | appKryo.serialize(l3ecmpIndex)); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 905 | GroupDescription l3ecmpGroupDesc = |
| 906 | new DefaultGroupDescription( |
| 907 | deviceId, |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 908 | SELECT, |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 909 | new GroupBuckets(l3ecmpGroupBuckets), |
| 910 | l3ecmpGroupKey, |
| 911 | l3ecmpGroupId, |
| 912 | nextObj.appId()); |
| 913 | GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc, |
| 914 | l3ecmpGroupBuckets.size(), |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 915 | false, deviceId); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 916 | |
| 917 | // create objects for local and distributed storage |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 918 | allGroupKeys.forEach(gKeyChain -> gKeyChain.addFirst(l3ecmpGroupKey)); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 919 | OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 920 | // store l3ecmpGroupKey with the ofdpaGroupChain for the nextObjective |
| 921 | // that depends on it |
| 922 | updatePendingNextObjective(l3ecmpGroupKey, ofdpaGrp); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 923 | log.debug("Trying L3ECMP: device:{} gid:{} gkey:{} nextId:{}", |
| 924 | deviceId, Integer.toHexString(l3ecmpGroupId), |
| 925 | l3ecmpGroupKey, nextObj.id()); |
| 926 | // finally we are ready to send the innermost groups |
| 927 | for (GroupInfo gi : unsentGroups) { |
| 928 | log.debug("Sending innermost group {} in group chain on device {} ", |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 929 | Integer.toHexString(gi.innerMostGroupDesc().givenGroupId()), deviceId); |
| 930 | updatePendingGroups(gi.nextGroupDesc().appCookie(), l3ecmpGce); |
| 931 | groupService.addGroup(gi.innerMostGroupDesc()); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 932 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | /** |
| 936 | * Creates group chains for all buckets in a hashed group, and stores the |
| 937 | * GroupInfos and GroupKeys for all the groups in the lists passed in, which |
| 938 | * should be empty. |
| 939 | * <p> |
| 940 | * Does not create the top level ECMP group. Does not actually send the |
| 941 | * groups to the groupService. |
| 942 | * |
| 943 | * @param nextObj the Next Objective with buckets that need to be converted |
| 944 | * to group chains |
| 945 | * @param allGroupKeys a list to store groupKey for each bucket-group-chain |
| 946 | * @param unsentGroups a list to store GroupInfo for each bucket-group-chain |
| 947 | */ |
Pier Ventre | 140a894 | 2016-11-02 07:26:38 -0700 | [diff] [blame] | 948 | protected void createHashBucketChains(NextObjective nextObj, |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 949 | List<Deque<GroupKey>> allGroupKeys, |
| 950 | List<GroupInfo> unsentGroups) { |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 951 | // break up hashed next objective to multiple groups |
| 952 | Collection<TrafficTreatment> buckets = nextObj.next(); |
| 953 | |
| 954 | for (TrafficTreatment bucket : buckets) { |
| 955 | //figure out how many labels are pushed in each bucket |
| 956 | int labelsPushed = 0; |
| 957 | MplsLabel innermostLabel = null; |
| 958 | for (Instruction ins : bucket.allInstructions()) { |
| 959 | if (ins.type() == Instruction.Type.L2MODIFICATION) { |
| 960 | L2ModificationInstruction l2ins = (L2ModificationInstruction) ins; |
| 961 | if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_PUSH) { |
| 962 | labelsPushed++; |
| 963 | } |
| 964 | if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_LABEL) { |
| 965 | if (innermostLabel == null) { |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 966 | innermostLabel = |
| 967 | ((L2ModificationInstruction.ModMplsLabelInstruction) l2ins).label(); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 968 | } |
| 969 | } |
| 970 | } |
| 971 | } |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 972 | Deque<GroupKey> gKeyChain = new ArrayDeque<>(); |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 973 | // here we only deal with 0 and 1 label push |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 974 | if (labelsPushed == 0) { |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 975 | GroupInfo noLabelGroupInfo; |
Pier Ventre | 140a894 | 2016-11-02 07:26:38 -0700 | [diff] [blame] | 976 | TrafficSelector metaSelector = nextObj.meta(); |
| 977 | if (metaSelector != null) { |
| 978 | if (isNotMplsBos(metaSelector)) { |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 979 | noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(), |
| 980 | nextObj.appId(), true, |
| 981 | nextObj.meta()); |
Pier Ventre | 140a894 | 2016-11-02 07:26:38 -0700 | [diff] [blame] | 982 | } else { |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 983 | noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(), |
| 984 | nextObj.appId(), false, |
| 985 | nextObj.meta()); |
Pier Ventre | 140a894 | 2016-11-02 07:26:38 -0700 | [diff] [blame] | 986 | } |
| 987 | } else { |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 988 | noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(), |
| 989 | nextObj.appId(), false, |
| 990 | nextObj.meta()); |
Pier Ventre | 140a894 | 2016-11-02 07:26:38 -0700 | [diff] [blame] | 991 | } |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 992 | if (noLabelGroupInfo == null) { |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 993 | log.error("Could not process nextObj={} in dev:{}", |
| 994 | nextObj.id(), deviceId); |
| 995 | return; |
| 996 | } |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 997 | gKeyChain.addFirst(noLabelGroupInfo.innerMostGroupDesc().appCookie()); |
| 998 | gKeyChain.addFirst(noLabelGroupInfo.nextGroupDesc().appCookie()); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 999 | // we can't send the inner group description yet, as we have to |
| 1000 | // create the dependent ECMP group first. So we store.. |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1001 | unsentGroups.add(noLabelGroupInfo); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1002 | } else if (labelsPushed == 1) { |
| 1003 | GroupInfo onelabelGroupInfo = createL2L3Chain(bucket, nextObj.id(), |
| 1004 | nextObj.appId(), true, |
| 1005 | nextObj.meta()); |
| 1006 | if (onelabelGroupInfo == null) { |
| 1007 | log.error("Could not process nextObj={} in dev:{}", |
| 1008 | nextObj.id(), deviceId); |
| 1009 | return; |
| 1010 | } |
| 1011 | // we need to add another group to this chain - the L3VPN group |
| 1012 | TrafficTreatment.Builder l3vpnTtb = DefaultTrafficTreatment.builder(); |
Charles Chan | 0f43e47 | 2017-02-14 14:00:16 -0800 | [diff] [blame] | 1013 | if (requireVlanPopBeforeMplsPush()) { |
| 1014 | l3vpnTtb.popVlan(); |
| 1015 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1016 | l3vpnTtb.pushMpls() |
| 1017 | .setMpls(innermostLabel) |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1018 | .group(new GroupId(onelabelGroupInfo.nextGroupDesc().givenGroupId())); |
Charles Chan | 40132b3 | 2017-01-22 00:19:37 -0800 | [diff] [blame] | 1019 | if (supportCopyTtl()) { |
| 1020 | l3vpnTtb.copyTtlOut(); |
| 1021 | } |
| 1022 | if (supportSetMplsBos()) { |
| 1023 | l3vpnTtb.setMplsBos(true); |
| 1024 | } |
Charles Chan | 0f43e47 | 2017-02-14 14:00:16 -0800 | [diff] [blame] | 1025 | if (requireVlanPopBeforeMplsPush()) { |
| 1026 | l3vpnTtb.pushVlan().setVlanId(VlanId.vlanId(VlanId.RESERVED)); |
| 1027 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1028 | GroupBucket l3vpnGrpBkt = |
| 1029 | DefaultGroupBucket.createIndirectGroupBucket(l3vpnTtb.build()); |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1030 | int l3vpnIndex = getNextAvailableIndex(); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1031 | int l3vpnGroupId = MPLS_L3VPN_SUBTYPE | (SUBTYPE_MASK & l3vpnIndex); |
| 1032 | GroupKey l3vpnGroupKey = new DefaultGroupKey( |
| 1033 | appKryo.serialize(l3vpnIndex)); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1034 | GroupDescription l3vpnGroupDesc = |
| 1035 | new DefaultGroupDescription( |
| 1036 | deviceId, |
| 1037 | GroupDescription.Type.INDIRECT, |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1038 | new GroupBuckets(Collections.singletonList(l3vpnGrpBkt)), |
| 1039 | l3vpnGroupKey, |
| 1040 | l3vpnGroupId, |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1041 | nextObj.appId()); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1042 | GroupChainElem l3vpnGce = new GroupChainElem(l3vpnGroupDesc, |
| 1043 | 1, |
| 1044 | false, |
| 1045 | deviceId); |
| 1046 | updatePendingGroups(onelabelGroupInfo.nextGroupDesc().appCookie(), l3vpnGce); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1047 | |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1048 | gKeyChain.addFirst(onelabelGroupInfo.innerMostGroupDesc().appCookie()); |
| 1049 | gKeyChain.addFirst(onelabelGroupInfo.nextGroupDesc().appCookie()); |
| 1050 | gKeyChain.addFirst(l3vpnGroupKey); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1051 | //now we can replace the outerGrpDesc with the one we just created |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1052 | onelabelGroupInfo.nextGroupDesc(l3vpnGroupDesc); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1053 | // we can't send the innermost group yet, as we have to create |
| 1054 | // the dependent ECMP group first. So we store ... |
| 1055 | unsentGroups.add(onelabelGroupInfo); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1056 | log.debug("Trying L3VPN: device:{} gid:{} group key:{} nextId:{}", |
| 1057 | deviceId, Integer.toHexString(l3vpnGroupId), |
| 1058 | l3vpnGroupKey, nextObj.id()); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1059 | } else { |
| 1060 | log.warn("Driver currently does not handle more than 1 MPLS " |
| 1061 | + "labels. Not processing nextObjective {}", nextObj.id()); |
| 1062 | return; |
| 1063 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1064 | // all groups in this chain |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1065 | allGroupKeys.add(gKeyChain); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1066 | } |
| 1067 | } |
| 1068 | |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1069 | /** |
| 1070 | * Processes the pseudo wire related next objective. |
| 1071 | * This procedure try to reuse the mpls label groups, |
| 1072 | * the mpls interface group and the l2 interface group. |
| 1073 | * |
| 1074 | * @param nextObjective the objective to process. |
| 1075 | */ |
| 1076 | protected void processPwNextObjective(NextObjective nextObjective) { |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 1077 | log.warn("Pseudo wire extensions are not supported in OFDPA 2.0 {}", |
| 1078 | nextObjective.id()); |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1079 | } |
| 1080 | |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1081 | ////////////////////////////////////// |
| 1082 | // Group Editing |
| 1083 | ////////////////////////////////////// |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1084 | /** |
| 1085 | * Adds a bucket to the top level group of a group-chain, and creates the chain. |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1086 | * Ensures that bucket being added is not a duplicate, by checking existing |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1087 | * buckets for the same output port. |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1088 | * |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1089 | * @param nextObjective the bucket information for a next group |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1090 | * @param next the representation of the existing group-chain for this next objective |
| 1091 | */ |
| 1092 | protected void addBucketToGroup(NextObjective nextObjective, NextGroup next) { |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1093 | if (nextObjective.type() != NextObjective.Type.HASHED && |
| 1094 | nextObjective.type() != NextObjective.Type.BROADCAST) { |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1095 | log.warn("AddBuckets not applied to nextType:{} in dev:{} for next:{}", |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1096 | nextObjective.type(), deviceId, nextObjective.id()); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1097 | fail(nextObjective, ObjectiveError.UNSUPPORTED); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1098 | return; |
| 1099 | } |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1100 | // first check to see if bucket being added is not a duplicate of an |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1101 | // existing bucket. If it is for an existing output port, then its a |
| 1102 | // duplicate. |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1103 | Set<TrafficTreatment> duplicateBuckets = Sets.newHashSet(); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1104 | List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data()); |
| 1105 | Set<PortNumber> existingPorts = getExistingOutputPorts(allActiveKeys, |
| 1106 | groupService, |
| 1107 | deviceId); |
Yi Tseng | 47f82dc | 2017-03-05 22:48:39 -0800 | [diff] [blame] | 1108 | Set<TrafficTreatment> nonDuplicateBuckets = Sets.newHashSet(); |
| 1109 | NextObjective objectiveToAdd; |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1110 | nextObjective.next().forEach(trafficTreatment -> { |
| 1111 | PortNumber portNumber = readOutPortFromTreatment(trafficTreatment); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1112 | if (portNumber == null) { |
| 1113 | return; |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1114 | } |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1115 | if (existingPorts.contains(portNumber)) { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1116 | // its possible that portnumbers are same but labels are different |
| 1117 | int label = readLabelFromTreatment(trafficTreatment); |
| 1118 | if (label == -1) { |
| 1119 | duplicateBuckets.add(trafficTreatment); |
| 1120 | } else { |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1121 | List<Integer> existing = existingPortAndLabel(allActiveKeys, |
| 1122 | groupService, deviceId, |
| 1123 | portNumber, label); |
| 1124 | if (!existing.isEmpty()) { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1125 | duplicateBuckets.add(trafficTreatment); |
| 1126 | } else { |
| 1127 | nonDuplicateBuckets.add(trafficTreatment); |
| 1128 | } |
| 1129 | } |
Yi Tseng | 47f82dc | 2017-03-05 22:48:39 -0800 | [diff] [blame] | 1130 | } else { |
| 1131 | nonDuplicateBuckets.add(trafficTreatment); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1132 | } |
| 1133 | }); |
Saurav Das | b28d5dd | 2017-03-24 19:03:58 -0700 | [diff] [blame] | 1134 | if (duplicateBuckets.isEmpty()) { |
| 1135 | // use the original objective |
| 1136 | objectiveToAdd = nextObjective; |
| 1137 | } else if (!nonDuplicateBuckets.isEmpty()) { |
| 1138 | // only use the non-duplicate buckets if there are any |
| 1139 | log.debug("Some buckets {} already exist in next id {}, duplicate " |
| 1140 | + "buckets will be ignored.", duplicateBuckets, nextObjective.id()); |
| 1141 | // new next objective with non duplicate treatments |
Yi Tseng | 47f82dc | 2017-03-05 22:48:39 -0800 | [diff] [blame] | 1142 | NextObjective.Builder builder = DefaultNextObjective.builder() |
| 1143 | .withType(nextObjective.type()) |
| 1144 | .withId(nextObjective.id()) |
| 1145 | .withMeta(nextObjective.meta()) |
| 1146 | .fromApp(nextObjective.appId()); |
| 1147 | nonDuplicateBuckets.forEach(builder::addTreatment); |
Yi Tseng | 47f82dc | 2017-03-05 22:48:39 -0800 | [diff] [blame] | 1148 | ObjectiveContext context = nextObjective.context().orElse(null); |
| 1149 | objectiveToAdd = builder.addToExisting(context); |
| 1150 | } else { |
Saurav Das | b28d5dd | 2017-03-24 19:03:58 -0700 | [diff] [blame] | 1151 | // buckets to add are already there - nothing to do |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 1152 | log.debug("buckets already exist {} in next: {} ..ignoring bucket add", |
| 1153 | duplicateBuckets, nextObjective.id()); |
| 1154 | pass(nextObjective); |
Saurav Das | b28d5dd | 2017-03-24 19:03:58 -0700 | [diff] [blame] | 1155 | return; |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1156 | } |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1157 | if (nextObjective.type() == NextObjective.Type.HASHED) { |
Yi Tseng | 47f82dc | 2017-03-05 22:48:39 -0800 | [diff] [blame] | 1158 | addBucketToHashGroup(objectiveToAdd, allActiveKeys); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1159 | } else if (nextObjective.type() == NextObjective.Type.BROADCAST) { |
Yi Tseng | 47f82dc | 2017-03-05 22:48:39 -0800 | [diff] [blame] | 1160 | addBucketToBroadcastGroup(objectiveToAdd, allActiveKeys); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1161 | } |
| 1162 | } |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1163 | |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1164 | private void addBucketToHashGroup(NextObjective nextObjective, |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1165 | List<Deque<GroupKey>> allActiveKeys) { |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1166 | // storage for all group keys in the chain of groups created |
| 1167 | List<Deque<GroupKey>> allGroupKeys = new ArrayList<>(); |
| 1168 | List<GroupInfo> unsentGroups = new ArrayList<>(); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1169 | List<GroupBucket> newBuckets; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1170 | createHashBucketChains(nextObjective, allGroupKeys, unsentGroups); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1171 | // now we can create the buckets to add to the outermost L3 ECMP group |
| 1172 | newBuckets = generateNextGroupBuckets(unsentGroups, SELECT); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1173 | // retrieve the original L3 ECMP group |
Jonghwan Hyun | f810a7a | 2018-02-12 16:43:45 +0900 | [diff] [blame] | 1174 | Group l3ecmpGroup = retrieveTopLevelGroup(allActiveKeys, deviceId, |
| 1175 | groupService, nextObjective.id()); |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1176 | if (l3ecmpGroup == null) { |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1177 | fail(nextObjective, ObjectiveError.GROUPMISSING); |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1178 | return; |
| 1179 | } |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1180 | GroupKey l3ecmpGroupKey = l3ecmpGroup.appCookie(); |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1181 | int l3ecmpGroupId = l3ecmpGroup.id().id(); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1182 | // Although GroupDescriptions are not necessary for adding buckets to |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1183 | // existing groups, we still use one in the GroupChainElem. When the latter is |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1184 | // processed, the info will be extracted for the bucketAdd call to groupService |
| 1185 | GroupDescription l3ecmpGroupDesc = |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1186 | new DefaultGroupDescription(deviceId, |
| 1187 | SELECT, |
| 1188 | new GroupBuckets(newBuckets), |
| 1189 | l3ecmpGroupKey, |
| 1190 | l3ecmpGroupId, |
| 1191 | nextObjective.appId()); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1192 | GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc, |
| 1193 | unsentGroups.size(), |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1194 | true, |
| 1195 | deviceId); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1196 | |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1197 | // update new bucket-chains |
| 1198 | List<Deque<GroupKey>> addedKeys = new ArrayList<>(); |
| 1199 | for (Deque<GroupKey> newBucketChain : allGroupKeys) { |
| 1200 | newBucketChain.addFirst(l3ecmpGroupKey); |
| 1201 | addedKeys.add(newBucketChain); |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1202 | } |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1203 | updatePendingNextObjective(l3ecmpGroupKey, |
| 1204 | new OfdpaNextGroup(addedKeys, nextObjective)); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1205 | log.debug("Adding to L3ECMP: device:{} gid:{} group key:{} nextId:{}", |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1206 | deviceId, Integer.toHexString(l3ecmpGroupId), |
| 1207 | l3ecmpGroupKey, nextObjective.id()); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1208 | unsentGroups.forEach(groupInfo -> { |
| 1209 | // send the innermost group |
| 1210 | log.debug("Sending innermost group {} in group chain on device {} ", |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1211 | Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()), |
| 1212 | deviceId); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1213 | updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l3ecmpGce); |
| 1214 | groupService.addGroup(groupInfo.innerMostGroupDesc()); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1215 | }); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1216 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1217 | |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1218 | private void addBucketToBroadcastGroup(NextObjective nextObj, |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1219 | List<Deque<GroupKey>> allActiveKeys) { |
| 1220 | VlanId assignedVlan = readVlanFromSelector(nextObj.meta()); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1221 | if (assignedVlan == null) { |
| 1222 | log.warn("VLAN ID required by broadcast next obj is missing. " |
| 1223 | + "Aborting add bucket to broadcast group for next:{} in dev:{}", |
| 1224 | nextObj.id(), deviceId); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1225 | fail(nextObj, ObjectiveError.BADPARAMS); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1226 | return; |
| 1227 | } |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1228 | List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1229 | IpPrefix ipDst = readIpDstFromSelector(nextObj.meta()); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1230 | if (ipDst != null) { |
| 1231 | if (ipDst.isMulticast()) { |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1232 | addBucketToL3MulticastGroup(nextObj, allActiveKeys, groupInfos, assignedVlan); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1233 | } else { |
| 1234 | log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1235 | fail(nextObj, ObjectiveError.BADPARAMS); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1236 | } |
| 1237 | } else { |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1238 | addBucketToL2FloodGroup(nextObj, allActiveKeys, groupInfos, assignedVlan); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1239 | } |
| 1240 | } |
| 1241 | |
| 1242 | private void addBucketToL2FloodGroup(NextObjective nextObj, |
| 1243 | List<Deque<GroupKey>> allActiveKeys, |
| 1244 | List<GroupInfo> groupInfos, |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1245 | VlanId assignedVlan) { |
Jonghwan Hyun | f810a7a | 2018-02-12 16:43:45 +0900 | [diff] [blame] | 1246 | Group l2FloodGroup = retrieveTopLevelGroup(allActiveKeys, deviceId, |
| 1247 | groupService, nextObj.id()); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1248 | |
| 1249 | if (l2FloodGroup == null) { |
| 1250 | log.warn("Can't find L2 flood group while adding bucket to it. NextObj = {}", |
| 1251 | nextObj); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1252 | fail(nextObj, ObjectiveError.GROUPMISSING); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1253 | return; |
| 1254 | } |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1255 | |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1256 | GroupKey l2floodGroupKey = l2FloodGroup.appCookie(); |
| 1257 | int l2floodGroupId = l2FloodGroup.id().id(); |
| 1258 | List<GroupBucket> newBuckets = generateNextGroupBuckets(groupInfos, ALL); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1259 | GroupDescription l2FloodGroupDescription = |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1260 | new DefaultGroupDescription(deviceId, |
| 1261 | ALL, |
| 1262 | new GroupBuckets(newBuckets), |
| 1263 | l2floodGroupKey, |
| 1264 | l2floodGroupId, |
| 1265 | nextObj.appId()); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1266 | GroupChainElem l2FloodGroupChainElement = |
| 1267 | new GroupChainElem(l2FloodGroupDescription, |
| 1268 | groupInfos.size(), |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1269 | true, |
| 1270 | deviceId); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1271 | |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1272 | |
| 1273 | //ensure assignedVlan applies to the chosen group |
| 1274 | VlanId floodGroupVlan = extractVlanIdFromGroupId(l2floodGroupId); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1275 | if (!floodGroupVlan.equals(assignedVlan)) { |
| 1276 | log.warn("VLAN ID {} does not match Flood group {} to which bucket is " |
| 1277 | + "being added, for next:{} in dev:{}. Abort.", assignedVlan, |
| 1278 | Integer.toHexString(l2floodGroupId), nextObj.id(), deviceId); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1279 | fail(nextObj, ObjectiveError.BADPARAMS); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1280 | return; |
| 1281 | } |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1282 | List<Deque<GroupKey>> addedKeys = new ArrayList<>(); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1283 | groupInfos.forEach(groupInfo -> { |
| 1284 | // update original NextGroup with new bucket-chain |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1285 | Deque<GroupKey> newBucketChain = new ArrayDeque<>(); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1286 | newBucketChain.addFirst(groupInfo.nextGroupDesc().appCookie()); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1287 | newBucketChain.addFirst(l2floodGroupKey); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1288 | addedKeys.add(newBucketChain); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1289 | log.debug("Adding to L2FLOOD: device:{} gid:{} group key:{} nextId:{}", |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1290 | deviceId, Integer.toHexString(l2floodGroupId), |
| 1291 | l2floodGroupKey, nextObj.id()); |
| 1292 | // send the innermost group |
| 1293 | log.debug("Sending innermost group {} in group chain on device {} ", |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1294 | Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()), |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1295 | deviceId); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1296 | updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l2FloodGroupChainElement); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1297 | |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1298 | DeviceId innerMostGroupDevice = groupInfo.innerMostGroupDesc().deviceId(); |
| 1299 | GroupKey innerMostGroupKey = groupInfo.innerMostGroupDesc().appCookie(); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1300 | Group existsL2IGroup = groupService.getGroup(innerMostGroupDevice, innerMostGroupKey); |
| 1301 | |
| 1302 | if (existsL2IGroup != null) { |
| 1303 | // group already exist |
| 1304 | processPendingAddGroupsOrNextObjs(innerMostGroupKey, true); |
| 1305 | } else { |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1306 | groupService.addGroup(groupInfo.innerMostGroupDesc()); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1307 | } |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1308 | }); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1309 | |
| 1310 | updatePendingNextObjective(l2floodGroupKey, |
| 1311 | new OfdpaNextGroup(addedKeys, nextObj)); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1312 | } |
| 1313 | |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1314 | private void addBucketToL3MulticastGroup(NextObjective nextObj, |
| 1315 | List<Deque<GroupKey>> allActiveKeys, |
| 1316 | List<GroupInfo> groupInfos, |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1317 | VlanId assignedVlan) { |
Pier Luigi | 21fffd2 | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 1318 | // Create the buckets to add to the outermost L3 Multicast group |
| 1319 | List<GroupBucket> newBuckets = createL3MulticastBucket(groupInfos); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1320 | |
| 1321 | // get the group being edited |
Jonghwan Hyun | f810a7a | 2018-02-12 16:43:45 +0900 | [diff] [blame] | 1322 | Group l3mcastGroup = retrieveTopLevelGroup(allActiveKeys, deviceId, |
| 1323 | groupService, nextObj.id()); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1324 | if (l3mcastGroup == null) { |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1325 | fail(nextObj, ObjectiveError.GROUPMISSING); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1326 | return; |
| 1327 | } |
| 1328 | GroupKey l3mcastGroupKey = l3mcastGroup.appCookie(); |
| 1329 | int l3mcastGroupId = l3mcastGroup.id().id(); |
| 1330 | |
| 1331 | //ensure assignedVlan applies to the chosen group |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1332 | VlanId expectedVlan = extractVlanIdFromGroupId(l3mcastGroupId); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1333 | if (!expectedVlan.equals(assignedVlan)) { |
| 1334 | log.warn("VLAN ID {} does not match L3 Mcast group {} to which bucket is " |
| 1335 | + "being added, for next:{} in dev:{}. Abort.", assignedVlan, |
| 1336 | Integer.toHexString(l3mcastGroupId), nextObj.id(), deviceId); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1337 | fail(nextObj, ObjectiveError.BADPARAMS); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1338 | } |
| 1339 | GroupDescription l3mcastGroupDescription = |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1340 | new DefaultGroupDescription(deviceId, |
| 1341 | ALL, |
| 1342 | new GroupBuckets(newBuckets), |
| 1343 | l3mcastGroupKey, |
| 1344 | l3mcastGroupId, |
| 1345 | nextObj.appId()); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1346 | GroupChainElem l3mcastGce = new GroupChainElem(l3mcastGroupDescription, |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1347 | groupInfos.size(), |
| 1348 | true, |
| 1349 | deviceId); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1350 | |
| 1351 | List<Deque<GroupKey>> addedKeys = new ArrayList<>(); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1352 | groupInfos.forEach(groupInfo -> { |
| 1353 | // update original NextGroup with new bucket-chain |
| 1354 | Deque<GroupKey> newBucketChain = new ArrayDeque<>(); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1355 | newBucketChain.addFirst(groupInfo.innerMostGroupDesc().appCookie()); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1356 | // Add L3 interface group to the chain if there is one. |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1357 | if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) { |
| 1358 | newBucketChain.addFirst(groupInfo.nextGroupDesc().appCookie()); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1359 | } |
| 1360 | newBucketChain.addFirst(l3mcastGroupKey); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1361 | addedKeys.add(newBucketChain); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1362 | |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1363 | updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l3mcastGce); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1364 | // Point next group to inner-most group, if any |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1365 | if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) { |
| 1366 | GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc(), |
| 1367 | 1, |
| 1368 | false, |
| 1369 | deviceId); |
| 1370 | updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), innerGce); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1371 | } |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1372 | log.debug("Adding to L3MCAST: device:{} gid:{} group key:{} nextId:{}", |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1373 | deviceId, Integer.toHexString(l3mcastGroupId), |
| 1374 | l3mcastGroupKey, nextObj.id()); |
| 1375 | // send the innermost group |
| 1376 | log.debug("Sending innermost group {} in group chain on device {} ", |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1377 | Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()), |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1378 | deviceId); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1379 | groupService.addGroup(groupInfo.innerMostGroupDesc()); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1380 | |
| 1381 | }); |
| 1382 | |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1383 | updatePendingNextObjective(l3mcastGroupKey, |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1384 | new OfdpaNextGroup(addedKeys, nextObj)); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | /** |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1388 | * Removes buckets in the top level group of a possible group-chain. Does |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1389 | * not remove the groups in the group-chain pointed to by this bucket, as they |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1390 | * may be in use (referenced by other groups) elsewhere. |
| 1391 | * |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1392 | * @param nextObjective a next objective that contains information for the |
| 1393 | * buckets to be removed from the group |
| 1394 | * @param next the representation of the existing group-chains for this next |
| 1395 | * objective, from which the top-level buckets to remove are determined |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1396 | */ |
| 1397 | protected void removeBucketFromGroup(NextObjective nextObjective, NextGroup next) { |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1398 | if (nextObjective.type() != NextObjective.Type.HASHED && |
| 1399 | nextObjective.type() != NextObjective.Type.BROADCAST) { |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1400 | log.warn("RemoveBuckets not applied to nextType:{} in dev:{} for next:{}", |
| 1401 | nextObjective.type(), deviceId, nextObjective.id()); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1402 | fail(nextObjective, ObjectiveError.UNSUPPORTED); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1403 | return; |
| 1404 | } |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1405 | List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data()); |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1406 | List<Integer> indicesToRemove = Lists.newArrayList(); |
| 1407 | for (TrafficTreatment treatment : nextObjective.next()) { |
| 1408 | // find the top-level bucket in the group-chain by matching the |
| 1409 | // outport and label from different groups in the chain |
| 1410 | PortNumber portToRemove = readOutPortFromTreatment(treatment); |
| 1411 | int labelToRemove = readLabelFromTreatment(treatment); |
| 1412 | if (portToRemove == null) { |
| 1413 | log.warn("treatment {} of next objective {} has no outport.. " |
| 1414 | + "cannot remove bucket from group in dev: {}", treatment, |
| 1415 | nextObjective.id(), deviceId); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1416 | continue; |
| 1417 | } |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1418 | List<Integer> existing = existingPortAndLabel(allActiveKeys, |
| 1419 | groupService, deviceId, |
| 1420 | portToRemove, labelToRemove); |
| 1421 | indicesToRemove.addAll(existing); |
| 1422 | |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1423 | } |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1424 | |
| 1425 | List<Deque<GroupKey>> chainsToRemove = Lists.newArrayList(); |
| 1426 | indicesToRemove.forEach(index -> chainsToRemove |
| 1427 | .add(allActiveKeys.get(index))); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1428 | if (chainsToRemove.isEmpty()) { |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1429 | log.warn("Could not find appropriate group-chain for removing bucket" |
| 1430 | + " for next id {} in dev:{}", nextObjective.id(), deviceId); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1431 | fail(nextObjective, ObjectiveError.BADPARAMS); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1432 | return; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1433 | } |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1434 | removeBucket(chainsToRemove, nextObjective); |
| 1435 | } |
| 1436 | |
| 1437 | /** |
| 1438 | * Removes top-level buckets from a group that represents the given next objective. |
| 1439 | * |
| 1440 | * @param chainsToRemove a list of group bucket chains to remove |
| 1441 | * @param nextObjective the next objective that contains information for the |
| 1442 | * buckets to be removed from the group |
| 1443 | */ |
| 1444 | protected void removeBucket(List<Deque<GroupKey>> chainsToRemove, |
| 1445 | NextObjective nextObjective) { |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1446 | List<GroupBucket> bucketsToRemove = Lists.newArrayList(); |
| 1447 | //first group key is the one we want to modify |
| 1448 | GroupKey modGroupKey = chainsToRemove.get(0).peekFirst(); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1449 | Group modGroup = groupService.getGroup(deviceId, modGroupKey); |
Ray Milkey | 4386981 | 2018-05-04 13:00:39 -0700 | [diff] [blame] | 1450 | if (modGroup == null) { |
| 1451 | log.warn("removeBucket(): Attempt to modify non-existent group {} for device {}", modGroupKey, deviceId); |
| 1452 | return; |
| 1453 | } |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1454 | for (Deque<GroupKey> foundChain : chainsToRemove) { |
| 1455 | //second group key is the one we wish to remove the reference to |
| 1456 | if (foundChain.size() < 2) { |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1457 | // additional check to make sure second group key exists in |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1458 | // the chain. |
| 1459 | log.warn("Can't find second group key from chain {}", |
| 1460 | foundChain); |
| 1461 | continue; |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1462 | } |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1463 | GroupKey pointedGroupKey = foundChain.stream() |
| 1464 | .collect(Collectors.toList()).get(1); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1465 | Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1466 | if (pointedGroup == null) { |
| 1467 | continue; |
| 1468 | } |
| 1469 | |
| 1470 | GroupBucket bucket; |
| 1471 | if (nextObjective.type() == NextObjective.Type.HASHED) { |
| 1472 | bucket = DefaultGroupBucket.createSelectGroupBucket( |
| 1473 | DefaultTrafficTreatment.builder() |
| 1474 | .group(pointedGroup.id()) |
| 1475 | .build()); |
| 1476 | } else { |
| 1477 | bucket = DefaultGroupBucket.createAllGroupBucket( |
| 1478 | DefaultTrafficTreatment.builder() |
| 1479 | .group(pointedGroup.id()) |
| 1480 | .build()); |
| 1481 | } |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1482 | bucketsToRemove.add(bucket); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1483 | } |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1484 | |
| 1485 | GroupBuckets removeBuckets = new GroupBuckets(bucketsToRemove); |
| 1486 | List<String> pointedGroupIds; // for debug log |
| 1487 | pointedGroupIds = bucketsToRemove.stream() |
| 1488 | .map(GroupBucket::treatment) |
| 1489 | .map(TrafficTreatment::allInstructions) |
| 1490 | .flatMap(List::stream) |
| 1491 | .filter(inst -> inst instanceof Instructions.GroupInstruction) |
| 1492 | .map(inst -> (Instructions.GroupInstruction) inst) |
| 1493 | .map(Instructions.GroupInstruction::groupId) |
| 1494 | .map(GroupId::id) |
| 1495 | .map(Integer::toHexString) |
| 1496 | .map(id -> HEX_PREFIX + id) |
| 1497 | .collect(Collectors.toList()); |
| 1498 | |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1499 | log.debug("Removing buckets from group id 0x{} pointing to group id(s) {} " |
Ray Milkey | 4386981 | 2018-05-04 13:00:39 -0700 | [diff] [blame] | 1500 | + "for next id {} in device {}", |
| 1501 | Integer.toHexString(modGroup.id().id()), |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1502 | pointedGroupIds, nextObjective.id(), deviceId); |
| 1503 | addPendingUpdateNextObjective(modGroupKey, nextObjective); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1504 | groupService.removeBucketsFromGroup(deviceId, modGroupKey, |
| 1505 | removeBuckets, modGroupKey, |
| 1506 | nextObjective.appId()); |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1507 | // update store - synchronize access as there may be multiple threads |
| 1508 | // trying to remove buckets from the same group, each with its own |
| 1509 | // potentially stale copy of allActiveKeys |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1510 | synchronized (flowObjectiveStore) { |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1511 | // get a fresh copy of what the store holds |
| 1512 | NextGroup next = flowObjectiveStore.getNextGroup(nextObjective.id()); |
| 1513 | List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data()); |
Pier Luigi | d008dbe | 2018-01-15 09:48:49 +0100 | [diff] [blame] | 1514 | allActiveKeys = Lists.newArrayList(allActiveKeys); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1515 | // Note that since we got a new object, and ArrayDeque does not implement |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1516 | // Object.equals(), we have to check the deque elems one by one |
| 1517 | allActiveKeys |
| 1518 | .removeIf(active -> |
| 1519 | chainsToRemove.stream().anyMatch(remove -> |
| 1520 | Arrays.equals(remove.toArray(new GroupKey[0]), |
| 1521 | active.toArray(new GroupKey[0])))); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1522 | // If no buckets in the group, then retain an entry for the |
| 1523 | // top level group which still exists. |
| 1524 | if (allActiveKeys.isEmpty()) { |
| 1525 | ArrayDeque<GroupKey> top = new ArrayDeque<>(); |
| 1526 | top.add(modGroupKey); |
| 1527 | allActiveKeys.add(top); |
| 1528 | } |
| 1529 | flowObjectiveStore.putNextGroup(nextObjective.id(), |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1530 | new OfdpaNextGroup(allActiveKeys, |
| 1531 | nextObjective)); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1532 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1533 | } |
| 1534 | |
| 1535 | /** |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 1536 | * Removes all groups in multiple possible group-chains that represent the next-obj. |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1537 | * |
| 1538 | * @param nextObjective the next objective to remove |
| 1539 | * @param next the NextGroup that represents the existing group-chain for |
| 1540 | * this next objective |
| 1541 | */ |
| 1542 | protected void removeGroup(NextObjective nextObjective, NextGroup next) { |
Andreas Pantelopoulos | 27532cd | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1543 | |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1544 | List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data()); |
Charles Chan | fc5c780 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 1545 | |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1546 | List<GroupKey> groupKeys = allActiveKeys.stream() |
Charles Chan | fc5c780 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 1547 | .map(Deque::getFirst).collect(Collectors.toList()); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1548 | addPendingRemoveNextObjective(nextObjective, groupKeys); |
Charles Chan | fc5c780 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 1549 | |
Andreas Pantelopoulos | 27532cd | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1550 | allActiveKeys |
| 1551 | .forEach(groupChain -> groupChain.forEach(groupKey -> groupService |
| 1552 | .removeGroup(deviceId, groupKey, nextObjective.appId()))); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1553 | flowObjectiveStore.removeNextGroup(nextObjective.id()); |
| 1554 | } |
| 1555 | |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1556 | /** |
Jonghwan Hyun | f810a7a | 2018-02-12 16:43:45 +0900 | [diff] [blame] | 1557 | * Modify buckets in the L2 interface group. |
| 1558 | * |
| 1559 | * @param nextObjective a next objective that contains information for the |
| 1560 | * buckets to be modified in the group |
| 1561 | * @param next the representation of the existing group-chains for this next |
| 1562 | * objective, from which the innermost group buckets to remove are determined |
| 1563 | */ |
| 1564 | protected void modifyBucketFromGroup(NextObjective nextObjective, NextGroup next) { |
| 1565 | if (nextObjective.type() != NextObjective.Type.SIMPLE) { |
| 1566 | log.warn("ModifyBucketFromGroup cannot be applied to nextType:{} in dev:{} for next:{}", |
| 1567 | nextObjective.type(), deviceId, nextObjective.id()); |
| 1568 | fail(nextObjective, ObjectiveError.UNSUPPORTED); |
| 1569 | return; |
| 1570 | } |
| 1571 | |
| 1572 | VlanId assignedVlan = readVlanFromSelector(nextObjective.meta()); |
| 1573 | if (assignedVlan == null) { |
| 1574 | log.warn("VLAN ID required by simple next obj is missing. Abort."); |
| 1575 | fail(nextObjective, ObjectiveError.BADPARAMS); |
| 1576 | return; |
| 1577 | } |
| 1578 | |
| 1579 | List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObjective, assignedVlan); |
| 1580 | |
| 1581 | // There is only one L2 interface group in this case |
| 1582 | GroupDescription l2InterfaceGroupDesc = groupInfos.get(0).innerMostGroupDesc(); |
| 1583 | |
| 1584 | // Replace group bucket for L2 interface group |
| 1585 | groupService.setBucketsForGroup(deviceId, |
| 1586 | l2InterfaceGroupDesc.appCookie(), |
| 1587 | l2InterfaceGroupDesc.buckets(), |
| 1588 | l2InterfaceGroupDesc.appCookie(), |
| 1589 | l2InterfaceGroupDesc.appId()); |
| 1590 | |
| 1591 | // update store - synchronize access as there may be multiple threads |
| 1592 | // trying to remove buckets from the same group, each with its own |
| 1593 | // potentially stale copy of allActiveKeys |
| 1594 | synchronized (flowObjectiveStore) { |
| 1595 | List<Deque<GroupKey>> modifiedGroupKeys = Lists.newArrayList(); |
| 1596 | ArrayDeque<GroupKey> top = new ArrayDeque<>(); |
| 1597 | top.add(l2InterfaceGroupDesc.appCookie()); |
| 1598 | modifiedGroupKeys.add(top); |
| 1599 | |
| 1600 | flowObjectiveStore.putNextGroup(nextObjective.id(), |
| 1601 | new OfdpaNextGroup(modifiedGroupKeys, |
| 1602 | nextObjective)); |
| 1603 | } |
| 1604 | } |
| 1605 | |
| 1606 | /** |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1607 | * Checks existing buckets in {@link NextGroup} to verify if they match |
| 1608 | * the buckets in the given {@link NextObjective}. Adds or removes buckets |
| 1609 | * to ensure that the buckets match up. |
| 1610 | * |
| 1611 | * @param nextObjective the next objective to verify |
| 1612 | * @param next the representation of the existing group which has to be |
| 1613 | * modified to match the given next objective |
| 1614 | */ |
| 1615 | protected void verifyGroup(NextObjective nextObjective, NextGroup next) { |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1616 | if (nextObjective.type() == NextObjective.Type.SIMPLE) { |
| 1617 | log.warn("verification not supported for indirect group"); |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1618 | fail(nextObjective, ObjectiveError.UNSUPPORTED); |
| 1619 | return; |
| 1620 | } |
Charles Chan | 1dd7557 | 2018-05-08 11:49:05 -0700 | [diff] [blame] | 1621 | log.trace("Call to verify device:{} nextId:{}", deviceId, nextObjective.id()); |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1622 | List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data()); |
| 1623 | List<TrafficTreatment> bucketsToCreate = Lists.newArrayList(); |
| 1624 | List<Integer> indicesToRemove = Lists.newArrayList(); |
Pier Luigi | ec6ac42 | 2018-01-29 10:30:59 +0100 | [diff] [blame] | 1625 | |
| 1626 | // Iterating over the treatments of the next objective allows |
| 1627 | // to detect missing buckets and/or duplicate buckets (to be removed) |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1628 | for (TrafficTreatment bkt : nextObjective.next()) { |
| 1629 | PortNumber portNumber = readOutPortFromTreatment(bkt); |
| 1630 | int label = readLabelFromTreatment(bkt); |
| 1631 | if (portNumber == null) { |
| 1632 | log.warn("treatment {} of next objective {} has no outport.. " |
| 1633 | + "cannot remove bucket from group in dev: {}", bkt, |
| 1634 | nextObjective.id(), deviceId); |
| 1635 | fail(nextObjective, ObjectiveError.BADPARAMS); |
| 1636 | return; |
| 1637 | } |
| 1638 | List<Integer> existing = existingPortAndLabel(allActiveKeys, |
| 1639 | groupService, deviceId, |
| 1640 | portNumber, label); |
| 1641 | if (existing.isEmpty()) { |
| 1642 | // if it doesn't exist, mark this bucket for creation |
| 1643 | bucketsToCreate.add(bkt); |
| 1644 | } |
| 1645 | if (existing.size() > 1) { |
| 1646 | // if it exists but there are duplicates, mark the others for removal |
| 1647 | existing.remove(0); |
| 1648 | indicesToRemove.addAll(existing); |
| 1649 | } |
| 1650 | } |
| 1651 | |
Pier Luigi | ec6ac42 | 2018-01-29 10:30:59 +0100 | [diff] [blame] | 1652 | // Detect situation where the next data has more buckets |
| 1653 | // (not duplicates) respect to the next objective |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 1654 | if (allActiveKeys.size() > nextObjective.next().size() && |
| 1655 | // ignore specific case of empty group |
| 1656 | !(nextObjective.next().size() == 0 && allActiveKeys.size() == 1 |
| 1657 | && allActiveKeys.get(0).size() == 1)) { |
Pier Luigi | ec6ac42 | 2018-01-29 10:30:59 +0100 | [diff] [blame] | 1658 | log.warn("Mismatch detected between next and flowobjstore for device {}: " + |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 1659 | "nextId:{}, nextObjective-size:{} next-size:{} .. correcting", |
| 1660 | deviceId, nextObjective.id(), nextObjective.next().size(), |
| 1661 | allActiveKeys.size()); |
| 1662 | List<Integer> otherIndices = |
| 1663 | indicesToRemoveFromNextGroup(allActiveKeys, nextObjective, |
| 1664 | groupService, deviceId); |
Pier Luigi | ec6ac42 | 2018-01-29 10:30:59 +0100 | [diff] [blame] | 1665 | // Filter out the indices not present |
| 1666 | otherIndices = otherIndices.stream() |
| 1667 | .filter(index -> !indicesToRemove.contains(index)) |
| 1668 | .collect(Collectors.toList()); |
| 1669 | // Add all to the final list |
| 1670 | indicesToRemove.addAll(otherIndices); |
| 1671 | } |
| 1672 | |
Charles Chan | 1dd7557 | 2018-05-08 11:49:05 -0700 | [diff] [blame] | 1673 | log.trace("Buckets to create {}", bucketsToCreate); |
| 1674 | log.trace("Indices to remove {}", indicesToRemove); |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1675 | |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1676 | if (!bucketsToCreate.isEmpty()) { |
| 1677 | log.info("creating {} buckets as part of nextId: {} verification", |
| 1678 | bucketsToCreate.size(), nextObjective.id()); |
| 1679 | //create a nextObjective only with these buckets |
| 1680 | NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder() |
| 1681 | .withId(nextObjective.id()) |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1682 | .withType(nextObjective.type()) |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1683 | .withMeta(nextObjective.meta()) |
| 1684 | .fromApp(nextObjective.appId()); |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1685 | bucketsToCreate.forEach(nextObjBuilder::addTreatment); |
| 1686 | // According to the next type we call the proper add function |
| 1687 | if (nextObjective.type() == NextObjective.Type.HASHED) { |
| 1688 | addBucketToHashGroup(nextObjBuilder.addToExisting(), allActiveKeys); |
| 1689 | } else { |
| 1690 | addBucketToBroadcastGroup(nextObjBuilder.addToExisting(), allActiveKeys); |
| 1691 | } |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1692 | } |
| 1693 | |
| 1694 | if (!indicesToRemove.isEmpty()) { |
| 1695 | log.info("removing {} buckets as part of nextId: {} verification", |
| 1696 | indicesToRemove.size(), nextObjective.id()); |
| 1697 | List<Deque<GroupKey>> chainsToRemove = Lists.newArrayList(); |
| 1698 | indicesToRemove.forEach(index -> chainsToRemove |
| 1699 | .add(allActiveKeys.get(index))); |
| 1700 | removeBucket(chainsToRemove, nextObjective); |
| 1701 | } |
| 1702 | |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 1703 | if (bucketsToCreate.isEmpty() && indicesToRemove.isEmpty()) { |
| 1704 | // flowObjective store record is in-sync with nextObjective passed-in |
| 1705 | // Nevertheless groupStore may not be in sync due to bug in the store |
| 1706 | // - see CORD-1844. XXX When this bug is fixed, the rest of this verify |
| 1707 | // method will not be required. |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1708 | GroupKey topGroupKey = allActiveKeys.get(0).peekFirst(); |
| 1709 | Group topGroup = groupService.getGroup(deviceId, topGroupKey); |
| 1710 | int actualGroupSize = topGroup.buckets().buckets().size(); |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 1711 | int objGroupSize = nextObjective.next().size(); |
| 1712 | if (actualGroupSize != objGroupSize) { |
| 1713 | log.warn("Mismatch detected in device:{}, nextId:{}, nextObjective-size" |
| 1714 | + ":{} group-size:{} .. correcting", deviceId, nextObjective.id(), |
| 1715 | objGroupSize, actualGroupSize); |
| 1716 | } |
| 1717 | if (actualGroupSize > objGroupSize) { |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1718 | // Group in the device has more chains |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 1719 | List<GroupBucket> bucketsToRemove = Lists.newArrayList(); |
| 1720 | //check every bucket in the actual group |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1721 | for (GroupBucket bucket : topGroup.buckets().buckets()) { |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 1722 | GroupInstruction g = (GroupInstruction) bucket.treatment() |
| 1723 | .allInstructions().iterator().next(); |
| 1724 | GroupId gidToCheck = g.groupId(); // the group pointed to |
| 1725 | boolean matches = false; |
| 1726 | for (Deque<GroupKey> validChain : allActiveKeys) { |
| 1727 | if (validChain.size() < 2) { |
| 1728 | continue; |
| 1729 | } |
| 1730 | GroupKey pointedGroupKey = validChain.stream() |
| 1731 | .collect(Collectors.toList()).get(1); |
| 1732 | Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey); |
| 1733 | if (pointedGroup != null && gidToCheck.equals(pointedGroup.id())) { |
| 1734 | matches = true; |
| 1735 | break; |
| 1736 | } |
| 1737 | } |
| 1738 | if (!matches) { |
| 1739 | log.warn("Removing bucket pointing to groupId:{}", gidToCheck); |
| 1740 | bucketsToRemove.add(bucket); |
| 1741 | } |
| 1742 | } |
| 1743 | // remove buckets for which there was no record in the obj store |
| 1744 | if (bucketsToRemove.isEmpty()) { |
| 1745 | log.warn("Mismatch detected but could not determine which" |
| 1746 | + "buckets to remove"); |
| 1747 | } else { |
| 1748 | GroupBuckets removeBuckets = new GroupBuckets(bucketsToRemove); |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1749 | groupService.removeBucketsFromGroup(deviceId, topGroupKey, |
| 1750 | removeBuckets, topGroupKey, |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 1751 | nextObjective.appId()); |
| 1752 | } |
| 1753 | } else if (actualGroupSize < objGroupSize) { |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1754 | // Group in the device has less chains |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 1755 | // should also add buckets not in group-store but in obj-store |
| 1756 | List<GroupBucket> bucketsToAdd = Lists.newArrayList(); |
| 1757 | //check every bucket in the obj |
| 1758 | for (Deque<GroupKey> validChain : allActiveKeys) { |
| 1759 | if (validChain.size() < 2) { |
| 1760 | continue; |
| 1761 | } |
| 1762 | GroupKey pointedGroupKey = validChain.stream() |
| 1763 | .collect(Collectors.toList()).get(1); |
| 1764 | Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey); |
| 1765 | if (pointedGroup == null) { |
| 1766 | // group should exist, otherwise cannot be added as bucket |
| 1767 | continue; |
| 1768 | } |
| 1769 | boolean matches = false; |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1770 | for (GroupBucket bucket : topGroup.buckets().buckets()) { |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 1771 | GroupInstruction g = (GroupInstruction) bucket.treatment() |
| 1772 | .allInstructions().iterator().next(); |
| 1773 | GroupId gidToCheck = g.groupId(); // the group pointed to |
| 1774 | if (pointedGroup.id().equals(gidToCheck)) { |
| 1775 | matches = true; |
| 1776 | break; |
| 1777 | } |
| 1778 | } |
| 1779 | if (!matches) { |
| 1780 | log.warn("Adding bucket pointing to groupId:{}", pointedGroup); |
| 1781 | TrafficTreatment t = DefaultTrafficTreatment.builder() |
| 1782 | .group(pointedGroup.id()) |
| 1783 | .build(); |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1784 | // Create the proper bucket according to the next type |
| 1785 | if (nextObjective.type() == NextObjective.Type.HASHED) { |
| 1786 | bucketsToAdd.add(DefaultGroupBucket.createSelectGroupBucket(t)); |
| 1787 | } else { |
| 1788 | bucketsToAdd.add(DefaultGroupBucket.createAllGroupBucket(t)); |
| 1789 | } |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 1790 | } |
| 1791 | } |
| 1792 | if (bucketsToAdd.isEmpty()) { |
| 1793 | log.warn("Mismatch detected but could not determine which " |
| 1794 | + "buckets to add"); |
| 1795 | } else { |
| 1796 | GroupBuckets addBuckets = new GroupBuckets(bucketsToAdd); |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1797 | groupService.addBucketsToGroup(deviceId, topGroupKey, |
| 1798 | addBuckets, topGroupKey, |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 1799 | nextObjective.appId()); |
| 1800 | } |
| 1801 | } |
| 1802 | } |
| 1803 | |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1804 | pass(nextObjective); |
| 1805 | } |
| 1806 | |
| 1807 | ////////////////////////////////////// |
| 1808 | // Helper methods and classes |
| 1809 | ////////////////////////////////////// |
| 1810 | |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1811 | protected void updatePendingNextObjective(GroupKey groupKey, OfdpaNextGroup nextGrp) { |
| 1812 | pendingAddNextObjectives.asMap().compute(groupKey, (k, val) -> { |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 1813 | if (val == null) { |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1814 | val = new CopyOnWriteArrayList<>(); |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 1815 | } |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1816 | val.add(nextGrp); |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 1817 | return val; |
| 1818 | }); |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1819 | } |
| 1820 | |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1821 | protected void updatePendingGroups(GroupKey groupKey, GroupChainElem gce) { |
| 1822 | pendingGroups.asMap().compute(groupKey, (k, val) -> { |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 1823 | if (val == null) { |
| 1824 | val = Sets.newConcurrentHashSet(); |
| 1825 | } |
| 1826 | val.add(gce); |
| 1827 | return val; |
| 1828 | }); |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1829 | } |
| 1830 | |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1831 | protected void addPendingUpdateNextObjective(GroupKey groupKey, |
| 1832 | NextObjective nextObjective) { |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1833 | pendingUpdateNextObjectives.compute(groupKey, (gKey, nextObjs) -> { |
| 1834 | if (nextObjs != null) { |
| 1835 | nextObjs.add(nextObjective); |
| 1836 | } else { |
| 1837 | nextObjs = Sets.newHashSet(nextObjective); |
| 1838 | } |
| 1839 | return nextObjs; |
| 1840 | }); |
| 1841 | } |
| 1842 | |
Saurav Das | 2f2c9d0 | 2018-04-07 16:51:09 -0700 | [diff] [blame] | 1843 | protected void addPendingRemoveNextObjective(NextObjective nextObjective, |
| 1844 | List<GroupKey> groupKeys) { |
| 1845 | pendingRemoveNextObjectives.put(nextObjective, groupKeys); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1846 | } |
| 1847 | |
Saurav Das | 2f2c9d0 | 2018-04-07 16:51:09 -0700 | [diff] [blame] | 1848 | protected int getNextAvailableIndex() { |
| 1849 | return (int) nextIndex.incrementAndGet(); |
| 1850 | } |
| 1851 | |
| 1852 | protected void processPendingUpdateNextObjs(GroupKey groupKey) { |
| 1853 | Set<NextObjective> nextObjs = pendingUpdateNextObjectives.remove(groupKey); |
| 1854 | if (nextObjs != null) { |
| 1855 | nextObjs.forEach(nextObj -> { |
| 1856 | log.debug("Group {} updated, update pending next objective {}.", |
| 1857 | groupKey, nextObj); |
| 1858 | pass(nextObj); |
| 1859 | }); |
| 1860 | } |
| 1861 | } |
| 1862 | |
| 1863 | protected void processPendingRemoveNextObjs(GroupKey key) { |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1864 | pendingRemoveNextObjectives.asMap().forEach((nextObjective, groupKeys) -> { |
jaegonkim | 2e4d99e | 2018-07-09 23:23:10 +0900 | [diff] [blame] | 1865 | groupKeys.remove(key); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1866 | if (groupKeys.isEmpty()) { |
| 1867 | pendingRemoveNextObjectives.invalidate(nextObjective); |
| 1868 | pass(nextObjective); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1869 | } |
| 1870 | }); |
| 1871 | } |
| 1872 | |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1873 | protected void processPendingAddGroupsOrNextObjs(GroupKey key, boolean added) { |
| 1874 | //first check for group chain |
| 1875 | Set<OfdpaGroupHandlerUtility.GroupChainElem> gceSet = pendingGroups.asMap().remove(key); |
| 1876 | if (gceSet != null) { |
| 1877 | for (GroupChainElem gce : gceSet) { |
| 1878 | log.debug("Group service {} group key {} in device {}. " |
| 1879 | + "Processing next group in group chain with group id 0x{}", |
| 1880 | (added) ? "ADDED" : "processed", |
| 1881 | key, deviceId, |
| 1882 | Integer.toHexString(gce.groupDescription().givenGroupId())); |
| 1883 | processGroupChain(gce); |
| 1884 | } |
| 1885 | } else { |
| 1886 | // otherwise chain complete - check for waiting nextObjectives |
| 1887 | List<OfdpaGroupHandlerUtility.OfdpaNextGroup> nextGrpList = |
| 1888 | pendingAddNextObjectives.getIfPresent(key); |
| 1889 | if (nextGrpList != null) { |
| 1890 | pendingAddNextObjectives.invalidate(key); |
| 1891 | nextGrpList.forEach(nextGrp -> { |
| 1892 | log.debug("Group service {} group key {} in device:{}. " |
| 1893 | + "Done implementing next objective: {} <<-->> gid:0x{}", |
| 1894 | (added) ? "ADDED" : "processed", |
| 1895 | key, deviceId, nextGrp.nextObjective().id(), |
| 1896 | Integer.toHexString(groupService.getGroup(deviceId, key) |
| 1897 | .givenGroupId())); |
| 1898 | pass(nextGrp.nextObjective()); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1899 | updateFlowObjectiveStore(nextGrp.nextObjective().id(), nextGrp); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1900 | |
| 1901 | // check if addBuckets waiting for this completion |
| 1902 | pendingBuckets.compute(nextGrp.nextObjective().id(), (nextId, pendBkts) -> { |
| 1903 | if (pendBkts != null) { |
| 1904 | pendBkts.forEach(pendBkt -> addBucketToGroup(pendBkt, nextGrp)); |
| 1905 | } |
| 1906 | return null; |
| 1907 | }); |
| 1908 | }); |
| 1909 | } |
| 1910 | } |
| 1911 | } |
| 1912 | |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1913 | /** |
| 1914 | * Processes next element of a group chain. Assumption is that if this |
| 1915 | * group points to another group, the latter has already been created |
| 1916 | * and this driver has received notification for it. A second assumption is |
| 1917 | * that if there is another group waiting for this group then the appropriate |
| 1918 | * stores already have the information to act upon the notification for the |
| 1919 | * creation of this group. |
| 1920 | * <p> |
| 1921 | * The processing of the GroupChainElement depends on the number of groups |
| 1922 | * this element is waiting on. For all group types other than SIMPLE, a |
| 1923 | * GroupChainElement could be waiting on multiple groups. |
| 1924 | * |
| 1925 | * @param gce the group chain element to be processed next |
| 1926 | */ |
| 1927 | private void processGroupChain(GroupChainElem gce) { |
| 1928 | int waitOnGroups = gce.decrementAndGetGroupsWaitedOn(); |
| 1929 | if (waitOnGroups != 0) { |
| 1930 | log.debug("GCE: {} not ready to be processed", gce); |
| 1931 | return; |
| 1932 | } |
| 1933 | log.debug("GCE: {} ready to be processed", gce); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1934 | if (gce.addBucketToGroup()) { |
| 1935 | groupService.addBucketsToGroup(gce.groupDescription().deviceId(), |
| 1936 | gce.groupDescription().appCookie(), |
| 1937 | gce.groupDescription().buckets(), |
| 1938 | gce.groupDescription().appCookie(), |
| 1939 | gce.groupDescription().appId()); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1940 | } else { |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1941 | groupService.addGroup(gce.groupDescription()); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1942 | } |
| 1943 | } |
| 1944 | |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1945 | private void updateFlowObjectiveStore(Integer nextId, OfdpaNextGroup nextGrp) { |
| 1946 | synchronized (flowObjectiveStore) { |
| 1947 | // get fresh copy of what the store holds |
| 1948 | NextGroup next = flowObjectiveStore.getNextGroup(nextId); |
| 1949 | if (next == null || nextGrp.nextObjective().op() == Operation.ADD) { |
| 1950 | flowObjectiveStore.putNextGroup(nextId, nextGrp); |
| 1951 | return; |
| 1952 | } |
| 1953 | if (nextGrp.nextObjective().op() == Operation.ADD_TO_EXISTING) { |
| 1954 | List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data()); |
Pier Luigi | d008dbe | 2018-01-15 09:48:49 +0100 | [diff] [blame] | 1955 | allActiveKeys = Lists.newArrayList(allActiveKeys); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1956 | // If active keys shows only the top-level group without a chain of groups, |
| 1957 | // then it represents an empty group. Update by replacing empty chain. |
| 1958 | if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) { |
| 1959 | allActiveKeys.clear(); |
| 1960 | } |
| 1961 | allActiveKeys.addAll(nextGrp.allKeys()); |
| 1962 | flowObjectiveStore.putNextGroup(nextId, |
| 1963 | new OfdpaNextGroup(allActiveKeys, nextGrp.nextObjective())); |
| 1964 | } |
| 1965 | } |
| 1966 | } |
| 1967 | |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1968 | private class InnerGroupListener implements GroupListener { |
| 1969 | @Override |
| 1970 | public void event(GroupEvent event) { |
Charles Chan | fc5c780 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 1971 | switch (event.type()) { |
| 1972 | case GROUP_ADDED: |
| 1973 | processPendingAddGroupsOrNextObjs(event.subject().appCookie(), true); |
| 1974 | break; |
| 1975 | case GROUP_REMOVED: |
| 1976 | processPendingRemoveNextObjs(event.subject().appCookie()); |
| 1977 | break; |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1978 | case GROUP_UPDATED: |
| 1979 | processPendingUpdateNextObjs(event.subject().appCookie()); |
| 1980 | break; |
Charles Chan | fc5c780 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 1981 | default: |
| 1982 | break; |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1983 | } |
| 1984 | } |
| 1985 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1986 | } |