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