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