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