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 |
| 498 | * L2 Flood group whose buckets point to L2 Interface groups. |
| 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) { |
| 908 | if (nextObjective.type() != NextObjective.Type.HASHED) { |
| 909 | log.warn("AddBuckets not applied to nextType:{} in dev:{} for next:{}", |
| 910 | nextObjective.type(), deviceId, nextObjective.id()); |
| 911 | return; |
| 912 | } |
| 913 | if (nextObjective.next().size() > 1) { |
| 914 | log.warn("Only one bucket can be added at a time"); |
| 915 | return; |
| 916 | } |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 917 | // first check to see if bucket being added is not a duplicate of an |
| 918 | // existing bucket. If it is for an existing outport, then its a duplicate. |
| 919 | Set<PortNumber> existingOutPorts = new HashSet<>(); |
| 920 | List<Deque<GroupKey>> allActiveKeys = Ofdpa2Pipeline.appKryo.deserialize(next.data()); |
| 921 | for (Deque<GroupKey> gkeys : allActiveKeys) { |
| 922 | // get the last group for the outport |
| 923 | Group glast = groupService.getGroup(deviceId, gkeys.peekLast()); |
| 924 | if (glast != null && !glast.buckets().buckets().isEmpty()) { |
| 925 | PortNumber op = readOutPortFromTreatment( |
| 926 | glast.buckets().buckets().get(0).treatment()); |
| 927 | if (op != null) { |
| 928 | existingOutPorts.add(op); |
| 929 | } |
| 930 | } |
| 931 | } |
| 932 | // only a single bucket being added |
| 933 | TrafficTreatment tt = nextObjective.next().iterator().next(); |
| 934 | PortNumber newport = readOutPortFromTreatment(tt); |
| 935 | if (existingOutPorts.contains(newport)) { |
Charles Chan | e5ccd58 | 2016-11-10 14:16:54 -0800 | [diff] [blame] | 936 | 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] | 937 | newport, deviceId, nextObjective.id()); |
| 938 | return; |
| 939 | } |
| 940 | |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 941 | // storage for all group keys in the chain of groups created |
| 942 | List<Deque<GroupKey>> allGroupKeys = new ArrayList<>(); |
| 943 | List<GroupInfo> unsentGroups = new ArrayList<>(); |
| 944 | createHashBucketChains(nextObjective, allGroupKeys, unsentGroups); |
| 945 | |
| 946 | // now we can create the outermost L3 ECMP group bucket to add |
| 947 | GroupInfo gi = unsentGroups.get(0); // only one bucket, so only one group-chain |
| 948 | TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder(); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 949 | ttb.group(new DefaultGroupId(gi.nextGroupDesc.givenGroupId())); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 950 | GroupBucket sbucket = DefaultGroupBucket.createSelectGroupBucket(ttb.build()); |
| 951 | |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 952 | // retrieve the original L3 ECMP group id and description from the first |
| 953 | // element in any deque. |
| 954 | GroupKey l3ecmpGroupKey = null; |
| 955 | if (!allActiveKeys.isEmpty()) { |
| 956 | l3ecmpGroupKey = allActiveKeys.get(0).peekFirst(); |
| 957 | } else { |
| 958 | log.warn("Could not determine top level group while trying to" |
| 959 | + "add bucket for port:{} in dev:{} for next:{}", |
| 960 | deviceId, nextObjective.id(), newport); |
| 961 | return; |
| 962 | } |
| 963 | Group l3ecmpGroup = groupService.getGroup(deviceId, l3ecmpGroupKey); |
| 964 | if (l3ecmpGroup == null) { |
| 965 | log.warn("Could not find l3 ecmp group while trying to add bucket" |
| 966 | + "for port:{} in dev:{} for next:{}", deviceId, |
| 967 | nextObjective.id(), newport); |
| 968 | return; |
| 969 | } |
| 970 | int l3ecmpGroupId = l3ecmpGroup.id().id(); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 971 | |
| 972 | // Although GroupDescriptions are not necessary for adding buckets to |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 973 | // 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] | 974 | // processed, the info will be extracted for the bucketAdd call to groupService |
| 975 | GroupDescription l3ecmpGroupDesc = |
| 976 | new DefaultGroupDescription( |
| 977 | deviceId, |
| 978 | GroupDescription.Type.SELECT, |
| 979 | new GroupBuckets(Collections.singletonList(sbucket)), |
| 980 | l3ecmpGroupKey, |
| 981 | l3ecmpGroupId, |
| 982 | nextObjective.appId()); |
| 983 | GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc, 1, true); |
| 984 | |
| 985 | // update original NextGroup with new bucket-chain |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 986 | // If active keys shows only the top-level group without a chain of groups, |
| 987 | // then it represents an empty group. Update by replacing empty chain. |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 988 | Deque<GroupKey> newBucketChain = allGroupKeys.get(0); |
| 989 | newBucketChain.addFirst(l3ecmpGroupKey); |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 990 | if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) { |
| 991 | allActiveKeys.clear(); |
| 992 | } |
| 993 | allActiveKeys.add(newBucketChain); |
| 994 | updatePendingNextObjective(l3ecmpGroupKey, |
| 995 | new OfdpaNextGroup(allActiveKeys, nextObjective)); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 996 | log.debug("Adding to L3ECMP: device:{} gid:{} gkey:{} nextId:{}", |
| 997 | deviceId, Integer.toHexString(l3ecmpGroupId), |
| 998 | l3ecmpGroupKey, nextObjective.id()); |
| 999 | // send the innermost group |
| 1000 | log.debug("Sending innermost group {} in group chain on device {} ", |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 1001 | Integer.toHexString(gi.innerMostGroupDesc.givenGroupId()), deviceId); |
| 1002 | updatePendingGroups(gi.nextGroupDesc.appCookie(), l3ecmpGce); |
| 1003 | groupService.addGroup(gi.innerMostGroupDesc); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1004 | |
| 1005 | } |
| 1006 | |
| 1007 | /** |
| 1008 | * Removes the bucket in the top level group of a possible group-chain. Does |
| 1009 | * not remove the groups in a group-chain pointed to by this bucket, as they |
| 1010 | * may be in use (referenced by other groups) elsewhere. |
| 1011 | * |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1012 | * @param nextObjective the bucket information for a next group |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1013 | * @param next the representation of the existing group-chain for this next objective |
| 1014 | */ |
| 1015 | protected void removeBucketFromGroup(NextObjective nextObjective, NextGroup next) { |
| 1016 | if (nextObjective.type() != NextObjective.Type.HASHED) { |
| 1017 | log.warn("RemoveBuckets not applied to nextType:{} in dev:{} for next:{}", |
| 1018 | nextObjective.type(), deviceId, nextObjective.id()); |
| 1019 | return; |
| 1020 | } |
| 1021 | Collection<TrafficTreatment> treatments = nextObjective.next(); |
| 1022 | TrafficTreatment treatment = treatments.iterator().next(); |
| 1023 | // find the bucket to remove by noting the outport, and figuring out the |
| 1024 | // top-level group in the group-chain that indirectly references the port |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1025 | PortNumber portToRemove = readOutPortFromTreatment(treatment); |
| 1026 | if (portToRemove == null) { |
| 1027 | log.warn("next objective {} has no outport.. cannot remove bucket" |
| 1028 | + "from group in dev: {}", nextObjective.id(), deviceId); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1029 | return; |
| 1030 | } |
| 1031 | |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1032 | List<Deque<GroupKey>> allActiveKeys = Ofdpa2Pipeline.appKryo.deserialize(next.data()); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1033 | Deque<GroupKey> foundChain = null; |
| 1034 | int index = 0; |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1035 | for (Deque<GroupKey> gkeys : allActiveKeys) { |
| 1036 | // 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] | 1037 | GroupKey groupWithPort = gkeys.peekLast(); |
| 1038 | Group group = groupService.getGroup(deviceId, groupWithPort); |
| 1039 | if (group == null) { |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1040 | log.warn("Inconsistent group chain found when removing bucket" |
| 1041 | + "for next:{} in dev:{}", nextObjective.id(), deviceId); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1042 | continue; |
| 1043 | } |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1044 | PortNumber pout = readOutPortFromTreatment( |
| 1045 | group.buckets().buckets().get(0).treatment()); |
| 1046 | if (pout.equals(portToRemove)) { |
| 1047 | foundChain = gkeys; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1048 | break; |
| 1049 | } |
| 1050 | index++; |
| 1051 | } |
| 1052 | if (foundChain != null) { |
| 1053 | //first groupkey is the one we want to modify |
| 1054 | GroupKey modGroupKey = foundChain.peekFirst(); |
| 1055 | Group modGroup = groupService.getGroup(deviceId, modGroupKey); |
| 1056 | //second groupkey is the one we wish to remove the reference to |
| 1057 | GroupKey pointedGroupKey = null; |
| 1058 | int i = 0; |
| 1059 | for (GroupKey gk : foundChain) { |
| 1060 | if (i++ == 1) { |
| 1061 | pointedGroupKey = gk; |
| 1062 | break; |
| 1063 | } |
| 1064 | } |
| 1065 | Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey); |
| 1066 | GroupBucket bucket = DefaultGroupBucket.createSelectGroupBucket( |
| 1067 | DefaultTrafficTreatment.builder() |
| 1068 | .group(pointedGroup.id()) |
| 1069 | .build()); |
| 1070 | GroupBuckets removeBuckets = new GroupBuckets(Collections |
| 1071 | .singletonList(bucket)); |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1072 | log.debug("Removing buckets from group id 0x{} pointing to group id 0x{}" |
| 1073 | + "for next id {} in device {}", Integer.toHexString(modGroup.id().id()), |
| 1074 | Integer.toHexString(pointedGroup.id().id()), nextObjective.id(), deviceId); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1075 | groupService.removeBucketsFromGroup(deviceId, modGroupKey, |
| 1076 | removeBuckets, modGroupKey, |
| 1077 | nextObjective.appId()); |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1078 | // update store |
| 1079 | // If the bucket removed was the last bucket in the group, then |
| 1080 | // retain an entry for the top level group which still exists. |
| 1081 | if (allActiveKeys.size() == 1) { |
| 1082 | ArrayDeque<GroupKey> top = new ArrayDeque<>(); |
| 1083 | top.add(modGroupKey); |
| 1084 | allActiveKeys.add(top); |
| 1085 | } |
| 1086 | allActiveKeys.remove(index); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1087 | flowObjectiveStore.putNextGroup(nextObjective.id(), |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1088 | new OfdpaNextGroup(allActiveKeys, |
| 1089 | nextObjective)); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1090 | } else { |
| 1091 | log.warn("Could not find appropriate group-chain for removing bucket" |
| 1092 | + " for next id {} in dev:{}", nextObjective.id(), deviceId); |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | /** |
| 1097 | * Removes all groups in multiple possible group-chains that represent the next |
| 1098 | * objective. |
| 1099 | * |
| 1100 | * @param nextObjective the next objective to remove |
| 1101 | * @param next the NextGroup that represents the existing group-chain for |
| 1102 | * this next objective |
| 1103 | */ |
| 1104 | protected void removeGroup(NextObjective nextObjective, NextGroup next) { |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1105 | List<Deque<GroupKey>> allActiveKeys = Ofdpa2Pipeline.appKryo.deserialize(next.data()); |
Charles Chan | fc5c780 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 1106 | |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1107 | List<GroupKey> groupKeys = allActiveKeys.stream() |
Charles Chan | fc5c780 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 1108 | .map(Deque::getFirst).collect(Collectors.toList()); |
| 1109 | pendingRemoveNextObjectives.put(nextObjective, groupKeys); |
| 1110 | |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1111 | allActiveKeys.forEach(groupChain -> groupChain.forEach(groupKey -> |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1112 | groupService.removeGroup(deviceId, groupKey, nextObjective.appId()))); |
| 1113 | flowObjectiveStore.removeNextGroup(nextObjective.id()); |
| 1114 | } |
| 1115 | |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1116 | ////////////////////////////////////// |
| 1117 | // Helper Methods and Classes |
| 1118 | ////////////////////////////////////// |
| 1119 | |
| 1120 | private void updatePendingNextObjective(GroupKey key, OfdpaNextGroup value) { |
| 1121 | List<OfdpaNextGroup> nextList = new CopyOnWriteArrayList<OfdpaNextGroup>(); |
| 1122 | nextList.add(value); |
Charles Chan | fc5c780 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 1123 | List<OfdpaNextGroup> ret = pendingAddNextObjectives.asMap() |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1124 | .putIfAbsent(key, nextList); |
| 1125 | if (ret != null) { |
| 1126 | ret.add(value); |
| 1127 | } |
| 1128 | } |
| 1129 | |
Charles Chan | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 1130 | protected void updatePendingGroups(GroupKey gkey, GroupChainElem gce) { |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1131 | Set<GroupChainElem> gceSet = Collections.newSetFromMap( |
| 1132 | new ConcurrentHashMap<GroupChainElem, Boolean>()); |
| 1133 | gceSet.add(gce); |
| 1134 | Set<GroupChainElem> retval = pendingGroups.putIfAbsent(gkey, gceSet); |
| 1135 | if (retval != null) { |
| 1136 | retval.add(gce); |
| 1137 | } |
| 1138 | } |
| 1139 | |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1140 | /** |
| 1141 | * Processes next element of a group chain. Assumption is that if this |
| 1142 | * group points to another group, the latter has already been created |
| 1143 | * and this driver has received notification for it. A second assumption is |
| 1144 | * that if there is another group waiting for this group then the appropriate |
| 1145 | * stores already have the information to act upon the notification for the |
| 1146 | * creation of this group. |
| 1147 | * <p> |
| 1148 | * The processing of the GroupChainElement depends on the number of groups |
| 1149 | * this element is waiting on. For all group types other than SIMPLE, a |
| 1150 | * GroupChainElement could be waiting on multiple groups. |
| 1151 | * |
| 1152 | * @param gce the group chain element to be processed next |
| 1153 | */ |
| 1154 | private void processGroupChain(GroupChainElem gce) { |
| 1155 | int waitOnGroups = gce.decrementAndGetGroupsWaitedOn(); |
| 1156 | if (waitOnGroups != 0) { |
| 1157 | log.debug("GCE: {} not ready to be processed", gce); |
| 1158 | return; |
| 1159 | } |
| 1160 | log.debug("GCE: {} ready to be processed", gce); |
| 1161 | if (gce.addBucketToGroup) { |
| 1162 | groupService.addBucketsToGroup(gce.groupDescription.deviceId(), |
| 1163 | gce.groupDescription.appCookie(), |
| 1164 | gce.groupDescription.buckets(), |
| 1165 | gce.groupDescription.appCookie(), |
| 1166 | gce.groupDescription.appId()); |
| 1167 | } else { |
| 1168 | groupService.addGroup(gce.groupDescription); |
| 1169 | } |
| 1170 | } |
| 1171 | |
| 1172 | private class GroupChecker implements Runnable { |
| 1173 | @Override |
| 1174 | public void run() { |
| 1175 | Set<GroupKey> keys = pendingGroups.keySet().stream() |
| 1176 | .filter(key -> groupService.getGroup(deviceId, key) != null) |
| 1177 | .collect(Collectors.toSet()); |
Charles Chan | fc5c780 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 1178 | Set<GroupKey> otherkeys = pendingAddNextObjectives.asMap().keySet().stream() |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1179 | .filter(otherkey -> groupService.getGroup(deviceId, otherkey) != null) |
| 1180 | .collect(Collectors.toSet()); |
| 1181 | keys.addAll(otherkeys); |
| 1182 | |
Sho SHIMIZU | a09e1bb | 2016-08-01 14:25:25 -0700 | [diff] [blame] | 1183 | keys.forEach(key -> |
Charles Chan | fc5c780 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 1184 | processPendingAddGroupsOrNextObjs(key, false)); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1185 | } |
| 1186 | } |
| 1187 | |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1188 | private class InnerGroupListener implements GroupListener { |
| 1189 | @Override |
| 1190 | public void event(GroupEvent event) { |
| 1191 | log.trace("received group event of type {}", event.type()); |
Charles Chan | fc5c780 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 1192 | switch (event.type()) { |
| 1193 | case GROUP_ADDED: |
| 1194 | processPendingAddGroupsOrNextObjs(event.subject().appCookie(), true); |
| 1195 | break; |
| 1196 | case GROUP_REMOVED: |
| 1197 | processPendingRemoveNextObjs(event.subject().appCookie()); |
| 1198 | break; |
| 1199 | default: |
| 1200 | break; |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1201 | } |
| 1202 | } |
| 1203 | } |
| 1204 | |
Charles Chan | fc5c780 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 1205 | private void processPendingAddGroupsOrNextObjs(GroupKey key, boolean added) { |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1206 | //first check for group chain |
| 1207 | Set<GroupChainElem> gceSet = pendingGroups.remove(key); |
| 1208 | if (gceSet != null) { |
| 1209 | for (GroupChainElem gce : gceSet) { |
Charles Chan | 216e3c8 | 2016-04-23 14:48:16 -0700 | [diff] [blame] | 1210 | log.debug("Group service {} group key {} in device {}. " |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 1211 | + "Processing next group in group chain with group id 0x{}", |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1212 | (added) ? "ADDED" : "processed", |
| 1213 | key, deviceId, |
| 1214 | Integer.toHexString(gce.groupDescription.givenGroupId())); |
| 1215 | processGroupChain(gce); |
| 1216 | } |
| 1217 | } else { |
| 1218 | // otherwise chain complete - check for waiting nextObjectives |
Charles Chan | fc5c780 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 1219 | List<OfdpaNextGroup> nextGrpList = pendingAddNextObjectives.getIfPresent(key); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1220 | if (nextGrpList != null) { |
Charles Chan | fc5c780 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 1221 | pendingAddNextObjectives.invalidate(key); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1222 | nextGrpList.forEach(nextGrp -> { |
Charles Chan | 216e3c8 | 2016-04-23 14:48:16 -0700 | [diff] [blame] | 1223 | log.debug("Group service {} group key {} in device:{}. " |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 1224 | + "Done implementing next objective: {} <<-->> gid:0x{}", |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1225 | (added) ? "ADDED" : "processed", |
| 1226 | key, deviceId, nextGrp.nextObjective().id(), |
| 1227 | Integer.toHexString(groupService.getGroup(deviceId, key) |
| 1228 | .givenGroupId())); |
Charles Chan | 361154b | 2016-03-24 10:23:39 -0700 | [diff] [blame] | 1229 | Ofdpa2Pipeline.pass(nextGrp.nextObjective()); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1230 | flowObjectiveStore.putNextGroup(nextGrp.nextObjective().id(), nextGrp); |
| 1231 | // check if addBuckets waiting for this completion |
| 1232 | NextObjective pendBkt = pendingBuckets |
| 1233 | .remove(nextGrp.nextObjective().id()); |
| 1234 | if (pendBkt != null) { |
| 1235 | addBucketToGroup(pendBkt, nextGrp); |
| 1236 | } |
| 1237 | }); |
| 1238 | } |
| 1239 | } |
| 1240 | } |
| 1241 | |
Charles Chan | fc5c780 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 1242 | private void processPendingRemoveNextObjs(GroupKey key) { |
| 1243 | pendingRemoveNextObjectives.asMap().forEach((nextObjective, groupKeys) -> { |
| 1244 | if (groupKeys.isEmpty()) { |
| 1245 | pendingRemoveNextObjectives.invalidate(nextObjective); |
| 1246 | Ofdpa2Pipeline.pass(nextObjective); |
| 1247 | } else { |
| 1248 | groupKeys.remove(key); |
| 1249 | } |
| 1250 | }); |
| 1251 | } |
| 1252 | |
Charles Chan | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 1253 | protected int getNextAvailableIndex() { |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1254 | return (int) nextIndex.incrementAndGet(); |
| 1255 | } |
| 1256 | |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 1257 | /** |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1258 | * Returns the outport in a traffic treatment. |
| 1259 | * |
| 1260 | * @param tt the treatment |
| 1261 | * @return the PortNumber for the outport or null |
| 1262 | */ |
| 1263 | protected static PortNumber readOutPortFromTreatment(TrafficTreatment tt) { |
| 1264 | for (Instruction ins : tt.allInstructions()) { |
| 1265 | if (ins.type() == Instruction.Type.OUTPUT) { |
| 1266 | return ((Instructions.OutputInstruction) ins).port(); |
| 1267 | } |
| 1268 | } |
| 1269 | return null; |
| 1270 | } |
| 1271 | |
| 1272 | /** |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 1273 | * Returns a hash as the L2 Interface Group Key. |
| 1274 | * |
| 1275 | * Keep the lower 6-bit for port since port number usually smaller than 64. |
| 1276 | * Hash other information into remaining 28 bits. |
| 1277 | * |
| 1278 | * @param deviceId Device ID |
| 1279 | * @param vlanId VLAN ID |
| 1280 | * @param portNumber Port number |
| 1281 | * @return L2 interface group key |
| 1282 | */ |
Charles Chan | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 1283 | protected int l2InterfaceGroupKey( |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 1284 | DeviceId deviceId, VlanId vlanId, long portNumber) { |
| 1285 | int portLowerBits = (int) portNumber & PORT_LOWER_BITS_MASK; |
| 1286 | long portHigherBits = portNumber & PORT_HIGHER_BITS_MASK; |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 1287 | int hash = Objects.hash(deviceId, vlanId, portHigherBits); |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 1288 | return L2_INTERFACE_TYPE | (TYPE_MASK & hash << 6) | portLowerBits; |
| 1289 | } |
| 1290 | |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1291 | /** |
| 1292 | * Utility class for moving group information around. |
| 1293 | */ |
Charles Chan | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 1294 | protected class GroupInfo { |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 1295 | /** |
| 1296 | * Description of the inner-most group of the group chain. |
| 1297 | * It is always an L2 interface group. |
| 1298 | */ |
| 1299 | private GroupDescription innerMostGroupDesc; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1300 | |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 1301 | /** |
| 1302 | * Description of the next group in the group chain. |
| 1303 | * It can be L2 interface, L3 interface, L3 unicast, L3 VPN group. |
| 1304 | * It is possible that nextGroup is the same as the innerMostGroup. |
| 1305 | */ |
| 1306 | private GroupDescription nextGroupDesc; |
| 1307 | |
| 1308 | GroupInfo(GroupDescription innerMostGroupDesc, GroupDescription nextGroupDesc) { |
| 1309 | this.innerMostGroupDesc = innerMostGroupDesc; |
| 1310 | this.nextGroupDesc = nextGroupDesc; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1311 | } |
| 1312 | } |
| 1313 | |
| 1314 | /** |
| 1315 | * Represents an entire group-chain that implements a Next-Objective from |
| 1316 | * the application. The objective is represented as a list of deques, where |
| 1317 | * each deque is a separate chain of groups. |
| 1318 | * <p> |
| 1319 | * For example, an ECMP group with 3 buckets, where each bucket points to |
| 1320 | * a group chain of L3 Unicast and L2 interface groups will look like this: |
| 1321 | * <ul> |
| 1322 | * <li>List[0] is a Deque of GroupKeyECMP(first)-GroupKeyL3(middle)-GroupKeyL2(last) |
| 1323 | * <li>List[1] is a Deque of GroupKeyECMP(first)-GroupKeyL3(middle)-GroupKeyL2(last) |
| 1324 | * <li>List[2] is a Deque of GroupKeyECMP(first)-GroupKeyL3(middle)-GroupKeyL2(last) |
| 1325 | * </ul> |
| 1326 | * where the first element of each deque is the same, representing the |
| 1327 | * top level ECMP group, while every other element represents a unique groupKey. |
| 1328 | * <p> |
| 1329 | * Also includes information about the next objective that |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1330 | * resulted in these group-chains. |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1331 | * |
| 1332 | */ |
| 1333 | protected class OfdpaNextGroup implements NextGroup { |
| 1334 | private final NextObjective nextObj; |
| 1335 | private final List<Deque<GroupKey>> gkeys; |
| 1336 | |
| 1337 | public OfdpaNextGroup(List<Deque<GroupKey>> gkeys, NextObjective nextObj) { |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1338 | this.nextObj = nextObj; |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1339 | this.gkeys = gkeys; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | public NextObjective nextObjective() { |
| 1343 | return nextObj; |
| 1344 | } |
| 1345 | |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1346 | public List<Deque<GroupKey>> groupKeys() { |
| 1347 | return gkeys; |
| 1348 | } |
| 1349 | |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1350 | @Override |
| 1351 | public byte[] data() { |
Charles Chan | 361154b | 2016-03-24 10:23:39 -0700 | [diff] [blame] | 1352 | return Ofdpa2Pipeline.appKryo.serialize(gkeys); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1353 | } |
| 1354 | } |
| 1355 | |
| 1356 | /** |
| 1357 | * Represents a group element that is part of a chain of groups. |
| 1358 | * Stores enough information to create a Group Description to add the group |
| 1359 | * to the switch by requesting the Group Service. Objects instantiating this |
| 1360 | * 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] | 1361 | * referenced groups in the group chain to be created. |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1362 | */ |
Charles Chan | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 1363 | protected class GroupChainElem { |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1364 | private GroupDescription groupDescription; |
| 1365 | private AtomicInteger waitOnGroups; |
| 1366 | private boolean addBucketToGroup; |
| 1367 | |
| 1368 | GroupChainElem(GroupDescription groupDescription, int waitOnGroups, |
| 1369 | boolean addBucketToGroup) { |
| 1370 | this.groupDescription = groupDescription; |
| 1371 | this.waitOnGroups = new AtomicInteger(waitOnGroups); |
| 1372 | this.addBucketToGroup = addBucketToGroup; |
| 1373 | } |
| 1374 | |
| 1375 | /** |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1376 | * This method atomically decrements the counter for the number of |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1377 | * groups this GroupChainElement is waiting on, for notifications from |
| 1378 | * the Group Service. When this method returns a value of 0, this |
| 1379 | * GroupChainElement is ready to be processed. |
| 1380 | * |
| 1381 | * @return integer indication of the number of notifications being waited on |
| 1382 | */ |
| 1383 | int decrementAndGetGroupsWaitedOn() { |
| 1384 | return waitOnGroups.decrementAndGet(); |
| 1385 | } |
| 1386 | |
| 1387 | @Override |
| 1388 | public String toString() { |
| 1389 | return (Integer.toHexString(groupDescription.givenGroupId()) + |
| 1390 | " groupKey: " + groupDescription.appCookie() + |
| 1391 | " waiting-on-groups: " + waitOnGroups.get() + |
| 1392 | " addBucketToGroup: " + addBucketToGroup + |
| 1393 | " device: " + deviceId); |
| 1394 | } |
| 1395 | } |
| 1396 | } |