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