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