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