Brian O'Connor | 7cbbbb7 | 2016-04-09 02:13:23 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Brian O'Connor | 7cbbbb7 | 2016-04-09 02:13:23 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 16 | package org.onosproject.driver.pipeline.ofdpa; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 17 | |
| 18 | import com.google.common.cache.Cache; |
| 19 | import com.google.common.cache.CacheBuilder; |
| 20 | import com.google.common.cache.RemovalCause; |
| 21 | import com.google.common.cache.RemovalNotification; |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableList; |
| 23 | import com.google.common.collect.Lists; |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 24 | import com.google.common.collect.Sets; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 25 | import org.onlab.osgi.ServiceDirectory; |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 26 | import org.onlab.packet.IpPrefix; |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 27 | import org.onlab.packet.MacAddress; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 28 | import org.onlab.packet.MplsLabel; |
| 29 | import org.onlab.packet.VlanId; |
| 30 | import org.onosproject.core.ApplicationId; |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 31 | import org.onosproject.core.GroupId; |
Charles Chan | 367c1c1 | 2018-10-19 16:23:28 -0700 | [diff] [blame] | 32 | import org.onosproject.driver.extensions.Ofdpa3AllowVlanTranslationType; |
| 33 | import org.onosproject.driver.extensions.OfdpaSetAllowVlanTranslation; |
Charles Chan | 3256252 | 2016-04-07 14:37:14 -0700 | [diff] [blame] | 34 | import org.onosproject.driver.extensions.OfdpaSetVlanVid; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 35 | import org.onosproject.net.DeviceId; |
| 36 | import org.onosproject.net.PortNumber; |
| 37 | import org.onosproject.net.behaviour.NextGroup; |
| 38 | import org.onosproject.net.behaviour.PipelinerContext; |
| 39 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 40 | import org.onosproject.net.flow.TrafficSelector; |
| 41 | import org.onosproject.net.flow.TrafficTreatment; |
| 42 | import org.onosproject.net.flow.criteria.Criterion; |
Charles Chan | 367c1c1 | 2018-10-19 16:23:28 -0700 | [diff] [blame] | 43 | import org.onosproject.net.flow.criteria.PortCriterion; |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 44 | import org.onosproject.net.flow.criteria.TunnelIdCriterion; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 45 | import org.onosproject.net.flow.criteria.VlanIdCriterion; |
| 46 | import org.onosproject.net.flow.instructions.Instruction; |
| 47 | import org.onosproject.net.flow.instructions.Instructions; |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 48 | import org.onosproject.net.flow.instructions.Instructions.GroupInstruction; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 49 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
Yi Tseng | 47f82dc | 2017-03-05 22:48:39 -0800 | [diff] [blame] | 50 | import org.onosproject.net.flowobjective.DefaultNextObjective; |
Charles Chan | 367c1c1 | 2018-10-19 16:23:28 -0700 | [diff] [blame] | 51 | import org.onosproject.net.flowobjective.DefaultNextTreatment; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 52 | import org.onosproject.net.flowobjective.FlowObjectiveStore; |
Charles Chan | 367c1c1 | 2018-10-19 16:23:28 -0700 | [diff] [blame] | 53 | import org.onosproject.net.flowobjective.IdNextTreatment; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 54 | import org.onosproject.net.flowobjective.NextObjective; |
Charles Chan | 367c1c1 | 2018-10-19 16:23:28 -0700 | [diff] [blame] | 55 | import org.onosproject.net.flowobjective.NextTreatment; |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 56 | import org.onosproject.net.flowobjective.Objective.Operation; |
Yi Tseng | 47f82dc | 2017-03-05 22:48:39 -0800 | [diff] [blame] | 57 | import org.onosproject.net.flowobjective.ObjectiveContext; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 58 | import org.onosproject.net.flowobjective.ObjectiveError; |
| 59 | import org.onosproject.net.group.DefaultGroupBucket; |
| 60 | import org.onosproject.net.group.DefaultGroupDescription; |
| 61 | import org.onosproject.net.group.DefaultGroupKey; |
| 62 | import org.onosproject.net.group.Group; |
| 63 | import org.onosproject.net.group.GroupBucket; |
| 64 | import org.onosproject.net.group.GroupBuckets; |
| 65 | import org.onosproject.net.group.GroupDescription; |
| 66 | import org.onosproject.net.group.GroupEvent; |
| 67 | import org.onosproject.net.group.GroupKey; |
| 68 | import org.onosproject.net.group.GroupListener; |
| 69 | import org.onosproject.net.group.GroupService; |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 70 | import org.onosproject.store.service.AtomicCounter; |
| 71 | import org.onosproject.store.service.StorageService; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 72 | import org.slf4j.Logger; |
| 73 | |
| 74 | import java.util.ArrayDeque; |
| 75 | import java.util.ArrayList; |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 76 | import java.util.Arrays; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 77 | import java.util.Collection; |
| 78 | import java.util.Collections; |
| 79 | import java.util.Deque; |
| 80 | import java.util.List; |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 81 | import java.util.Objects; |
Charles Chan | 367c1c1 | 2018-10-19 16:23:28 -0700 | [diff] [blame] | 82 | import java.util.Optional; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 83 | import java.util.Set; |
| 84 | import java.util.concurrent.ConcurrentHashMap; |
| 85 | import java.util.concurrent.CopyOnWriteArrayList; |
| 86 | import java.util.concurrent.Executors; |
| 87 | import java.util.concurrent.ScheduledExecutorService; |
| 88 | import java.util.concurrent.TimeUnit; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 89 | import java.util.stream.Collectors; |
| 90 | |
| 91 | import static org.onlab.util.Tools.groupedThreads; |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 92 | import static org.onosproject.driver.pipeline.ofdpa.Ofdpa2Pipeline.*; |
pier | 9469f3e | 2019-04-17 17:05:08 +0200 | [diff] [blame] | 93 | import static org.onosproject.driver.pipeline.ofdpa.OfdpaPipelineUtility.*; |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 94 | import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*; |
jayakumarthazhath | 655b9a8 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 95 | import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.L2_MULTICAST_TYPE; |
| 96 | import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.l2MulticastGroupKey; |
Charles Chan | 367c1c1 | 2018-10-19 16:23:28 -0700 | [diff] [blame] | 97 | import static org.onosproject.net.flow.criteria.Criterion.Type.IN_PORT; |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 98 | import static org.onosproject.net.flow.criteria.Criterion.Type.TUNNEL_ID; |
Pier Ventre | 140a894 | 2016-11-02 07:26:38 -0700 | [diff] [blame] | 99 | import static org.onosproject.net.flow.criteria.Criterion.Type.VLAN_VID; |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 100 | import static org.onosproject.net.group.GroupDescription.Type.ALL; |
Andreas Pantelopoulos | 7a8488c | 2018-01-22 16:00:28 -0800 | [diff] [blame] | 101 | import static org.onosproject.net.group.GroupDescription.Type.INDIRECT; |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 102 | import static org.onosproject.net.group.GroupDescription.Type.SELECT; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 103 | import static org.slf4j.LoggerFactory.getLogger; |
| 104 | |
| 105 | /** |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 106 | * Group handler that emulates Broadcom OF-DPA TTP. |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 107 | */ |
Charles Chan | 361154b | 2016-03-24 10:23:39 -0700 | [diff] [blame] | 108 | public class Ofdpa2GroupHandler { |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 109 | protected final Logger log = getLogger(getClass()); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 110 | // Services, Stores |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 111 | protected GroupService groupService; |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 112 | protected StorageService storageService; |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 113 | protected FlowObjectiveStore flowObjectiveStore; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 114 | // index number for group creation |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 115 | private AtomicCounter nextIndex; |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 116 | protected DeviceId deviceId; |
Saurav Das | 2f2c9d0 | 2018-04-07 16:51:09 -0700 | [diff] [blame] | 117 | Cache<GroupKey, List<OfdpaGroupHandlerUtility.OfdpaNextGroup>> pendingAddNextObjectives; |
| 118 | Cache<NextObjective, List<GroupKey>> pendingRemoveNextObjectives; |
| 119 | Cache<GroupKey, Set<OfdpaGroupHandlerUtility.GroupChainElem>> pendingGroups; |
| 120 | ConcurrentHashMap<GroupKey, Set<NextObjective>> pendingUpdateNextObjectives; |
Yi Tseng | 47f82dc | 2017-03-05 22:48:39 -0800 | [diff] [blame] | 121 | // local store for pending bucketAdds - by design there can be multiple |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 122 | // pending bucket for a group |
Yi Tseng | 47f82dc | 2017-03-05 22:48:39 -0800 | [diff] [blame] | 123 | protected ConcurrentHashMap<Integer, Set<NextObjective>> pendingBuckets = |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 124 | new ConcurrentHashMap<>(); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 125 | private ScheduledExecutorService groupCheckerExecutor = |
| 126 | Executors.newScheduledThreadPool(2, groupedThreads("onos/pipeliner", "ofdpa-%d", log)); |
pier | 7a47720 | 2020-03-20 11:00:38 +0100 | [diff] [blame] | 127 | private InnerGroupListener innerGroupListener = new InnerGroupListener(); |
Charles Chan | 40132b3 | 2017-01-22 00:19:37 -0800 | [diff] [blame] | 128 | /** |
| 129 | * Determines whether this pipeline support copy ttl instructions or not. |
| 130 | * |
| 131 | * @return true if copy ttl instructions are supported |
| 132 | */ |
| 133 | protected boolean supportCopyTtl() { |
| 134 | return true; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Determines whether this pipeline support set mpls bos instruction or not. |
| 139 | * |
| 140 | * @return true if set mpls bos instruction is supported |
| 141 | */ |
| 142 | protected boolean supportSetMplsBos() { |
| 143 | return true; |
| 144 | } |
| 145 | |
Charles Chan | 0f43e47 | 2017-02-14 14:00:16 -0800 | [diff] [blame] | 146 | /** |
| 147 | * Determines whether this pipeline requires popping VLAN before pushing MPLS. |
| 148 | * <p> |
| 149 | * If required, pop vlan before push mpls and add an arbitrary vlan back afterward. |
| 150 | * MPLS interface group will substitute the arbitrary VLAN with expected VLAN later on. |
| 151 | * |
| 152 | * @return true if this pipeline requires popping VLAN before pushing MPLS |
| 153 | */ |
| 154 | protected boolean requireVlanPopBeforeMplsPush() { |
| 155 | return false; |
| 156 | } |
| 157 | |
Charles Chan | 367c1c1 | 2018-10-19 16:23:28 -0700 | [diff] [blame] | 158 | /** |
| 159 | * Determines whether this pipeline requires AllowVlanTransition in L2 unfiltered group. |
| 160 | * |
| 161 | * @return true if the AllowVlanTransition action is required |
| 162 | */ |
| 163 | protected boolean requireAllowVlanTransition() { |
| 164 | return true; |
| 165 | } |
| 166 | |
Alex Yashchuk | 4caa8e8 | 2017-12-08 17:40:05 +0200 | [diff] [blame] | 167 | public void init(DeviceId deviceId, PipelinerContext context) { |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 168 | ServiceDirectory serviceDirectory = context.directory(); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 169 | this.deviceId = deviceId; |
| 170 | this.flowObjectiveStore = context.store(); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 171 | this.groupService = serviceDirectory.get(GroupService.class); |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 172 | this.storageService = serviceDirectory.get(StorageService.class); |
Madan Jampani | d5714e0 | 2016-04-19 14:15:20 -0700 | [diff] [blame] | 173 | this.nextIndex = storageService.getAtomicCounter("group-id-index-counter"); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 174 | |
Charles Chan | fc5c780 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 175 | pendingAddNextObjectives = CacheBuilder.newBuilder() |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 176 | .expireAfterWrite(20, TimeUnit.SECONDS) |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 177 | .removalListener((RemovalNotification<GroupKey, List<OfdpaNextGroup>> notification) -> { |
| 178 | if (notification.getCause() == RemovalCause.EXPIRED && |
| 179 | Objects.nonNull(notification.getValue())) { |
| 180 | notification.getValue() |
| 181 | .forEach(ofdpaNextGrp -> |
| 182 | fail(ofdpaNextGrp.nextObjective(), |
| 183 | ObjectiveError.GROUPINSTALLATIONFAILED)); |
Charles Chan | fc5c780 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 184 | } |
| 185 | }).build(); |
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 | pendingRemoveNextObjectives = CacheBuilder.newBuilder() |
| 188 | .expireAfterWrite(20, TimeUnit.SECONDS) |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 189 | .removalListener((RemovalNotification<NextObjective, List<GroupKey>> notification) -> { |
Charles Chan | fc5c780 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 190 | if (notification.getCause() == RemovalCause.EXPIRED) { |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 191 | fail(notification.getKey(), |
| 192 | ObjectiveError.GROUPREMOVALFAILED); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 193 | } |
| 194 | }).build(); |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 195 | pendingGroups = CacheBuilder.newBuilder() |
| 196 | .expireAfterWrite(20, TimeUnit.SECONDS) |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 197 | .removalListener((RemovalNotification<GroupKey, Set<GroupChainElem>> notification) -> { |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 198 | if (notification.getCause() == RemovalCause.EXPIRED) { |
| 199 | log.error("Unable to install group with key {} and pending GCEs: {}", |
| 200 | notification.getKey(), notification.getValue()); |
| 201 | } |
| 202 | }).build(); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 203 | pendingUpdateNextObjectives = new ConcurrentHashMap<>(); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 204 | GroupChecker groupChecker = new GroupChecker(this); |
| 205 | groupCheckerExecutor.scheduleAtFixedRate(groupChecker, 0, 500, TimeUnit.MILLISECONDS); |
pier | 7a47720 | 2020-03-20 11:00:38 +0100 | [diff] [blame] | 206 | groupService.addListener(innerGroupListener); |
| 207 | } |
| 208 | |
| 209 | // Terminate internal references |
| 210 | public void terminate() { |
| 211 | if (nextIndex != null) { |
| 212 | nextIndex.destroy(); |
| 213 | } |
| 214 | nextIndex = null; |
| 215 | if (pendingAddNextObjectives != null) { |
| 216 | pendingAddNextObjectives.cleanUp(); |
| 217 | } |
| 218 | pendingAddNextObjectives = null; |
| 219 | if (pendingRemoveNextObjectives != null) { |
| 220 | pendingRemoveNextObjectives.cleanUp(); |
| 221 | } |
| 222 | pendingRemoveNextObjectives = null; |
| 223 | if (pendingGroups != null) { |
| 224 | pendingGroups.cleanUp(); |
| 225 | } |
| 226 | pendingGroups = null; |
| 227 | if (groupCheckerExecutor != null) { |
| 228 | groupCheckerExecutor.shutdown(); |
| 229 | } |
| 230 | groupCheckerExecutor = null; |
| 231 | if (groupService != null) { |
| 232 | groupService.removeListener(innerGroupListener); |
| 233 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 234 | } |
| 235 | |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 236 | ////////////////////////////////////// |
| 237 | // Group Creation |
| 238 | ////////////////////////////////////// |
| 239 | |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 240 | /** |
| 241 | * Adds a list of group chain by given NextObjective. |
| 242 | * |
| 243 | * @param nextObjective the NextObjective |
| 244 | */ |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 245 | protected void addGroup(NextObjective nextObjective) { |
| 246 | switch (nextObjective.type()) { |
| 247 | case SIMPLE: |
| 248 | Collection<TrafficTreatment> treatments = nextObjective.next(); |
| 249 | if (treatments.size() != 1) { |
| 250 | log.error("Next Objectives of type Simple should only have a " |
| 251 | + "single Traffic Treatment. Next Objective Id:{}", |
| 252 | nextObjective.id()); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 253 | fail(nextObjective, ObjectiveError.BADPARAMS); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 254 | return; |
| 255 | } |
| 256 | processSimpleNextObjective(nextObjective); |
| 257 | break; |
| 258 | case BROADCAST: |
| 259 | processBroadcastNextObjective(nextObjective); |
| 260 | break; |
| 261 | case HASHED: |
Pier Ventre | 140a894 | 2016-11-02 07:26:38 -0700 | [diff] [blame] | 262 | if (!verifyHashedNextObjective(nextObjective)) { |
| 263 | log.error("Next Objectives of type hashed not supported. Next Objective Id:{}", |
| 264 | nextObjective.id()); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 265 | fail(nextObjective, ObjectiveError.BADPARAMS); |
Pier Ventre | 140a894 | 2016-11-02 07:26:38 -0700 | [diff] [blame] | 266 | return; |
| 267 | } |
Charles Chan | 367c1c1 | 2018-10-19 16:23:28 -0700 | [diff] [blame] | 268 | if (isL2Hash(nextObjective)) { |
| 269 | processL2HashedNextObjective(nextObjective); |
| 270 | return; |
| 271 | } |
| 272 | processEcmpHashedNextObjective(nextObjective); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 273 | break; |
| 274 | case FAILOVER: |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 275 | fail(nextObjective, ObjectiveError.UNSUPPORTED); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 276 | log.warn("Unsupported next objective type {}", nextObjective.type()); |
| 277 | break; |
| 278 | default: |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 279 | fail(nextObjective, ObjectiveError.UNKNOWN); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 280 | log.warn("Unknown next objective type {}", nextObjective.type()); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | /** |
jayakumarthazhath | 655b9a8 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 285 | * Similar to processBroadcastNextObjective but handles L2 Multicast Next Objectives. |
| 286 | * |
| 287 | * @param nextObj NextObjective of L2_MULTICAST with chained NextObjectives for single homed access ports |
| 288 | */ |
| 289 | private void processL2MulticastNextObjective(NextObjective nextObj) { |
| 290 | |
| 291 | VlanId assignedVlan = readVlanFromSelector(nextObj.meta()); |
| 292 | if (assignedVlan == null) { |
| 293 | log.warn("VLAN ID required by L2 multicast next objective is missing. Aborting group creation."); |
| 294 | fail(nextObj, ObjectiveError.BADPARAMS); |
| 295 | return; |
| 296 | } |
| 297 | |
| 298 | // Group info should contain only single homed hosts for a given vlanId |
| 299 | List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan); |
| 300 | createL2MulticastGroup(nextObj, assignedVlan, groupInfos); |
| 301 | } |
| 302 | |
| 303 | private void createL2MulticastGroup(NextObjective nextObj, VlanId vlanId, List<GroupInfo> groupInfos) { |
| 304 | // Realize & represent L2 multicast group in OFDPA driver layer |
| 305 | // TODO : Need to identify significance of OfdpaNextGroup. |
| 306 | Integer l2MulticastGroupId = L2_MULTICAST_TYPE | (vlanId.toShort() << 16); |
| 307 | final GroupKey l2MulticastGroupKey = l2MulticastGroupKey(vlanId, deviceId); |
| 308 | List<Deque<GroupKey>> l2MulticastAllGroup = Lists.newArrayList(); |
| 309 | groupInfos.forEach(groupInfo -> { |
| 310 | Deque<GroupKey> groupKeyChain = new ArrayDeque<>(); |
| 311 | groupKeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie()); |
| 312 | groupKeyChain.addFirst(l2MulticastGroupKey); |
| 313 | l2MulticastAllGroup.add(groupKeyChain); |
| 314 | }); |
| 315 | OfdpaNextGroup ofdpaL2MulticastGroup = new OfdpaNextGroup(l2MulticastAllGroup, nextObj); |
| 316 | updatePendingNextObjective(l2MulticastGroupKey, ofdpaL2MulticastGroup); |
| 317 | // Group Chain Hierarchy creation using group service and thus in device level |
| 318 | List<GroupBucket> l2McastBuckets = new ArrayList<>(); |
| 319 | groupInfos.forEach(groupInfo -> { |
| 320 | // Points to L2 interface group directly. |
| 321 | TrafficTreatment.Builder trafficTreatment = DefaultTrafficTreatment.builder(); |
| 322 | trafficTreatment.group(new GroupId(groupInfo.innerMostGroupDesc().givenGroupId())); |
| 323 | GroupBucket bucket = DefaultGroupBucket.createAllGroupBucket(trafficTreatment.build()); |
| 324 | l2McastBuckets.add(bucket); |
| 325 | }); |
| 326 | GroupDescription l2MulticastGroupDescription = |
| 327 | new DefaultGroupDescription( |
| 328 | deviceId, |
| 329 | ALL, |
| 330 | new GroupBuckets(l2McastBuckets), |
| 331 | l2MulticastGroupKey, |
| 332 | l2MulticastGroupId, |
| 333 | nextObj.appId()); |
| 334 | GroupChainElem l2MulticastGce = new GroupChainElem(l2MulticastGroupDescription, |
| 335 | groupInfos.size(), false, deviceId); |
| 336 | groupInfos.forEach(groupInfo -> { |
| 337 | updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), l2MulticastGce); |
| 338 | groupService.addGroup(groupInfo.innerMostGroupDesc()); |
| 339 | }); |
| 340 | } |
| 341 | |
| 342 | /** |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 343 | * As per the OFDPA 2.0 TTP, packets are sent out of ports by using |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 344 | * a chain of groups. The simple Next Objective passed in by the application |
| 345 | * is broken up into a group chain. The following chains can be created |
| 346 | * depending on the parameters in the Next Objective. |
| 347 | * 1. L2 Interface group (no chaining) |
| 348 | * 2. L3 Unicast group -> L2 Interface group |
| 349 | * 3. MPLS Interface group -> L2 Interface group |
| 350 | * 4. MPLS Swap group -> MPLS Interface group -> L2 Interface group |
| 351 | * 5. PW initiation group chain |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 352 | * |
| 353 | * @param nextObj the nextObjective of type SIMPLE |
| 354 | */ |
| 355 | private void processSimpleNextObjective(NextObjective nextObj) { |
| 356 | TrafficTreatment treatment = nextObj.next().iterator().next(); |
Andreas Pantelopoulos | 7a8488c | 2018-01-22 16:00:28 -0800 | [diff] [blame] | 357 | // determine if plain L2 or L3->L2 or MPLS Swap -> MPLS Interface -> L2 |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 358 | boolean plainL2 = true; |
Andreas Pantelopoulos | 7a8488c | 2018-01-22 16:00:28 -0800 | [diff] [blame] | 359 | boolean mplsSwap = false; |
| 360 | MplsLabel mplsLabel = null; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 361 | for (Instruction ins : treatment.allInstructions()) { |
| 362 | if (ins.type() == Instruction.Type.L2MODIFICATION) { |
| 363 | L2ModificationInstruction l2ins = (L2ModificationInstruction) ins; |
| 364 | if (l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_DST || |
| 365 | l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_SRC) { |
| 366 | plainL2 = false; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 367 | } |
Andreas Pantelopoulos | 7a8488c | 2018-01-22 16:00:28 -0800 | [diff] [blame] | 368 | // mpls label in simple next objectives is used only to indicate |
| 369 | // a MPLS Swap group before the MPLS Interface Group |
| 370 | if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_LABEL) { |
| 371 | mplsSwap = true; |
| 372 | mplsLabel = ((L2ModificationInstruction.ModMplsLabelInstruction) l2ins).label(); |
| 373 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 374 | } |
| 375 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 376 | if (plainL2) { |
| 377 | createL2InterfaceGroup(nextObj); |
| 378 | return; |
| 379 | } |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 380 | // In order to understand if it is a pseudowire related |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 381 | // next objective we look for the tunnel id in the meta. |
| 382 | boolean isPw = false; |
Pier Ventre | 140a894 | 2016-11-02 07:26:38 -0700 | [diff] [blame] | 383 | if (nextObj.meta() != null) { |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 384 | TunnelIdCriterion tunnelIdCriterion = (TunnelIdCriterion) nextObj |
| 385 | .meta() |
| 386 | .getCriterion(TUNNEL_ID); |
| 387 | if (tunnelIdCriterion != null) { |
| 388 | isPw = true; |
| 389 | } |
Pier Ventre | 140a894 | 2016-11-02 07:26:38 -0700 | [diff] [blame] | 390 | } |
Andreas Pantelopoulos | 25db42f | 2018-02-08 12:42:06 -0800 | [diff] [blame] | 391 | if (mplsSwap && !isPw) { |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 392 | log.debug("Creating a MPLS Swap -> MPLS Interface -> L2 Interface group chain."); |
Andreas Pantelopoulos | 7a8488c | 2018-01-22 16:00:28 -0800 | [diff] [blame] | 393 | // break up simple next objective to GroupChain objects |
| 394 | GroupInfo groupInfo = createL2L3Chain(treatment, nextObj.id(), |
| 395 | nextObj.appId(), true, |
| 396 | nextObj.meta()); |
| 397 | if (groupInfo == null) { |
| 398 | log.error("Could not process nextObj={} in dev:{}", nextObj.id(), deviceId); |
| 399 | fail(nextObj, ObjectiveError.BADPARAMS); |
| 400 | return; |
| 401 | } |
Andreas Pantelopoulos | 7a8488c | 2018-01-22 16:00:28 -0800 | [diff] [blame] | 402 | Deque<GroupKey> gkeyChain = new ArrayDeque<>(); |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 403 | gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie()); // l2 interface |
| 404 | gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie()); // mpls interface |
Andreas Pantelopoulos | 7a8488c | 2018-01-22 16:00:28 -0800 | [diff] [blame] | 405 | // creating the mpls swap group and adding it to the chain |
Andreas Pantelopoulos | 7a8488c | 2018-01-22 16:00:28 -0800 | [diff] [blame] | 406 | int nextGid = groupInfo.nextGroupDesc().givenGroupId(); |
| 407 | int index = getNextAvailableIndex(); |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 408 | GroupDescription swapGroupDescription = createMplsSwap( |
Andreas Pantelopoulos | 7a8488c | 2018-01-22 16:00:28 -0800 | [diff] [blame] | 409 | nextGid, |
| 410 | OfdpaMplsGroupSubType.MPLS_SWAP_LABEL, |
| 411 | index, |
| 412 | mplsLabel, |
| 413 | nextObj.appId() |
| 414 | ); |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 415 | // ensure swap group is added after L2L3 chain |
| 416 | GroupKey swapGroupKey = swapGroupDescription.appCookie(); |
| 417 | GroupChainElem swapChainElem = new GroupChainElem(swapGroupDescription, |
| 418 | 1, false, deviceId); |
| 419 | updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), swapChainElem); |
| 420 | gkeyChain.addFirst(swapGroupKey); |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 421 | // ensure nextObjective waits on the outermost groupKey |
| 422 | List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList(); |
| 423 | allGroupKeys.add(gkeyChain); |
| 424 | OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj); |
| 425 | updatePendingNextObjective(swapGroupKey, ofdpaGrp); |
Andreas Pantelopoulos | 7a8488c | 2018-01-22 16:00:28 -0800 | [diff] [blame] | 426 | // now we are ready to send the l2 groupDescription (inner), as all the stores |
| 427 | // that will get async replies have been updated. By waiting to update |
| 428 | // the stores, we prevent nasty race conditions. |
| 429 | groupService.addGroup(groupInfo.innerMostGroupDesc()); |
| 430 | } else if (!isPw) { |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 431 | boolean isMpls = false; |
| 432 | if (nextObj.meta() != null) { |
| 433 | isMpls = isNotMplsBos(nextObj.meta()); |
| 434 | } |
| 435 | log.debug("Creating a {} -> L2 Interface group chain.", |
| 436 | (isMpls) ? "MPLS Interface" : "L3 Unicast"); |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 437 | // break up simple next objective to GroupChain objects |
| 438 | GroupInfo groupInfo = createL2L3Chain(treatment, nextObj.id(), |
| 439 | nextObj.appId(), isMpls, |
| 440 | nextObj.meta()); |
| 441 | if (groupInfo == null) { |
| 442 | log.error("Could not process nextObj={} in dev:{}", nextObj.id(), deviceId); |
Andreas Pantelopoulos | 7a8488c | 2018-01-22 16:00:28 -0800 | [diff] [blame] | 443 | fail(nextObj, ObjectiveError.BADPARAMS); |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 444 | return; |
| 445 | } |
| 446 | // create object for local and distributed storage |
| 447 | Deque<GroupKey> gkeyChain = new ArrayDeque<>(); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 448 | gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie()); |
| 449 | gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie()); |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 450 | List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList(); |
| 451 | allGroupKeys.add(gkeyChain); |
| 452 | OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj); |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 453 | // store l3groupkey with the ofdpaNextGroup for the nextObjective that depends on it |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 454 | updatePendingNextObjective(groupInfo.nextGroupDesc().appCookie(), ofdpaGrp); |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 455 | // now we are ready to send the l2 groupDescription (inner), as all the stores |
| 456 | // that will get async replies have been updated. By waiting to update |
| 457 | // the stores, we prevent nasty race conditions. |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 458 | groupService.addGroup(groupInfo.innerMostGroupDesc()); |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 459 | } else { |
| 460 | // We handle the pseudo wire with a different a procedure. |
| 461 | // This procedure is meant to handle both initiation and |
| 462 | // termination of the pseudo wire. |
| 463 | processPwNextObjective(nextObj); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 464 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 465 | } |
| 466 | |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 467 | /** |
| 468 | * Creates a simple L2 Interface Group. |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 469 | * @param nextObj the next Objective |
| 470 | */ |
| 471 | private void createL2InterfaceGroup(NextObjective nextObj) { |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 472 | VlanId assignedVlan = readVlanFromSelector(nextObj.meta()); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 473 | if (assignedVlan == null) { |
| 474 | log.warn("VLAN ID required by simple next obj is missing. Abort."); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 475 | fail(nextObj, ObjectiveError.BADPARAMS); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 476 | return; |
| 477 | } |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 478 | List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 479 | // There is only one L2 interface group in this case |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 480 | GroupDescription l2InterfaceGroupDesc = groupInfos.get(0).innerMostGroupDesc(); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 481 | // Put all dependency information into allGroupKeys |
| 482 | List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList(); |
| 483 | Deque<GroupKey> gkeyChain = new ArrayDeque<>(); |
| 484 | gkeyChain.addFirst(l2InterfaceGroupDesc.appCookie()); |
| 485 | allGroupKeys.add(gkeyChain); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 486 | // Point the next objective to this group |
| 487 | OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj); |
| 488 | updatePendingNextObjective(l2InterfaceGroupDesc.appCookie(), ofdpaGrp); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 489 | // Start installing the inner-most group |
jayakumarthazhath | 655b9a8 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 490 | groupService.addGroup(l2InterfaceGroupDesc); } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 491 | |
| 492 | /** |
Andreas Pantelopoulos | 7a8488c | 2018-01-22 16:00:28 -0800 | [diff] [blame] | 493 | * Creates an Mpls group of type swap. |
| 494 | * |
| 495 | * @param nextGroupId the next group in the chain |
| 496 | * @param subtype the mpls swap label group subtype |
| 497 | * @param index the index of the group |
| 498 | * @param mplsLabel the mpls label to swap |
| 499 | * @param applicationId the application id |
| 500 | * @return the group description |
| 501 | */ |
| 502 | protected GroupDescription createMplsSwap(int nextGroupId, |
| 503 | OfdpaMplsGroupSubType subtype, |
| 504 | int index, |
| 505 | MplsLabel mplsLabel, |
| 506 | ApplicationId applicationId) { |
Andreas Pantelopoulos | 7a8488c | 2018-01-22 16:00:28 -0800 | [diff] [blame] | 507 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
Andreas Pantelopoulos | 7a8488c | 2018-01-22 16:00:28 -0800 | [diff] [blame] | 508 | treatment.setMpls(mplsLabel); |
Andreas Pantelopoulos | 7a8488c | 2018-01-22 16:00:28 -0800 | [diff] [blame] | 509 | // We point the group to the next group. |
| 510 | treatment.group(new GroupId(nextGroupId)); |
| 511 | GroupBucket groupBucket = DefaultGroupBucket |
| 512 | .createIndirectGroupBucket(treatment.build()); |
| 513 | // Finally we build the group description. |
| 514 | int groupId = makeMplsLabelGroupId(subtype, index); |
| 515 | GroupKey groupKey = new DefaultGroupKey( |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 516 | Ofdpa2Pipeline.appKryo.serialize(index)); |
Andreas Pantelopoulos | 7a8488c | 2018-01-22 16:00:28 -0800 | [diff] [blame] | 517 | return new DefaultGroupDescription( |
| 518 | deviceId, |
| 519 | INDIRECT, |
| 520 | new GroupBuckets(Collections.singletonList(groupBucket)), |
| 521 | groupKey, |
| 522 | groupId, |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 523 | applicationId); |
Andreas Pantelopoulos | 7a8488c | 2018-01-22 16:00:28 -0800 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | /** |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 527 | * Creates one of two possible group-chains from the treatment |
| 528 | * passed in. Depending on the MPLS boolean, this method either creates |
Charles Chan | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 529 | * an L3Unicast Group --> L2Interface Group, if mpls is false; |
| 530 | * or MPLSInterface Group --> L2Interface Group, if mpls is true; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 531 | * The returned 'inner' group description is always the L2 Interface group. |
| 532 | * |
| 533 | * @param treatment that needs to be broken up to create the group chain |
| 534 | * @param nextId of the next objective that needs this group chain |
| 535 | * @param appId of the application that sent this next objective |
| 536 | * @param mpls determines if L3Unicast or MPLSInterface group is created |
| 537 | * @param meta metadata passed in by the application as part of the nextObjective |
| 538 | * @return GroupInfo containing the GroupDescription of the |
| 539 | * L2Interface group(inner) and the GroupDescription of the (outer) |
| 540 | * L3Unicast/MPLSInterface group. May return null if there is an |
| 541 | * error in processing the chain |
| 542 | */ |
Charles Chan | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 543 | protected GroupInfo createL2L3Chain(TrafficTreatment treatment, int nextId, |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 544 | ApplicationId appId, boolean mpls, |
| 545 | TrafficSelector meta) { |
| 546 | return createL2L3ChainInternal(treatment, nextId, appId, mpls, meta, true); |
| 547 | } |
| 548 | |
| 549 | /** |
| 550 | * Internal implementation of createL2L3Chain. |
| 551 | * <p> |
| 552 | * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12. |
| 553 | * Since it is non-OF spec, we need an extension treatment for that. |
| 554 | * The useSetVlanExtension must be set to false for OFDPA i12. |
| 555 | * </p> |
| 556 | * |
| 557 | * @param treatment that needs to be broken up to create the group chain |
| 558 | * @param nextId of the next objective that needs this group chain |
| 559 | * @param appId of the application that sent this next objective |
| 560 | * @param mpls determines if L3Unicast or MPLSInterface group is created |
| 561 | * @param meta metadata passed in by the application as part of the nextObjective |
| 562 | * @param useSetVlanExtension use the setVlanVid extension that has is_present bit set to 0. |
| 563 | * @return GroupInfo containing the GroupDescription of the |
| 564 | * L2Interface group(inner) and the GroupDescription of the (outer) |
| 565 | * L3Unicast/MPLSInterface group. May return null if there is an |
| 566 | * error in processing the chain |
| 567 | */ |
| 568 | protected GroupInfo createL2L3ChainInternal(TrafficTreatment treatment, int nextId, |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 569 | ApplicationId appId, boolean mpls, |
| 570 | TrafficSelector meta, boolean useSetVlanExtension) { |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 571 | // for the l2interface group, get vlan and port info |
| 572 | // for the outer group, get the src/dst mac, and vlan info |
| 573 | TrafficTreatment.Builder outerTtb = DefaultTrafficTreatment.builder(); |
| 574 | TrafficTreatment.Builder innerTtb = DefaultTrafficTreatment.builder(); |
| 575 | VlanId vlanid = null; |
| 576 | long portNum = 0; |
| 577 | boolean setVlan = false, popVlan = false; |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 578 | MacAddress srcMac; |
| 579 | MacAddress dstMac; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 580 | for (Instruction ins : treatment.allInstructions()) { |
| 581 | if (ins.type() == Instruction.Type.L2MODIFICATION) { |
| 582 | L2ModificationInstruction l2ins = (L2ModificationInstruction) ins; |
| 583 | switch (l2ins.subtype()) { |
| 584 | case ETH_DST: |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 585 | dstMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac(); |
| 586 | outerTtb.setEthDst(dstMac); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 587 | break; |
| 588 | case ETH_SRC: |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 589 | srcMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac(); |
| 590 | outerTtb.setEthSrc(srcMac); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 591 | break; |
| 592 | case VLAN_ID: |
| 593 | vlanid = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId(); |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 594 | if (useSetVlanExtension) { |
| 595 | OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid); |
| 596 | outerTtb.extension(ofdpaSetVlanVid, deviceId); |
| 597 | } else { |
| 598 | outerTtb.setVlanId(vlanid); |
| 599 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 600 | setVlan = true; |
| 601 | break; |
| 602 | case VLAN_POP: |
| 603 | innerTtb.popVlan(); |
| 604 | popVlan = true; |
| 605 | break; |
| 606 | case DEC_MPLS_TTL: |
| 607 | case MPLS_LABEL: |
| 608 | case MPLS_POP: |
| 609 | case MPLS_PUSH: |
| 610 | case VLAN_PCP: |
| 611 | case VLAN_PUSH: |
| 612 | default: |
| 613 | break; |
| 614 | } |
| 615 | } else if (ins.type() == Instruction.Type.OUTPUT) { |
| 616 | portNum = ((Instructions.OutputInstruction) ins).port().toLong(); |
| 617 | innerTtb.add(ins); |
| 618 | } else { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 619 | log.debug("Driver does not handle this type of TrafficTreatment" |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 620 | + " instruction in l2l3chain: {} - {}", ins.type(), |
| 621 | ins); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 622 | } |
| 623 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 624 | if (vlanid == null && meta != null) { |
| 625 | // use metadata if available |
Pier Ventre | 140a894 | 2016-11-02 07:26:38 -0700 | [diff] [blame] | 626 | Criterion vidCriterion = meta.getCriterion(VLAN_VID); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 627 | if (vidCriterion != null) { |
| 628 | vlanid = ((VlanIdCriterion) vidCriterion).vlanId(); |
| 629 | } |
| 630 | // if vlan is not set, use the vlan in metadata for outerTtb |
| 631 | if (vlanid != null && !setVlan) { |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 632 | if (useSetVlanExtension) { |
| 633 | OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid); |
| 634 | outerTtb.extension(ofdpaSetVlanVid, deviceId); |
| 635 | } else { |
| 636 | outerTtb.setVlanId(vlanid); |
| 637 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 638 | } |
| 639 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 640 | if (vlanid == null) { |
| 641 | log.error("Driver cannot process an L2/L3 group chain without " |
| 642 | + "egress vlan information for dev: {} port:{}", |
| 643 | deviceId, portNum); |
| 644 | return null; |
| 645 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 646 | if (!setVlan && !popVlan) { |
| 647 | // untagged outgoing port |
| 648 | TrafficTreatment.Builder temp = DefaultTrafficTreatment.builder(); |
| 649 | temp.popVlan(); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 650 | innerTtb.build().allInstructions().forEach(temp::add); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 651 | innerTtb = temp; |
| 652 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 653 | // assemble information for ofdpa l2interface group |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 654 | int l2groupId = l2GroupId(vlanid, portNum); |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 655 | // 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] | 656 | // but different for the same portnumber on different devices. Also different |
| 657 | // for the various group-types created out of the same next objective. |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 658 | int l2gk = l2InterfaceGroupKey(deviceId, vlanid, portNum); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 659 | final GroupKey l2groupkey = new DefaultGroupKey(appKryo.serialize(l2gk)); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 660 | // assemble information for outer group |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 661 | GroupDescription outerGrpDesc; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 662 | if (mpls) { |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 663 | // outer group is MPLS Interface |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 664 | int mplsInterfaceIndex = getNextAvailableIndex(); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 665 | int mplsGroupId = MPLS_INTERFACE_TYPE | (SUBTYPE_MASK & mplsInterfaceIndex); |
| 666 | final GroupKey mplsGroupKey = new DefaultGroupKey( |
| 667 | appKryo.serialize(mplsInterfaceIndex)); |
Yi Tseng | fa394de | 2017-02-01 11:26:40 -0800 | [diff] [blame] | 668 | outerTtb.group(new GroupId(l2groupId)); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 669 | // create the mpls-interface group description to wait for the |
| 670 | // l2 interface group to be processed |
| 671 | GroupBucket mplsinterfaceGroupBucket = |
| 672 | DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build()); |
| 673 | outerGrpDesc = new DefaultGroupDescription( |
| 674 | deviceId, |
| 675 | GroupDescription.Type.INDIRECT, |
| 676 | new GroupBuckets(Collections.singletonList( |
| 677 | mplsinterfaceGroupBucket)), |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 678 | mplsGroupKey, |
| 679 | mplsGroupId, |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 680 | appId); |
| 681 | log.debug("Trying MPLS-Interface: device:{} gid:{} gkey:{} nextid:{}", |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 682 | deviceId, Integer.toHexString(mplsGroupId), |
| 683 | mplsGroupKey, nextId); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 684 | } else { |
| 685 | // outer group is L3Unicast |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 686 | int l3unicastIndex = getNextAvailableIndex(); |
| 687 | int l3groupId = L3_UNICAST_TYPE | (TYPE_MASK & l3unicastIndex); |
| 688 | final GroupKey l3groupkey = new DefaultGroupKey( |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 689 | appKryo.serialize(l3unicastIndex)); |
Yi Tseng | fa394de | 2017-02-01 11:26:40 -0800 | [diff] [blame] | 690 | outerTtb.group(new GroupId(l2groupId)); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 691 | // create the l3unicast group description to wait for the |
| 692 | // l2 interface group to be processed |
| 693 | GroupBucket l3unicastGroupBucket = |
| 694 | DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build()); |
| 695 | outerGrpDesc = new DefaultGroupDescription( |
| 696 | deviceId, |
| 697 | GroupDescription.Type.INDIRECT, |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 698 | new GroupBuckets(Collections.singletonList(l3unicastGroupBucket)), |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 699 | l3groupkey, |
| 700 | l3groupId, |
| 701 | appId); |
| 702 | log.debug("Trying L3Unicast: device:{} gid:{} gkey:{} nextid:{}", |
| 703 | deviceId, Integer.toHexString(l3groupId), |
| 704 | l3groupkey, nextId); |
| 705 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 706 | // store l2groupkey with the groupChainElem for the outer-group that depends on it |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 707 | GroupChainElem gce = new GroupChainElem(outerGrpDesc, 1, false, deviceId); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 708 | updatePendingGroups(l2groupkey, gce); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 709 | // create group description for the inner l2 interface group |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 710 | GroupBucket l2InterfaceGroupBucket = |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 711 | DefaultGroupBucket.createIndirectGroupBucket(innerTtb.build()); |
| 712 | GroupDescription l2groupDescription = |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 713 | new DefaultGroupDescription(deviceId, |
| 714 | GroupDescription.Type.INDIRECT, |
| 715 | new GroupBuckets(Collections.singletonList(l2InterfaceGroupBucket)), |
| 716 | l2groupkey, |
| 717 | l2groupId, |
| 718 | appId); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 719 | log.debug("Trying L2Interface: device:{} gid:{} gkey:{} nextId:{}", |
| 720 | deviceId, Integer.toHexString(l2groupId), |
| 721 | l2groupkey, nextId); |
| 722 | return new GroupInfo(l2groupDescription, outerGrpDesc); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | /** |
| 726 | * As per the OFDPA 2.0 TTP, packets are sent out of ports by using |
| 727 | * a chain of groups. The broadcast Next Objective passed in by the application |
| 728 | * has to be broken up into a group chain comprising of an |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 729 | * 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] | 730 | * |
| 731 | * @param nextObj the nextObjective of type BROADCAST |
| 732 | */ |
| 733 | private void processBroadcastNextObjective(NextObjective nextObj) { |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 734 | VlanId assignedVlan = readVlanFromSelector(nextObj.meta()); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 735 | if (assignedVlan == null) { |
| 736 | log.warn("VLAN ID required by broadcast next obj is missing. Abort."); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 737 | fail(nextObj, ObjectiveError.BADPARAMS); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 738 | return; |
| 739 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 740 | |
jayakumarthazhath | 655b9a8 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 741 | // Handling L2 multicast cases. |
| 742 | MacAddress dstMac = readEthDstFromSelector(nextObj.meta()); |
| 743 | if (dstMac != null && dstMac.isMulticast()) { |
| 744 | processL2MulticastNextObjective(nextObj); |
Jayakumar Thazhath | 812aa76 | 2018-10-24 01:22:04 -0400 | [diff] [blame] | 745 | return; |
jayakumarthazhath | 655b9a8 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 746 | } |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 747 | |
Charles Chan | 367c1c1 | 2018-10-19 16:23:28 -0700 | [diff] [blame] | 748 | // FIXME Improve the logic |
| 749 | // If L2 load balancer is not involved, use L2IG. Otherwise, use L2UG. |
| 750 | // The purpose is to make sure existing XConnect logic can still work on a configured port. |
| 751 | List<GroupInfo> groupInfos; |
| 752 | if (nextObj.nextTreatments().stream().allMatch(n -> n.type() == NextTreatment.Type.TREATMENT)) { |
| 753 | groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan); |
| 754 | log.debug("prepareL2InterfaceGroup"); |
| 755 | } else { |
| 756 | groupInfos = prepareL2UnfilteredGroup(nextObj); |
| 757 | log.debug("prepareL2UnfilteredGroup"); |
| 758 | } |
| 759 | |
pier | 0023ca9 | 2018-11-29 10:32:40 -0800 | [diff] [blame] | 760 | if (groupInfos == null || groupInfos.isEmpty()) { |
| 761 | log.warn("No buckets for Broadcast NextObj {}", nextObj); |
| 762 | fail(nextObj, ObjectiveError.GROUPMISSING); |
| 763 | return; |
| 764 | } |
| 765 | |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 766 | IpPrefix ipDst = readIpDstFromSelector(nextObj.meta()); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 767 | if (ipDst != null) { |
| 768 | if (ipDst.isMulticast()) { |
| 769 | createL3MulticastGroup(nextObj, assignedVlan, groupInfos); |
| 770 | } else { |
| 771 | log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 772 | fail(nextObj, ObjectiveError.BADPARAMS); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 773 | } |
| 774 | } else { |
| 775 | createL2FloodGroup(nextObj, assignedVlan, groupInfos); |
| 776 | } |
| 777 | } |
| 778 | |
Charles Chan | 367c1c1 | 2018-10-19 16:23:28 -0700 | [diff] [blame] | 779 | private List<GroupInfo> prepareL2UnfilteredGroup(NextObjective nextObj) { |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 780 | ImmutableList.Builder<GroupInfo> groupInfoBuilder = ImmutableList.builder(); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 781 | // break up broadcast next objective to multiple groups |
Charles Chan | 367c1c1 | 2018-10-19 16:23:28 -0700 | [diff] [blame] | 782 | Collection<TrafficTreatment> treatments = nextObj.nextTreatments().stream() |
| 783 | .filter(nt -> nt.type() == NextTreatment.Type.TREATMENT) |
| 784 | .map(nt -> ((DefaultNextTreatment) nt).treatment()) |
| 785 | .collect(Collectors.toSet()); |
| 786 | Collection<Integer> nextIds = nextObj.nextTreatments().stream() |
| 787 | .filter(nt -> nt.type() == NextTreatment.Type.ID) |
| 788 | .map(nt -> ((IdNextTreatment) nt).nextId()) |
| 789 | .collect(Collectors.toSet()); |
| 790 | |
| 791 | // Each treatment is converted to an L2 unfiltered group |
| 792 | treatments.forEach(treatment -> { |
| 793 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 794 | // Extract port information |
| 795 | PortNumber port = treatment.allInstructions().stream() |
| 796 | .map(instr -> (Instructions.OutputInstruction) instr) |
| 797 | .map(instr -> instr.port()) |
| 798 | .findFirst().orElse(null); |
| 799 | if (port == null) { |
| 800 | log.debug("Skip bucket without output instruction"); |
| 801 | return; |
| 802 | } |
| 803 | tBuilder.setOutput(port); |
| 804 | if (requireAllowVlanTransition()) { |
| 805 | tBuilder.extension(new OfdpaSetAllowVlanTranslation(Ofdpa3AllowVlanTranslationType.ALLOW), deviceId); |
| 806 | } |
| 807 | |
| 808 | // Build L2UG |
| 809 | int l2ugk = l2UnfilteredGroupKey(deviceId, port.toLong()); |
| 810 | final GroupKey l2UnfilterGroupKey = new DefaultGroupKey(appKryo.serialize(l2ugk)); |
| 811 | int l2UnfilteredGroupId = L2_UNFILTERED_TYPE | ((int) port.toLong() & FOUR_NIBBLE_MASK); |
| 812 | GroupBucket l2UnfilteredGroupBucket = DefaultGroupBucket.createIndirectGroupBucket(tBuilder.build()); |
| 813 | GroupDescription l2UnfilteredGroupDesc = new DefaultGroupDescription(deviceId, |
| 814 | GroupDescription.Type.INDIRECT, |
| 815 | new GroupBuckets(Collections.singletonList(l2UnfilteredGroupBucket)), |
| 816 | l2UnfilterGroupKey, |
| 817 | l2UnfilteredGroupId, |
| 818 | nextObj.appId()); |
| 819 | log.debug("Trying L2-Unfiltered: device:{} gid:{} gkey:{} nextid:{}", |
| 820 | deviceId, Integer.toHexString(l2UnfilteredGroupId), l2UnfilterGroupKey, nextObj.id()); |
| 821 | groupInfoBuilder.add(new GroupInfo(l2UnfilteredGroupDesc, l2UnfilteredGroupDesc)); |
| 822 | }); |
pier | 0023ca9 | 2018-11-29 10:32:40 -0800 | [diff] [blame] | 823 | // Save the current count |
| 824 | int counts = groupInfoBuilder.build().size(); |
Charles Chan | 367c1c1 | 2018-10-19 16:23:28 -0700 | [diff] [blame] | 825 | // Lookup each nextId in the store and obtain the group information |
| 826 | nextIds.forEach(nextId -> { |
pier | 0023ca9 | 2018-11-29 10:32:40 -0800 | [diff] [blame] | 827 | NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextId); |
| 828 | if (nextGroup != null) { |
| 829 | List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(nextGroup.data()); |
| 830 | GroupKey topGroupKey = allActiveKeys.get(0).getFirst(); |
| 831 | GroupDescription groupDesc = groupService.getGroup(deviceId, topGroupKey); |
| 832 | if (groupDesc != null) { |
| 833 | log.debug("Trying L2-Hash device:{} gid:{}, gkey:{}, nextid:{}", |
| 834 | deviceId, Integer.toHexString(((Group) groupDesc).id().id()), topGroupKey, nextId); |
| 835 | groupInfoBuilder.add(new GroupInfo(groupDesc, groupDesc)); |
| 836 | } else { |
| 837 | log.error("Not found L2-Hash device:{}, gkey:{}, nextid:{}", deviceId, topGroupKey, nextId); |
| 838 | } |
| 839 | } else { |
| 840 | log.error("Not found NextGroup device:{}, nextid:{}", deviceId, nextId); |
| 841 | } |
Charles Chan | 367c1c1 | 2018-10-19 16:23:28 -0700 | [diff] [blame] | 842 | }); |
pier | 0023ca9 | 2018-11-29 10:32:40 -0800 | [diff] [blame] | 843 | // Compare the size before and after to detect problems during the creation |
| 844 | ImmutableList<GroupInfo> groupInfos = groupInfoBuilder.build(); |
| 845 | return (counts + nextIds.size()) == groupInfos.size() ? groupInfos : ImmutableList.of(); |
Charles Chan | 367c1c1 | 2018-10-19 16:23:28 -0700 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | private List<GroupInfo> prepareL2InterfaceGroup(NextObjective nextObj, VlanId assignedVlan) { |
| 849 | ImmutableList.Builder<GroupInfo> groupInfoBuilder = ImmutableList.builder(); |
| 850 | // break up broadcast next objective to multiple groups |
| 851 | Collection<TrafficTreatment> buckets = nextObj.nextTreatments().stream() |
| 852 | .filter(nt -> nt.type() == NextTreatment.Type.TREATMENT) |
| 853 | .map(nt -> ((DefaultNextTreatment) nt).treatment()) |
| 854 | .collect(Collectors.toSet()); |
| 855 | |
| 856 | // Each treatment is converted to an L2 interface group |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 857 | for (TrafficTreatment treatment : buckets) { |
| 858 | TrafficTreatment.Builder newTreatment = DefaultTrafficTreatment.builder(); |
| 859 | PortNumber portNum = null; |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 860 | VlanId egressVlan = null; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 861 | // ensure that the only allowed treatments are pop-vlan and output |
| 862 | for (Instruction ins : treatment.allInstructions()) { |
| 863 | if (ins.type() == Instruction.Type.L2MODIFICATION) { |
| 864 | L2ModificationInstruction l2ins = (L2ModificationInstruction) ins; |
| 865 | switch (l2ins.subtype()) { |
| 866 | case VLAN_POP: |
| 867 | newTreatment.add(l2ins); |
| 868 | break; |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 869 | case VLAN_ID: |
| 870 | egressVlan = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId(); |
| 871 | break; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 872 | default: |
| 873 | log.debug("action {} not permitted for broadcast nextObj", |
| 874 | l2ins.subtype()); |
| 875 | break; |
| 876 | } |
| 877 | } else if (ins.type() == Instruction.Type.OUTPUT) { |
| 878 | portNum = ((Instructions.OutputInstruction) ins).port(); |
| 879 | newTreatment.add(ins); |
| 880 | } else { |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 881 | log.debug("TrafficTreatment of type {} not permitted in " + |
| 882 | " broadcast nextObjective", ins.type()); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 883 | } |
| 884 | } |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 885 | if (portNum == null) { |
Ruchi Sahota | ef0761c | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 886 | log.debug("Can't find output port for the bucket {}.", treatment); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 887 | continue; |
| 888 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 889 | // assemble info for l2 interface group |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 890 | VlanId l2InterfaceGroupVlan = |
| 891 | (egressVlan != null && !assignedVlan.equals(egressVlan)) ? |
| 892 | egressVlan : assignedVlan; |
| 893 | int l2gk = l2InterfaceGroupKey(deviceId, l2InterfaceGroupVlan, portNum.toLong()); |
| 894 | final GroupKey l2InterfaceGroupKey = |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 895 | new DefaultGroupKey(appKryo.serialize(l2gk)); |
Charles Chan | 372b63e | 2017-02-07 12:10:53 -0800 | [diff] [blame] | 896 | int l2InterfaceGroupId = L2_INTERFACE_TYPE | |
Charles Chan | 367c1c1 | 2018-10-19 16:23:28 -0700 | [diff] [blame] | 897 | ((l2InterfaceGroupVlan.toShort() & THREE_NIBBLE_MASK) << PORT_LEN) | |
| 898 | ((int) portNum.toLong() & FOUR_NIBBLE_MASK); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 899 | GroupBucket l2InterfaceGroupBucket = |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 900 | DefaultGroupBucket.createIndirectGroupBucket(newTreatment.build()); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 901 | GroupDescription l2InterfaceGroupDescription = |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 902 | new DefaultGroupDescription(deviceId, |
| 903 | GroupDescription.Type.INDIRECT, |
| 904 | new GroupBuckets(Collections.singletonList( |
| 905 | l2InterfaceGroupBucket)), |
| 906 | l2InterfaceGroupKey, |
| 907 | l2InterfaceGroupId, |
| 908 | nextObj.appId()); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 909 | log.debug("Trying L2-Interface: device:{} gid:{} gkey:{} nextid:{}", |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 910 | deviceId, Integer.toHexString(l2InterfaceGroupId), |
| 911 | l2InterfaceGroupKey, nextObj.id()); |
Ruchi Sahota | ef0761c | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 912 | |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 913 | groupInfoBuilder.add(new GroupInfo(l2InterfaceGroupDescription, |
| 914 | l2InterfaceGroupDescription)); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 915 | } |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 916 | return groupInfoBuilder.build(); |
| 917 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 918 | |
Ruchi Sahota | ef0761c | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 919 | private GroupInfo prepareL3UnicastGroup(NextObjective nextObj, NextGroup next) { |
| 920 | |
| 921 | ImmutableList.Builder<GroupInfo> groupInfoBuilder = ImmutableList.builder(); |
| 922 | TrafficTreatment treatment = nextObj.next().iterator().next(); |
| 923 | |
| 924 | VlanId assignedVlan = readVlanFromSelector(nextObj.meta()); |
| 925 | if (assignedVlan == null) { |
| 926 | log.warn("VLAN ID required by next obj is missing. Abort."); |
| 927 | return null; |
| 928 | } |
| 929 | |
| 930 | List<GroupInfo> l2GroupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan); |
| 931 | GroupDescription l2InterfaceGroupDesc = l2GroupInfos.get(0).innerMostGroupDesc(); |
| 932 | GroupKey l2groupkey = l2InterfaceGroupDesc.appCookie(); |
| 933 | |
| 934 | TrafficTreatment.Builder outerTtb = DefaultTrafficTreatment.builder(); |
| 935 | VlanId vlanid = null; |
| 936 | MacAddress srcMac; |
| 937 | MacAddress dstMac; |
| 938 | for (Instruction ins : treatment.allInstructions()) { |
| 939 | if (ins.type() == Instruction.Type.L2MODIFICATION) { |
| 940 | L2ModificationInstruction l2ins = (L2ModificationInstruction) ins; |
| 941 | switch (l2ins.subtype()) { |
| 942 | case ETH_DST: |
| 943 | dstMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac(); |
| 944 | outerTtb.setEthDst(dstMac); |
| 945 | break; |
| 946 | case ETH_SRC: |
| 947 | srcMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac(); |
| 948 | outerTtb.setEthSrc(srcMac); |
| 949 | break; |
| 950 | case VLAN_ID: |
| 951 | vlanid = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId(); |
| 952 | outerTtb.setVlanId(vlanid); |
| 953 | break; |
| 954 | default: |
| 955 | break; |
| 956 | } |
| 957 | } else { |
| 958 | log.debug("Driver does not handle this type of TrafficTreatment" |
| 959 | + " instruction in l2l3chain: {} - {}", ins.type(), ins); |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | GroupId l2groupId = new GroupId(l2InterfaceGroupDesc.givenGroupId()); |
| 964 | outerTtb.group(l2groupId); |
| 965 | |
| 966 | // we need the top level group's key to point the flow to it |
| 967 | List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data()); |
| 968 | GroupKey l3groupkey = gkeys.get(0).peekFirst(); |
| 969 | GroupId grpId = groupService.getGroup(deviceId, l3groupkey).id(); |
| 970 | int l3groupId = grpId.id(); |
| 971 | |
| 972 | // create the l3unicast group description to wait for the |
| 973 | // l2 interface group to be processed |
| 974 | GroupBucket l3UnicastGroupBucket = DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build()); |
| 975 | |
| 976 | GroupDescription l3UnicastGroupDescription = new DefaultGroupDescription(deviceId, |
| 977 | GroupDescription.Type.INDIRECT, |
| 978 | new GroupBuckets(Collections.singletonList( |
| 979 | l3UnicastGroupBucket)), l3groupkey, |
| 980 | l3groupId, nextObj.appId()); |
| 981 | |
| 982 | // store l2groupkey with the groupChainElem for the outer-group that depends on it |
| 983 | GroupChainElem gce = new GroupChainElem(l3UnicastGroupDescription, 1, false, deviceId); |
| 984 | updatePendingGroups(l2groupkey, gce); |
| 985 | |
| 986 | log.debug("Trying L3-Interface: device:{} gid:{} gkey:{} nextid:{}", |
| 987 | deviceId, Integer.toHexString(l3groupId), l3groupkey, nextObj.id()); |
| 988 | |
| 989 | groupInfoBuilder.add(new GroupInfo(l2InterfaceGroupDesc, |
| 990 | l3UnicastGroupDescription)); |
| 991 | |
| 992 | return groupInfoBuilder.build().iterator().next(); |
| 993 | } |
| 994 | |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 995 | private void createL2FloodGroup(NextObjective nextObj, VlanId vlanId, |
| 996 | List<GroupInfo> groupInfos) { |
| 997 | // assemble info for l2 flood group. Since there can be only one flood |
| 998 | // group for a vlan, its index is always the same - 0 |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 999 | Integer l2FloodGroupId = L2_FLOOD_TYPE | (vlanId.toShort() << 16); |
| 1000 | final GroupKey l2FloodGroupKey = l2FloodGroupKey(vlanId, deviceId); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1001 | // collection of group buckets pointing to all the l2 interface groups |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1002 | List<GroupBucket> l2floodBuckets = generateNextGroupBuckets(groupInfos, ALL); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1003 | // create the l2flood group-description to wait for all the |
| 1004 | // l2interface groups to be processed |
| 1005 | GroupDescription l2floodGroupDescription = |
| 1006 | new DefaultGroupDescription( |
| 1007 | deviceId, |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1008 | ALL, |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1009 | new GroupBuckets(l2floodBuckets), |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1010 | l2FloodGroupKey, |
| 1011 | l2FloodGroupId, |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1012 | nextObj.appId()); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1013 | log.debug("Trying L2-Flood: device:{} gid:{} gkey:{} nextid:{}", |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1014 | deviceId, Integer.toHexString(l2FloodGroupId), |
| 1015 | l2FloodGroupKey, nextObj.id()); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 1016 | // Put all dependency information into allGroupKeys |
| 1017 | List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList(); |
| 1018 | groupInfos.forEach(groupInfo -> { |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1019 | Deque<GroupKey> groupKeyChain = new ArrayDeque<>(); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 1020 | // In this case we should have L2 interface group only |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1021 | groupKeyChain.addFirst(groupInfo.nextGroupDesc().appCookie()); |
| 1022 | groupKeyChain.addFirst(l2FloodGroupKey); |
| 1023 | allGroupKeys.add(groupKeyChain); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 1024 | }); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 1025 | // Point the next objective to this group |
| 1026 | OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1027 | updatePendingNextObjective(l2FloodGroupKey, ofdpaGrp); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 1028 | GroupChainElem gce = new GroupChainElem(l2floodGroupDescription, |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1029 | groupInfos.size(), false, deviceId); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 1030 | groupInfos.forEach(groupInfo -> { |
| 1031 | // Point this group to the next group |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1032 | updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), gce); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 1033 | // Start installing the inner-most group |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1034 | groupService.addGroup(groupInfo.innerMostGroupDesc()); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 1035 | }); |
| 1036 | } |
| 1037 | |
Pier Luigi | 21fffd2 | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 1038 | private void createL3MulticastGroup(NextObjective nextObj, VlanId vlanId, |
| 1039 | List<GroupInfo> groupInfos) { |
| 1040 | // Let's create a new list mcast buckets |
| 1041 | List<GroupBucket> l3McastBuckets = createL3MulticastBucket(groupInfos); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 1042 | |
| 1043 | int l3MulticastIndex = getNextAvailableIndex(); |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1044 | int l3MulticastGroupId = L3_MULTICAST_TYPE | |
| 1045 | vlanId.toShort() << 16 | (TYPE_VLAN_MASK & l3MulticastIndex); |
| 1046 | final GroupKey l3MulticastGroupKey = |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1047 | new DefaultGroupKey(appKryo.serialize(l3MulticastIndex)); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 1048 | |
| 1049 | GroupDescription l3MulticastGroupDesc = new DefaultGroupDescription(deviceId, |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1050 | ALL, |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 1051 | new GroupBuckets(l3McastBuckets), |
| 1052 | l3MulticastGroupKey, |
| 1053 | l3MulticastGroupId, |
| 1054 | nextObj.appId()); |
| 1055 | |
| 1056 | // Put all dependency information into allGroupKeys |
| 1057 | List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList(); |
| 1058 | groupInfos.forEach(groupInfo -> { |
| 1059 | Deque<GroupKey> gkeyChain = new ArrayDeque<>(); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1060 | gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie()); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 1061 | // Add L3 interface group to the chain if there is one. |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1062 | if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) { |
| 1063 | gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie()); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 1064 | } |
| 1065 | gkeyChain.addFirst(l3MulticastGroupKey); |
| 1066 | allGroupKeys.add(gkeyChain); |
| 1067 | }); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 1068 | // Point the next objective to this group |
| 1069 | OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj); |
| 1070 | updatePendingNextObjective(l3MulticastGroupKey, ofdpaGrp); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 1071 | GroupChainElem outerGce = new GroupChainElem(l3MulticastGroupDesc, |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1072 | groupInfos.size(), false, deviceId); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 1073 | groupInfos.forEach(groupInfo -> { |
| 1074 | // Point this group (L3 multicast) to the next group |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1075 | updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), outerGce); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 1076 | // Point next group to inner-most group, if any |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1077 | if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) { |
| 1078 | GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc(), |
| 1079 | 1, false, deviceId); |
| 1080 | updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), innerGce); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 1081 | } |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 1082 | // Start installing the inner-most group |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1083 | groupService.addGroup(groupInfo.innerMostGroupDesc()); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 1084 | }); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1085 | } |
| 1086 | |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1087 | /** |
| 1088 | * As per the OFDPA 2.0 TTP, packets are sent out of ports by using |
| 1089 | * a chain of groups. The hashed Next Objective passed in by the application |
| 1090 | * has to be broken up into a group chain comprising of an |
| 1091 | * L3 ECMP group as the top level group. Buckets of this group can point |
| 1092 | * to a variety of groups in a group chain, depending on the whether |
| 1093 | * MPLS labels are being pushed or not. |
| 1094 | * <p> |
| 1095 | * NOTE: We do not create MPLS ECMP groups as they are unimplemented in |
| 1096 | * OF-DPA 2.0 (even though it is in the spec). Therefore we do not |
| 1097 | * check the nextObjective meta to see what is matching before being |
| 1098 | * sent to this nextObjective. |
| 1099 | * |
| 1100 | * @param nextObj the nextObjective of type HASHED |
| 1101 | */ |
Charles Chan | 367c1c1 | 2018-10-19 16:23:28 -0700 | [diff] [blame] | 1102 | protected void processEcmpHashedNextObjective(NextObjective nextObj) { |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1103 | // storage for all group keys in the chain of groups created |
| 1104 | List<Deque<GroupKey>> allGroupKeys = new ArrayList<>(); |
| 1105 | List<GroupInfo> unsentGroups = new ArrayList<>(); |
Charles Chan | 367c1c1 | 2018-10-19 16:23:28 -0700 | [diff] [blame] | 1106 | createEcmpHashBucketChains(nextObj, allGroupKeys, unsentGroups); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1107 | // now we can create the outermost L3 ECMP group |
| 1108 | List<GroupBucket> l3ecmpGroupBuckets = new ArrayList<>(); |
| 1109 | for (GroupInfo gi : unsentGroups) { |
| 1110 | // create ECMP bucket to point to the outer group |
| 1111 | TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder(); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1112 | ttb.group(new GroupId(gi.nextGroupDesc().givenGroupId())); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1113 | GroupBucket sbucket = DefaultGroupBucket |
| 1114 | .createSelectGroupBucket(ttb.build()); |
| 1115 | l3ecmpGroupBuckets.add(sbucket); |
| 1116 | } |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1117 | int l3ecmpIndex = getNextAvailableIndex(); |
| 1118 | int l3ecmpGroupId = L3_ECMP_TYPE | (TYPE_MASK & l3ecmpIndex); |
| 1119 | GroupKey l3ecmpGroupKey = new DefaultGroupKey( |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1120 | appKryo.serialize(l3ecmpIndex)); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1121 | GroupDescription l3ecmpGroupDesc = |
| 1122 | new DefaultGroupDescription( |
| 1123 | deviceId, |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1124 | SELECT, |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1125 | new GroupBuckets(l3ecmpGroupBuckets), |
| 1126 | l3ecmpGroupKey, |
| 1127 | l3ecmpGroupId, |
| 1128 | nextObj.appId()); |
| 1129 | GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc, |
| 1130 | l3ecmpGroupBuckets.size(), |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1131 | false, deviceId); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1132 | |
| 1133 | // create objects for local and distributed storage |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1134 | allGroupKeys.forEach(gKeyChain -> gKeyChain.addFirst(l3ecmpGroupKey)); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1135 | OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1136 | // store l3ecmpGroupKey with the ofdpaGroupChain for the nextObjective |
| 1137 | // that depends on it |
| 1138 | updatePendingNextObjective(l3ecmpGroupKey, ofdpaGrp); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1139 | log.debug("Trying L3ECMP: device:{} gid:{} gkey:{} nextId:{}", |
| 1140 | deviceId, Integer.toHexString(l3ecmpGroupId), |
| 1141 | l3ecmpGroupKey, nextObj.id()); |
| 1142 | // finally we are ready to send the innermost groups |
| 1143 | for (GroupInfo gi : unsentGroups) { |
| 1144 | log.debug("Sending innermost group {} in group chain on device {} ", |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1145 | Integer.toHexString(gi.innerMostGroupDesc().givenGroupId()), deviceId); |
| 1146 | updatePendingGroups(gi.nextGroupDesc().appCookie(), l3ecmpGce); |
| 1147 | groupService.addGroup(gi.innerMostGroupDesc()); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1148 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | /** |
| 1152 | * Creates group chains for all buckets in a hashed group, and stores the |
| 1153 | * GroupInfos and GroupKeys for all the groups in the lists passed in, which |
| 1154 | * should be empty. |
| 1155 | * <p> |
| 1156 | * Does not create the top level ECMP group. Does not actually send the |
| 1157 | * groups to the groupService. |
| 1158 | * |
| 1159 | * @param nextObj the Next Objective with buckets that need to be converted |
| 1160 | * to group chains |
| 1161 | * @param allGroupKeys a list to store groupKey for each bucket-group-chain |
| 1162 | * @param unsentGroups a list to store GroupInfo for each bucket-group-chain |
| 1163 | */ |
Charles Chan | 367c1c1 | 2018-10-19 16:23:28 -0700 | [diff] [blame] | 1164 | protected void createEcmpHashBucketChains(NextObjective nextObj, |
| 1165 | List<Deque<GroupKey>> allGroupKeys, |
| 1166 | List<GroupInfo> unsentGroups) { |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1167 | // break up hashed next objective to multiple groups |
| 1168 | Collection<TrafficTreatment> buckets = nextObj.next(); |
| 1169 | |
| 1170 | for (TrafficTreatment bucket : buckets) { |
| 1171 | //figure out how many labels are pushed in each bucket |
| 1172 | int labelsPushed = 0; |
| 1173 | MplsLabel innermostLabel = null; |
| 1174 | for (Instruction ins : bucket.allInstructions()) { |
| 1175 | if (ins.type() == Instruction.Type.L2MODIFICATION) { |
| 1176 | L2ModificationInstruction l2ins = (L2ModificationInstruction) ins; |
| 1177 | if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_PUSH) { |
| 1178 | labelsPushed++; |
| 1179 | } |
| 1180 | if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_LABEL) { |
| 1181 | if (innermostLabel == null) { |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1182 | innermostLabel = |
| 1183 | ((L2ModificationInstruction.ModMplsLabelInstruction) l2ins).label(); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1184 | } |
| 1185 | } |
| 1186 | } |
| 1187 | } |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1188 | Deque<GroupKey> gKeyChain = new ArrayDeque<>(); |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 1189 | // here we only deal with 0 and 1 label push |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1190 | if (labelsPushed == 0) { |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1191 | GroupInfo noLabelGroupInfo; |
Pier Ventre | 140a894 | 2016-11-02 07:26:38 -0700 | [diff] [blame] | 1192 | TrafficSelector metaSelector = nextObj.meta(); |
| 1193 | if (metaSelector != null) { |
| 1194 | if (isNotMplsBos(metaSelector)) { |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1195 | noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(), |
| 1196 | nextObj.appId(), true, |
| 1197 | nextObj.meta()); |
Pier Ventre | 140a894 | 2016-11-02 07:26:38 -0700 | [diff] [blame] | 1198 | } else { |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1199 | noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(), |
| 1200 | nextObj.appId(), false, |
| 1201 | nextObj.meta()); |
Pier Ventre | 140a894 | 2016-11-02 07:26:38 -0700 | [diff] [blame] | 1202 | } |
| 1203 | } else { |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1204 | noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(), |
| 1205 | nextObj.appId(), false, |
| 1206 | nextObj.meta()); |
Pier Ventre | 140a894 | 2016-11-02 07:26:38 -0700 | [diff] [blame] | 1207 | } |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1208 | if (noLabelGroupInfo == null) { |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1209 | log.error("Could not process nextObj={} in dev:{}", |
| 1210 | nextObj.id(), deviceId); |
| 1211 | return; |
| 1212 | } |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1213 | gKeyChain.addFirst(noLabelGroupInfo.innerMostGroupDesc().appCookie()); |
| 1214 | gKeyChain.addFirst(noLabelGroupInfo.nextGroupDesc().appCookie()); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1215 | // we can't send the inner group description yet, as we have to |
| 1216 | // create the dependent ECMP group first. So we store.. |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1217 | unsentGroups.add(noLabelGroupInfo); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1218 | } else if (labelsPushed == 1) { |
| 1219 | GroupInfo onelabelGroupInfo = createL2L3Chain(bucket, nextObj.id(), |
| 1220 | nextObj.appId(), true, |
| 1221 | nextObj.meta()); |
| 1222 | if (onelabelGroupInfo == null) { |
| 1223 | log.error("Could not process nextObj={} in dev:{}", |
| 1224 | nextObj.id(), deviceId); |
| 1225 | return; |
| 1226 | } |
| 1227 | // we need to add another group to this chain - the L3VPN group |
| 1228 | TrafficTreatment.Builder l3vpnTtb = DefaultTrafficTreatment.builder(); |
Charles Chan | 0f43e47 | 2017-02-14 14:00:16 -0800 | [diff] [blame] | 1229 | if (requireVlanPopBeforeMplsPush()) { |
| 1230 | l3vpnTtb.popVlan(); |
| 1231 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1232 | l3vpnTtb.pushMpls() |
| 1233 | .setMpls(innermostLabel) |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1234 | .group(new GroupId(onelabelGroupInfo.nextGroupDesc().givenGroupId())); |
Charles Chan | 40132b3 | 2017-01-22 00:19:37 -0800 | [diff] [blame] | 1235 | if (supportCopyTtl()) { |
| 1236 | l3vpnTtb.copyTtlOut(); |
| 1237 | } |
| 1238 | if (supportSetMplsBos()) { |
| 1239 | l3vpnTtb.setMplsBos(true); |
| 1240 | } |
Charles Chan | 0f43e47 | 2017-02-14 14:00:16 -0800 | [diff] [blame] | 1241 | if (requireVlanPopBeforeMplsPush()) { |
| 1242 | l3vpnTtb.pushVlan().setVlanId(VlanId.vlanId(VlanId.RESERVED)); |
| 1243 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1244 | GroupBucket l3vpnGrpBkt = |
| 1245 | DefaultGroupBucket.createIndirectGroupBucket(l3vpnTtb.build()); |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1246 | int l3vpnIndex = getNextAvailableIndex(); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1247 | int l3vpnGroupId = MPLS_L3VPN_SUBTYPE | (SUBTYPE_MASK & l3vpnIndex); |
| 1248 | GroupKey l3vpnGroupKey = new DefaultGroupKey( |
| 1249 | appKryo.serialize(l3vpnIndex)); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1250 | GroupDescription l3vpnGroupDesc = |
| 1251 | new DefaultGroupDescription( |
| 1252 | deviceId, |
| 1253 | GroupDescription.Type.INDIRECT, |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1254 | new GroupBuckets(Collections.singletonList(l3vpnGrpBkt)), |
| 1255 | l3vpnGroupKey, |
| 1256 | l3vpnGroupId, |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1257 | nextObj.appId()); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1258 | GroupChainElem l3vpnGce = new GroupChainElem(l3vpnGroupDesc, |
| 1259 | 1, |
| 1260 | false, |
| 1261 | deviceId); |
| 1262 | updatePendingGroups(onelabelGroupInfo.nextGroupDesc().appCookie(), l3vpnGce); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1263 | |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1264 | gKeyChain.addFirst(onelabelGroupInfo.innerMostGroupDesc().appCookie()); |
| 1265 | gKeyChain.addFirst(onelabelGroupInfo.nextGroupDesc().appCookie()); |
| 1266 | gKeyChain.addFirst(l3vpnGroupKey); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1267 | //now we can replace the outerGrpDesc with the one we just created |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1268 | onelabelGroupInfo.nextGroupDesc(l3vpnGroupDesc); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1269 | // we can't send the innermost group yet, as we have to create |
| 1270 | // the dependent ECMP group first. So we store ... |
| 1271 | unsentGroups.add(onelabelGroupInfo); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1272 | log.debug("Trying L3VPN: device:{} gid:{} group key:{} nextId:{}", |
| 1273 | deviceId, Integer.toHexString(l3vpnGroupId), |
| 1274 | l3vpnGroupKey, nextObj.id()); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1275 | } else { |
| 1276 | log.warn("Driver currently does not handle more than 1 MPLS " |
| 1277 | + "labels. Not processing nextObjective {}", nextObj.id()); |
| 1278 | return; |
| 1279 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1280 | // all groups in this chain |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1281 | allGroupKeys.add(gKeyChain); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1282 | } |
| 1283 | } |
| 1284 | |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1285 | /** |
Charles Chan | 367c1c1 | 2018-10-19 16:23:28 -0700 | [diff] [blame] | 1286 | * Create L2 hash group. |
| 1287 | * |
| 1288 | * @param nextObj next objective |
| 1289 | */ |
| 1290 | private void processL2HashedNextObjective(NextObjective nextObj) { |
| 1291 | int l2LbIndex = Optional.ofNullable(nextObj.meta().getCriterion(IN_PORT)) |
| 1292 | .map(c -> (PortCriterion) c).map(PortCriterion::port).map(PortNumber::toLong).map(Long::intValue) |
| 1293 | .orElse(-1); |
| 1294 | if (l2LbIndex == -1) { |
| 1295 | log.warn("l2LbIndex is not found in the meta of L2 hash objective. Abort"); |
| 1296 | return; |
| 1297 | } |
| 1298 | |
| 1299 | // Storage for all group keys in the chain of groups created |
| 1300 | List<Deque<GroupKey>> allGroupKeys = new ArrayList<>(); |
| 1301 | List<GroupInfo> unsentGroups = new ArrayList<>(); |
| 1302 | createL2HashBuckets(nextObj, allGroupKeys, unsentGroups); |
| 1303 | |
| 1304 | // Create L2 load balancing group |
| 1305 | List<GroupBucket> l2LbGroupBuckets = new ArrayList<>(); |
| 1306 | for (GroupInfo gi : unsentGroups) { |
| 1307 | // create load balancing bucket to point to the outer group |
| 1308 | TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder(); |
| 1309 | ttb.group(new GroupId(gi.nextGroupDesc().givenGroupId())); |
| 1310 | GroupBucket sbucket = DefaultGroupBucket.createSelectGroupBucket(ttb.build()); |
| 1311 | l2LbGroupBuckets.add(sbucket); |
| 1312 | } |
| 1313 | |
| 1314 | int l2LbGroupId = L2_LB_TYPE | (TYPE_MASK & l2LbIndex); |
| 1315 | int l2lbgk = l2HashGroupKey(deviceId, l2LbIndex); |
| 1316 | GroupKey l2LbGroupKey = new DefaultGroupKey(appKryo.serialize(l2lbgk)); |
| 1317 | GroupDescription l2LbGroupDesc = |
| 1318 | new DefaultGroupDescription( |
| 1319 | deviceId, |
| 1320 | SELECT, |
| 1321 | new GroupBuckets(l2LbGroupBuckets), |
| 1322 | l2LbGroupKey, |
| 1323 | l2LbGroupId, |
| 1324 | nextObj.appId()); |
| 1325 | GroupChainElem l2LbGce = new GroupChainElem(l2LbGroupDesc, l2LbGroupBuckets.size(), false, deviceId); |
| 1326 | |
| 1327 | // Create objects for local and distributed storage |
| 1328 | allGroupKeys.forEach(gKeyChain -> gKeyChain.addFirst(l2LbGroupKey)); |
| 1329 | OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj); |
| 1330 | // Store l2LbGroupKey with the ofdpaGroupChain for the nextObjective that depends on it |
| 1331 | updatePendingNextObjective(l2LbGroupKey, ofdpaGrp); |
| 1332 | log.debug("Trying L2-LB: device:{} gid:{} gkey:{} nextId:{}", |
| 1333 | deviceId, Integer.toHexString(l2LbGroupId), l2LbGroupKey, nextObj.id()); |
| 1334 | // finally we are ready to send the innermost groups |
| 1335 | for (GroupInfo gi : unsentGroups) { |
| 1336 | log.debug("Sending innermost group {} in group chain on device {} ", |
| 1337 | Integer.toHexString(gi.innerMostGroupDesc().givenGroupId()), deviceId); |
| 1338 | updatePendingGroups(gi.nextGroupDesc().appCookie(), l2LbGce); |
| 1339 | groupService.addGroup(gi.innerMostGroupDesc()); |
| 1340 | } |
| 1341 | } |
| 1342 | |
| 1343 | /** |
| 1344 | * Create L2 hash group buckets. |
| 1345 | * |
| 1346 | * @param nextObj next objective |
| 1347 | */ |
| 1348 | private void createL2HashBuckets(NextObjective nextObj, |
| 1349 | List<Deque<GroupKey>> allGroupKeys, List<GroupInfo> unsentGroups) { |
| 1350 | List<GroupInfo> groupInfos = prepareL2UnfilteredGroup(nextObj); |
| 1351 | groupInfos.forEach(groupInfo -> { |
| 1352 | // Update allGroupKeys |
| 1353 | Deque<GroupKey> gKeyChain = new ArrayDeque<>(); |
| 1354 | gKeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie()); |
| 1355 | allGroupKeys.add(gKeyChain); |
| 1356 | |
| 1357 | // Update unsent group list |
| 1358 | unsentGroups.add(groupInfo); |
| 1359 | }); |
| 1360 | } |
| 1361 | |
| 1362 | /** |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1363 | * Processes the pseudo wire related next objective. |
| 1364 | * This procedure try to reuse the mpls label groups, |
| 1365 | * the mpls interface group and the l2 interface group. |
| 1366 | * |
| 1367 | * @param nextObjective the objective to process. |
| 1368 | */ |
| 1369 | protected void processPwNextObjective(NextObjective nextObjective) { |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 1370 | log.warn("Pseudo wire extensions are not supported in OFDPA 2.0 {}", |
| 1371 | nextObjective.id()); |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1372 | } |
| 1373 | |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1374 | ////////////////////////////////////// |
| 1375 | // Group Editing |
| 1376 | ////////////////////////////////////// |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1377 | /** |
| 1378 | * 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] | 1379 | * Ensures that bucket being added is not a duplicate, by checking existing |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1380 | * buckets for the same output port. |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1381 | * |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1382 | * @param nextObjective the bucket information for a next group |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1383 | * @param next the representation of the existing group-chain for this next objective |
| 1384 | */ |
| 1385 | protected void addBucketToGroup(NextObjective nextObjective, NextGroup next) { |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1386 | if (nextObjective.type() != NextObjective.Type.HASHED && |
| 1387 | nextObjective.type() != NextObjective.Type.BROADCAST) { |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1388 | log.warn("AddBuckets not applied to nextType:{} in dev:{} for next:{}", |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1389 | nextObjective.type(), deviceId, nextObjective.id()); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1390 | fail(nextObjective, ObjectiveError.UNSUPPORTED); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1391 | return; |
| 1392 | } |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1393 | // first check to see if bucket being added is not a duplicate of an |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1394 | // existing bucket. If it is for an existing output port, then its a |
| 1395 | // duplicate. |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1396 | Set<TrafficTreatment> duplicateBuckets = Sets.newHashSet(); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1397 | List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data()); |
| 1398 | Set<PortNumber> existingPorts = getExistingOutputPorts(allActiveKeys, |
| 1399 | groupService, |
| 1400 | deviceId); |
Yi Tseng | 47f82dc | 2017-03-05 22:48:39 -0800 | [diff] [blame] | 1401 | Set<TrafficTreatment> nonDuplicateBuckets = Sets.newHashSet(); |
| 1402 | NextObjective objectiveToAdd; |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1403 | nextObjective.next().forEach(trafficTreatment -> { |
| 1404 | PortNumber portNumber = readOutPortFromTreatment(trafficTreatment); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1405 | if (portNumber == null) { |
| 1406 | return; |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1407 | } |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1408 | if (existingPorts.contains(portNumber)) { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1409 | // its possible that portnumbers are same but labels are different |
| 1410 | int label = readLabelFromTreatment(trafficTreatment); |
| 1411 | if (label == -1) { |
| 1412 | duplicateBuckets.add(trafficTreatment); |
| 1413 | } else { |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1414 | List<Integer> existing = existingPortAndLabel(allActiveKeys, |
| 1415 | groupService, deviceId, |
| 1416 | portNumber, label); |
| 1417 | if (!existing.isEmpty()) { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1418 | duplicateBuckets.add(trafficTreatment); |
| 1419 | } else { |
| 1420 | nonDuplicateBuckets.add(trafficTreatment); |
| 1421 | } |
| 1422 | } |
Yi Tseng | 47f82dc | 2017-03-05 22:48:39 -0800 | [diff] [blame] | 1423 | } else { |
| 1424 | nonDuplicateBuckets.add(trafficTreatment); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1425 | } |
| 1426 | }); |
Saurav Das | b28d5dd | 2017-03-24 19:03:58 -0700 | [diff] [blame] | 1427 | if (duplicateBuckets.isEmpty()) { |
| 1428 | // use the original objective |
| 1429 | objectiveToAdd = nextObjective; |
| 1430 | } else if (!nonDuplicateBuckets.isEmpty()) { |
| 1431 | // only use the non-duplicate buckets if there are any |
| 1432 | log.debug("Some buckets {} already exist in next id {}, duplicate " |
| 1433 | + "buckets will be ignored.", duplicateBuckets, nextObjective.id()); |
| 1434 | // new next objective with non duplicate treatments |
Yi Tseng | 47f82dc | 2017-03-05 22:48:39 -0800 | [diff] [blame] | 1435 | NextObjective.Builder builder = DefaultNextObjective.builder() |
| 1436 | .withType(nextObjective.type()) |
| 1437 | .withId(nextObjective.id()) |
| 1438 | .withMeta(nextObjective.meta()) |
| 1439 | .fromApp(nextObjective.appId()); |
| 1440 | nonDuplicateBuckets.forEach(builder::addTreatment); |
Yi Tseng | 47f82dc | 2017-03-05 22:48:39 -0800 | [diff] [blame] | 1441 | ObjectiveContext context = nextObjective.context().orElse(null); |
| 1442 | objectiveToAdd = builder.addToExisting(context); |
| 1443 | } else { |
Saurav Das | b28d5dd | 2017-03-24 19:03:58 -0700 | [diff] [blame] | 1444 | // buckets to add are already there - nothing to do |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 1445 | log.debug("buckets already exist {} in next: {} ..ignoring bucket add", |
| 1446 | duplicateBuckets, nextObjective.id()); |
| 1447 | pass(nextObjective); |
Saurav Das | b28d5dd | 2017-03-24 19:03:58 -0700 | [diff] [blame] | 1448 | return; |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1449 | } |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1450 | if (nextObjective.type() == NextObjective.Type.HASHED) { |
pier | cd2a3ef | 2018-11-22 15:44:57 +0100 | [diff] [blame] | 1451 | if (isL2Hash(nextObjective)) { |
| 1452 | addBucketToL2HashGroup(objectiveToAdd, allActiveKeys); |
| 1453 | return; |
| 1454 | } |
| 1455 | addBucketToEcmpHashGroup(objectiveToAdd, allActiveKeys); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1456 | } else if (nextObjective.type() == NextObjective.Type.BROADCAST) { |
Yi Tseng | 47f82dc | 2017-03-05 22:48:39 -0800 | [diff] [blame] | 1457 | addBucketToBroadcastGroup(objectiveToAdd, allActiveKeys); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1458 | } |
| 1459 | } |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1460 | |
pier | cd2a3ef | 2018-11-22 15:44:57 +0100 | [diff] [blame] | 1461 | private void addBucketToL2HashGroup(NextObjective nextObjective, |
| 1462 | List<Deque<GroupKey>> allActiveKeys) { |
| 1463 | // storage for all group keys in the chain of groups created |
| 1464 | List<Deque<GroupKey>> allGroupKeys = new ArrayList<>(); |
| 1465 | List<GroupInfo> unsentGroups = new ArrayList<>(); |
| 1466 | List<GroupBucket> newBuckets; |
| 1467 | // Prepare the l2 unfiltered groups |
| 1468 | createL2HashBuckets(nextObjective, allGroupKeys, unsentGroups); |
| 1469 | // now we can create the buckets to add to the outermost L2 hash group |
| 1470 | newBuckets = generateNextGroupBuckets(unsentGroups, SELECT); |
| 1471 | // retrieve the original l2 load balance group |
| 1472 | Group l2hashGroup = retrieveTopLevelGroup(allActiveKeys, deviceId, |
| 1473 | groupService, nextObjective.id()); |
| 1474 | if (l2hashGroup == null) { |
| 1475 | fail(nextObjective, ObjectiveError.GROUPMISSING); |
| 1476 | return; |
| 1477 | } |
| 1478 | GroupKey l2hashGroupKey = l2hashGroup.appCookie(); |
| 1479 | int l2hashGroupId = l2hashGroup.id().id(); |
| 1480 | GroupDescription l2hashGroupDesc = new DefaultGroupDescription(deviceId, |
| 1481 | SELECT, |
| 1482 | new GroupBuckets(newBuckets), |
| 1483 | l2hashGroupKey, |
| 1484 | l2hashGroupId, |
| 1485 | nextObjective.appId()); |
| 1486 | GroupChainElem l2hashGce = new GroupChainElem(l2hashGroupDesc, |
| 1487 | unsentGroups.size(), |
| 1488 | true, |
| 1489 | deviceId); |
| 1490 | // update new bucket-chains |
| 1491 | List<Deque<GroupKey>> addedKeys = new ArrayList<>(); |
| 1492 | for (Deque<GroupKey> newBucketChain : allGroupKeys) { |
| 1493 | newBucketChain.addFirst(l2hashGroupKey); |
| 1494 | addedKeys.add(newBucketChain); |
| 1495 | } |
| 1496 | updatePendingNextObjective(l2hashGroupKey, |
| 1497 | new OfdpaNextGroup(addedKeys, nextObjective)); |
| 1498 | log.debug("Adding to L2HASH: device:{} gid:{} group key:{} nextId:{}", |
| 1499 | deviceId, Integer.toHexString(l2hashGroupId), |
| 1500 | l2hashGroupKey, nextObjective.id()); |
| 1501 | unsentGroups.forEach(groupInfo -> { |
| 1502 | // send the innermost group |
| 1503 | log.debug("Sending innermost group {} in group chain on device {} ", |
| 1504 | Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()), |
| 1505 | deviceId); |
| 1506 | updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l2hashGce); |
| 1507 | groupService.addGroup(groupInfo.innerMostGroupDesc()); |
| 1508 | }); |
| 1509 | } |
| 1510 | |
| 1511 | private void addBucketToEcmpHashGroup(NextObjective nextObjective, |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1512 | List<Deque<GroupKey>> allActiveKeys) { |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1513 | // storage for all group keys in the chain of groups created |
| 1514 | List<Deque<GroupKey>> allGroupKeys = new ArrayList<>(); |
| 1515 | List<GroupInfo> unsentGroups = new ArrayList<>(); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1516 | List<GroupBucket> newBuckets; |
Charles Chan | 367c1c1 | 2018-10-19 16:23:28 -0700 | [diff] [blame] | 1517 | createEcmpHashBucketChains(nextObjective, allGroupKeys, unsentGroups); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1518 | // now we can create the buckets to add to the outermost L3 ECMP group |
| 1519 | newBuckets = generateNextGroupBuckets(unsentGroups, SELECT); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1520 | // retrieve the original L3 ECMP group |
Jonghwan Hyun | f810a7a | 2018-02-12 16:43:45 +0900 | [diff] [blame] | 1521 | Group l3ecmpGroup = retrieveTopLevelGroup(allActiveKeys, deviceId, |
| 1522 | groupService, nextObjective.id()); |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1523 | if (l3ecmpGroup == null) { |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1524 | fail(nextObjective, ObjectiveError.GROUPMISSING); |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1525 | return; |
| 1526 | } |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1527 | GroupKey l3ecmpGroupKey = l3ecmpGroup.appCookie(); |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1528 | int l3ecmpGroupId = l3ecmpGroup.id().id(); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1529 | // Although GroupDescriptions are not necessary for adding buckets to |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1530 | // 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] | 1531 | // processed, the info will be extracted for the bucketAdd call to groupService |
| 1532 | GroupDescription l3ecmpGroupDesc = |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1533 | new DefaultGroupDescription(deviceId, |
| 1534 | SELECT, |
| 1535 | new GroupBuckets(newBuckets), |
| 1536 | l3ecmpGroupKey, |
| 1537 | l3ecmpGroupId, |
| 1538 | nextObjective.appId()); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1539 | GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc, |
| 1540 | unsentGroups.size(), |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1541 | true, |
| 1542 | deviceId); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1543 | |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1544 | // update new bucket-chains |
| 1545 | List<Deque<GroupKey>> addedKeys = new ArrayList<>(); |
| 1546 | for (Deque<GroupKey> newBucketChain : allGroupKeys) { |
| 1547 | newBucketChain.addFirst(l3ecmpGroupKey); |
| 1548 | addedKeys.add(newBucketChain); |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1549 | } |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1550 | updatePendingNextObjective(l3ecmpGroupKey, |
| 1551 | new OfdpaNextGroup(addedKeys, nextObjective)); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1552 | log.debug("Adding to L3ECMP: device:{} gid:{} group key:{} nextId:{}", |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1553 | deviceId, Integer.toHexString(l3ecmpGroupId), |
| 1554 | l3ecmpGroupKey, nextObjective.id()); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1555 | unsentGroups.forEach(groupInfo -> { |
| 1556 | // send the innermost group |
| 1557 | log.debug("Sending innermost group {} in group chain on device {} ", |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1558 | Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()), |
| 1559 | deviceId); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1560 | updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l3ecmpGce); |
| 1561 | groupService.addGroup(groupInfo.innerMostGroupDesc()); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1562 | }); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1563 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1564 | |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1565 | private void addBucketToBroadcastGroup(NextObjective nextObj, |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1566 | List<Deque<GroupKey>> allActiveKeys) { |
| 1567 | VlanId assignedVlan = readVlanFromSelector(nextObj.meta()); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1568 | if (assignedVlan == null) { |
| 1569 | log.warn("VLAN ID required by broadcast next obj is missing. " |
| 1570 | + "Aborting add bucket to broadcast group for next:{} in dev:{}", |
| 1571 | nextObj.id(), deviceId); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1572 | fail(nextObj, ObjectiveError.BADPARAMS); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1573 | return; |
| 1574 | } |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1575 | List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1576 | IpPrefix ipDst = readIpDstFromSelector(nextObj.meta()); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1577 | if (ipDst != null) { |
| 1578 | if (ipDst.isMulticast()) { |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1579 | addBucketToL3MulticastGroup(nextObj, allActiveKeys, groupInfos, assignedVlan); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1580 | } else { |
| 1581 | log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1582 | fail(nextObj, ObjectiveError.BADPARAMS); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1583 | } |
| 1584 | } else { |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1585 | addBucketToL2FloodGroup(nextObj, allActiveKeys, groupInfos, assignedVlan); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1586 | } |
| 1587 | } |
| 1588 | |
| 1589 | private void addBucketToL2FloodGroup(NextObjective nextObj, |
| 1590 | List<Deque<GroupKey>> allActiveKeys, |
| 1591 | List<GroupInfo> groupInfos, |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1592 | VlanId assignedVlan) { |
Jonghwan Hyun | f810a7a | 2018-02-12 16:43:45 +0900 | [diff] [blame] | 1593 | Group l2FloodGroup = retrieveTopLevelGroup(allActiveKeys, deviceId, |
| 1594 | groupService, nextObj.id()); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1595 | |
| 1596 | if (l2FloodGroup == null) { |
| 1597 | log.warn("Can't find L2 flood group while adding bucket to it. NextObj = {}", |
| 1598 | nextObj); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1599 | fail(nextObj, ObjectiveError.GROUPMISSING); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1600 | return; |
| 1601 | } |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1602 | |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1603 | GroupKey l2floodGroupKey = l2FloodGroup.appCookie(); |
| 1604 | int l2floodGroupId = l2FloodGroup.id().id(); |
| 1605 | List<GroupBucket> newBuckets = generateNextGroupBuckets(groupInfos, ALL); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1606 | GroupDescription l2FloodGroupDescription = |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1607 | new DefaultGroupDescription(deviceId, |
| 1608 | ALL, |
| 1609 | new GroupBuckets(newBuckets), |
| 1610 | l2floodGroupKey, |
| 1611 | l2floodGroupId, |
| 1612 | nextObj.appId()); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1613 | GroupChainElem l2FloodGroupChainElement = |
| 1614 | new GroupChainElem(l2FloodGroupDescription, |
| 1615 | groupInfos.size(), |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1616 | true, |
| 1617 | deviceId); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1618 | |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1619 | |
| 1620 | //ensure assignedVlan applies to the chosen group |
| 1621 | VlanId floodGroupVlan = extractVlanIdFromGroupId(l2floodGroupId); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1622 | if (!floodGroupVlan.equals(assignedVlan)) { |
| 1623 | log.warn("VLAN ID {} does not match Flood group {} to which bucket is " |
| 1624 | + "being added, for next:{} in dev:{}. Abort.", assignedVlan, |
| 1625 | Integer.toHexString(l2floodGroupId), nextObj.id(), deviceId); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1626 | fail(nextObj, ObjectiveError.BADPARAMS); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1627 | return; |
| 1628 | } |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1629 | List<Deque<GroupKey>> addedKeys = new ArrayList<>(); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1630 | groupInfos.forEach(groupInfo -> { |
| 1631 | // update original NextGroup with new bucket-chain |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1632 | Deque<GroupKey> newBucketChain = new ArrayDeque<>(); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1633 | newBucketChain.addFirst(groupInfo.nextGroupDesc().appCookie()); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1634 | newBucketChain.addFirst(l2floodGroupKey); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1635 | addedKeys.add(newBucketChain); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1636 | log.debug("Adding to L2FLOOD: device:{} gid:{} group key:{} nextId:{}", |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1637 | deviceId, Integer.toHexString(l2floodGroupId), |
| 1638 | l2floodGroupKey, nextObj.id()); |
| 1639 | // send the innermost group |
| 1640 | log.debug("Sending innermost group {} in group chain on device {} ", |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1641 | Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()), |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1642 | deviceId); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1643 | updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l2FloodGroupChainElement); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1644 | |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1645 | DeviceId innerMostGroupDevice = groupInfo.innerMostGroupDesc().deviceId(); |
| 1646 | GroupKey innerMostGroupKey = groupInfo.innerMostGroupDesc().appCookie(); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1647 | Group existsL2IGroup = groupService.getGroup(innerMostGroupDevice, innerMostGroupKey); |
| 1648 | |
| 1649 | if (existsL2IGroup != null) { |
| 1650 | // group already exist |
| 1651 | processPendingAddGroupsOrNextObjs(innerMostGroupKey, true); |
| 1652 | } else { |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1653 | groupService.addGroup(groupInfo.innerMostGroupDesc()); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1654 | } |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1655 | }); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1656 | |
| 1657 | updatePendingNextObjective(l2floodGroupKey, |
| 1658 | new OfdpaNextGroup(addedKeys, nextObj)); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1659 | } |
| 1660 | |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1661 | private void addBucketToL3MulticastGroup(NextObjective nextObj, |
| 1662 | List<Deque<GroupKey>> allActiveKeys, |
| 1663 | List<GroupInfo> groupInfos, |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1664 | VlanId assignedVlan) { |
Pier Luigi | 21fffd2 | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 1665 | // Create the buckets to add to the outermost L3 Multicast group |
| 1666 | List<GroupBucket> newBuckets = createL3MulticastBucket(groupInfos); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1667 | |
| 1668 | // get the group being edited |
Jonghwan Hyun | f810a7a | 2018-02-12 16:43:45 +0900 | [diff] [blame] | 1669 | Group l3mcastGroup = retrieveTopLevelGroup(allActiveKeys, deviceId, |
| 1670 | groupService, nextObj.id()); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1671 | if (l3mcastGroup == null) { |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1672 | fail(nextObj, ObjectiveError.GROUPMISSING); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1673 | return; |
| 1674 | } |
| 1675 | GroupKey l3mcastGroupKey = l3mcastGroup.appCookie(); |
| 1676 | int l3mcastGroupId = l3mcastGroup.id().id(); |
| 1677 | |
| 1678 | //ensure assignedVlan applies to the chosen group |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1679 | VlanId expectedVlan = extractVlanIdFromGroupId(l3mcastGroupId); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1680 | if (!expectedVlan.equals(assignedVlan)) { |
| 1681 | log.warn("VLAN ID {} does not match L3 Mcast group {} to which bucket is " |
| 1682 | + "being added, for next:{} in dev:{}. Abort.", assignedVlan, |
| 1683 | Integer.toHexString(l3mcastGroupId), nextObj.id(), deviceId); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1684 | fail(nextObj, ObjectiveError.BADPARAMS); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1685 | } |
| 1686 | GroupDescription l3mcastGroupDescription = |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1687 | new DefaultGroupDescription(deviceId, |
| 1688 | ALL, |
| 1689 | new GroupBuckets(newBuckets), |
| 1690 | l3mcastGroupKey, |
| 1691 | l3mcastGroupId, |
| 1692 | nextObj.appId()); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1693 | GroupChainElem l3mcastGce = new GroupChainElem(l3mcastGroupDescription, |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1694 | groupInfos.size(), |
| 1695 | true, |
| 1696 | deviceId); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1697 | |
| 1698 | List<Deque<GroupKey>> addedKeys = new ArrayList<>(); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1699 | groupInfos.forEach(groupInfo -> { |
| 1700 | // update original NextGroup with new bucket-chain |
| 1701 | Deque<GroupKey> newBucketChain = new ArrayDeque<>(); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1702 | newBucketChain.addFirst(groupInfo.innerMostGroupDesc().appCookie()); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1703 | // Add L3 interface group to the chain if there is one. |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1704 | if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) { |
| 1705 | newBucketChain.addFirst(groupInfo.nextGroupDesc().appCookie()); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1706 | } |
| 1707 | newBucketChain.addFirst(l3mcastGroupKey); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1708 | addedKeys.add(newBucketChain); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1709 | |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1710 | updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l3mcastGce); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1711 | // Point next group to inner-most group, if any |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1712 | if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) { |
| 1713 | GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc(), |
| 1714 | 1, |
| 1715 | false, |
| 1716 | deviceId); |
| 1717 | updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), innerGce); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1718 | } |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1719 | log.debug("Adding to L3MCAST: device:{} gid:{} group key:{} nextId:{}", |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1720 | deviceId, Integer.toHexString(l3mcastGroupId), |
| 1721 | l3mcastGroupKey, nextObj.id()); |
| 1722 | // send the innermost group |
| 1723 | log.debug("Sending innermost group {} in group chain on device {} ", |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1724 | Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()), |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1725 | deviceId); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1726 | groupService.addGroup(groupInfo.innerMostGroupDesc()); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1727 | |
| 1728 | }); |
| 1729 | |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1730 | updatePendingNextObjective(l3mcastGroupKey, |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1731 | new OfdpaNextGroup(addedKeys, nextObj)); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1732 | } |
| 1733 | |
| 1734 | /** |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1735 | * Removes buckets in the top level group of a possible group-chain. Does |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1736 | * 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] | 1737 | * may be in use (referenced by other groups) elsewhere. |
| 1738 | * |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1739 | * @param nextObjective a next objective that contains information for the |
| 1740 | * buckets to be removed from the group |
| 1741 | * @param next the representation of the existing group-chains for this next |
| 1742 | * objective, from which the top-level buckets to remove are determined |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1743 | */ |
| 1744 | protected void removeBucketFromGroup(NextObjective nextObjective, NextGroup next) { |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1745 | if (nextObjective.type() != NextObjective.Type.HASHED && |
| 1746 | nextObjective.type() != NextObjective.Type.BROADCAST) { |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1747 | log.warn("RemoveBuckets not applied to nextType:{} in dev:{} for next:{}", |
| 1748 | nextObjective.type(), deviceId, nextObjective.id()); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1749 | fail(nextObjective, ObjectiveError.UNSUPPORTED); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1750 | return; |
| 1751 | } |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1752 | List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data()); |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1753 | List<Integer> indicesToRemove = Lists.newArrayList(); |
| 1754 | for (TrafficTreatment treatment : nextObjective.next()) { |
| 1755 | // find the top-level bucket in the group-chain by matching the |
| 1756 | // outport and label from different groups in the chain |
| 1757 | PortNumber portToRemove = readOutPortFromTreatment(treatment); |
| 1758 | int labelToRemove = readLabelFromTreatment(treatment); |
| 1759 | if (portToRemove == null) { |
| 1760 | log.warn("treatment {} of next objective {} has no outport.. " |
| 1761 | + "cannot remove bucket from group in dev: {}", treatment, |
| 1762 | nextObjective.id(), deviceId); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1763 | continue; |
| 1764 | } |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1765 | List<Integer> existing = existingPortAndLabel(allActiveKeys, |
| 1766 | groupService, deviceId, |
| 1767 | portToRemove, labelToRemove); |
| 1768 | indicesToRemove.addAll(existing); |
| 1769 | |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1770 | } |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1771 | |
| 1772 | List<Deque<GroupKey>> chainsToRemove = Lists.newArrayList(); |
| 1773 | indicesToRemove.forEach(index -> chainsToRemove |
| 1774 | .add(allActiveKeys.get(index))); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1775 | if (chainsToRemove.isEmpty()) { |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1776 | log.warn("Could not find appropriate group-chain for removing bucket" |
| 1777 | + " for next id {} in dev:{}", nextObjective.id(), deviceId); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1778 | fail(nextObjective, ObjectiveError.BADPARAMS); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1779 | return; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1780 | } |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1781 | removeBucket(chainsToRemove, nextObjective); |
| 1782 | } |
| 1783 | |
| 1784 | /** |
| 1785 | * Removes top-level buckets from a group that represents the given next objective. |
| 1786 | * |
| 1787 | * @param chainsToRemove a list of group bucket chains to remove |
| 1788 | * @param nextObjective the next objective that contains information for the |
| 1789 | * buckets to be removed from the group |
| 1790 | */ |
| 1791 | protected void removeBucket(List<Deque<GroupKey>> chainsToRemove, |
| 1792 | NextObjective nextObjective) { |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1793 | List<GroupBucket> bucketsToRemove = Lists.newArrayList(); |
| 1794 | //first group key is the one we want to modify |
| 1795 | GroupKey modGroupKey = chainsToRemove.get(0).peekFirst(); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1796 | Group modGroup = groupService.getGroup(deviceId, modGroupKey); |
Ray Milkey | 4386981 | 2018-05-04 13:00:39 -0700 | [diff] [blame] | 1797 | if (modGroup == null) { |
| 1798 | log.warn("removeBucket(): Attempt to modify non-existent group {} for device {}", modGroupKey, deviceId); |
| 1799 | return; |
| 1800 | } |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1801 | for (Deque<GroupKey> foundChain : chainsToRemove) { |
| 1802 | //second group key is the one we wish to remove the reference to |
| 1803 | if (foundChain.size() < 2) { |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1804 | // additional check to make sure second group key exists in |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1805 | // the chain. |
| 1806 | log.warn("Can't find second group key from chain {}", |
| 1807 | foundChain); |
| 1808 | continue; |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1809 | } |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1810 | GroupKey pointedGroupKey = foundChain.stream() |
| 1811 | .collect(Collectors.toList()).get(1); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1812 | Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey); |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1813 | if (pointedGroup == null) { |
| 1814 | continue; |
| 1815 | } |
| 1816 | |
| 1817 | GroupBucket bucket; |
| 1818 | if (nextObjective.type() == NextObjective.Type.HASHED) { |
| 1819 | bucket = DefaultGroupBucket.createSelectGroupBucket( |
| 1820 | DefaultTrafficTreatment.builder() |
| 1821 | .group(pointedGroup.id()) |
| 1822 | .build()); |
| 1823 | } else { |
| 1824 | bucket = DefaultGroupBucket.createAllGroupBucket( |
| 1825 | DefaultTrafficTreatment.builder() |
| 1826 | .group(pointedGroup.id()) |
| 1827 | .build()); |
| 1828 | } |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1829 | bucketsToRemove.add(bucket); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1830 | } |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1831 | |
| 1832 | GroupBuckets removeBuckets = new GroupBuckets(bucketsToRemove); |
| 1833 | List<String> pointedGroupIds; // for debug log |
| 1834 | pointedGroupIds = bucketsToRemove.stream() |
| 1835 | .map(GroupBucket::treatment) |
| 1836 | .map(TrafficTreatment::allInstructions) |
| 1837 | .flatMap(List::stream) |
| 1838 | .filter(inst -> inst instanceof Instructions.GroupInstruction) |
| 1839 | .map(inst -> (Instructions.GroupInstruction) inst) |
| 1840 | .map(Instructions.GroupInstruction::groupId) |
| 1841 | .map(GroupId::id) |
| 1842 | .map(Integer::toHexString) |
| 1843 | .map(id -> HEX_PREFIX + id) |
| 1844 | .collect(Collectors.toList()); |
| 1845 | |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1846 | log.debug("Removing buckets from group id 0x{} pointing to group id(s) {} " |
Ray Milkey | 4386981 | 2018-05-04 13:00:39 -0700 | [diff] [blame] | 1847 | + "for next id {} in device {}", |
| 1848 | Integer.toHexString(modGroup.id().id()), |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 1849 | pointedGroupIds, nextObjective.id(), deviceId); |
| 1850 | addPendingUpdateNextObjective(modGroupKey, nextObjective); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1851 | groupService.removeBucketsFromGroup(deviceId, modGroupKey, |
| 1852 | removeBuckets, modGroupKey, |
| 1853 | nextObjective.appId()); |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1854 | // update store - synchronize access as there may be multiple threads |
| 1855 | // trying to remove buckets from the same group, each with its own |
| 1856 | // potentially stale copy of allActiveKeys |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1857 | synchronized (flowObjectiveStore) { |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1858 | // get a fresh copy of what the store holds |
| 1859 | NextGroup next = flowObjectiveStore.getNextGroup(nextObjective.id()); |
| 1860 | List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data()); |
Pier Luigi | d008dbe | 2018-01-15 09:48:49 +0100 | [diff] [blame] | 1861 | allActiveKeys = Lists.newArrayList(allActiveKeys); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1862 | // Note that since we got a new object, and ArrayDeque does not implement |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1863 | // Object.equals(), we have to check the deque elems one by one |
| 1864 | allActiveKeys |
| 1865 | .removeIf(active -> |
| 1866 | chainsToRemove.stream().anyMatch(remove -> |
| 1867 | Arrays.equals(remove.toArray(new GroupKey[0]), |
| 1868 | active.toArray(new GroupKey[0])))); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1869 | // If no buckets in the group, then retain an entry for the |
| 1870 | // top level group which still exists. |
| 1871 | if (allActiveKeys.isEmpty()) { |
| 1872 | ArrayDeque<GroupKey> top = new ArrayDeque<>(); |
| 1873 | top.add(modGroupKey); |
| 1874 | allActiveKeys.add(top); |
| 1875 | } |
| 1876 | flowObjectiveStore.putNextGroup(nextObjective.id(), |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1877 | new OfdpaNextGroup(allActiveKeys, |
| 1878 | nextObjective)); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1879 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1880 | } |
| 1881 | |
| 1882 | /** |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 1883 | * Removes all groups in multiple possible group-chains that represent the next-obj. |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1884 | * |
| 1885 | * @param nextObjective the next objective to remove |
| 1886 | * @param next the NextGroup that represents the existing group-chain for |
| 1887 | * this next objective |
| 1888 | */ |
| 1889 | protected void removeGroup(NextObjective nextObjective, NextGroup next) { |
Andreas Pantelopoulos | 27532cd | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1890 | |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1891 | List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data()); |
Charles Chan | fc5c780 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 1892 | |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1893 | List<GroupKey> groupKeys = allActiveKeys.stream() |
Charles Chan | fc5c780 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 1894 | .map(Deque::getFirst).collect(Collectors.toList()); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 1895 | addPendingRemoveNextObjective(nextObjective, groupKeys); |
Charles Chan | fc5c780 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 1896 | |
Andreas Pantelopoulos | 27532cd | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1897 | allActiveKeys |
| 1898 | .forEach(groupChain -> groupChain.forEach(groupKey -> groupService |
| 1899 | .removeGroup(deviceId, groupKey, nextObjective.appId()))); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1900 | flowObjectiveStore.removeNextGroup(nextObjective.id()); |
| 1901 | } |
| 1902 | |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1903 | /** |
Ruchi Sahota | ef0761c | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 1904 | * modifies group with next objective. |
| 1905 | * |
| 1906 | * @param nextObjective the NextObjective |
| 1907 | * @param nextGroup the NextGroup |
| 1908 | */ |
| 1909 | protected void modifyBucketFromGroup(NextObjective nextObjective, NextGroup nextGroup) { |
| 1910 | switch (nextObjective.type()) { |
| 1911 | case SIMPLE: |
| 1912 | Collection<TrafficTreatment> treatments = nextObjective.next(); |
| 1913 | if (treatments.size() != 1) { |
| 1914 | log.error("Next Objectives of type Simple should only have a " |
| 1915 | + "single Traffic Treatment. Next Objective Id:{}", |
| 1916 | nextObjective.id()); |
| 1917 | fail(nextObjective, ObjectiveError.BADPARAMS); |
| 1918 | return; |
| 1919 | } |
| 1920 | modifySimpleNextObjective(nextObjective, nextGroup); |
| 1921 | break; |
| 1922 | default: |
| 1923 | fail(nextObjective, ObjectiveError.UNKNOWN); |
| 1924 | log.warn("Unknown next objective type {}", nextObjective.type()); |
| 1925 | } |
| 1926 | } |
| 1927 | |
| 1928 | /** |
| 1929 | * As per the OFDPA 2.0 TTP, packets are sent out of ports by using |
| 1930 | * a chain of groups. The simple Next Objective passed in by the application |
| 1931 | * is broken up into a group chain. The following chains can be modified |
| 1932 | * depending on the parameters in the Next Objective. |
| 1933 | * 1. L2 Interface group (no chaining) |
| 1934 | * 2. L3 Unicast group -> L2 Interface group |
| 1935 | * |
| 1936 | * @param nextObj the nextObjective of type SIMPLE |
| 1937 | */ |
| 1938 | private void modifySimpleNextObjective(NextObjective nextObj, NextGroup nextGroup) { |
| 1939 | TrafficTreatment treatment = nextObj.next().iterator().next(); |
| 1940 | // determine if plain L2 or L3->L2 chain |
| 1941 | boolean plainL2 = true; |
| 1942 | for (Instruction ins : treatment.allInstructions()) { |
| 1943 | if (ins.type() == Instruction.Type.L2MODIFICATION) { |
| 1944 | L2ModificationInstruction l2ins = (L2ModificationInstruction) ins; |
| 1945 | if (l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_DST || |
| 1946 | l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_SRC || |
| 1947 | l2ins.subtype() == L2ModificationInstruction.L2SubType.VLAN_ID) { |
| 1948 | plainL2 = false; |
| 1949 | } |
| 1950 | } |
| 1951 | } |
| 1952 | if (plainL2) { |
| 1953 | modifyBucketInL2Group(nextObj, nextGroup); |
| 1954 | } else { |
| 1955 | modifyBucketInL3Group(nextObj, nextGroup); |
| 1956 | } |
| 1957 | return; |
| 1958 | } |
| 1959 | |
| 1960 | |
| 1961 | /** |
Jonghwan Hyun | f810a7a | 2018-02-12 16:43:45 +0900 | [diff] [blame] | 1962 | * Modify buckets in the L2 interface group. |
| 1963 | * |
| 1964 | * @param nextObjective a next objective that contains information for the |
| 1965 | * buckets to be modified in the group |
| 1966 | * @param next the representation of the existing group-chains for this next |
| 1967 | * objective, from which the innermost group buckets to remove are determined |
| 1968 | */ |
Ruchi Sahota | ef0761c | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 1969 | |
| 1970 | protected void modifyBucketInL2Group(NextObjective nextObjective, NextGroup next) { |
Jonghwan Hyun | f810a7a | 2018-02-12 16:43:45 +0900 | [diff] [blame] | 1971 | |
| 1972 | VlanId assignedVlan = readVlanFromSelector(nextObjective.meta()); |
| 1973 | if (assignedVlan == null) { |
| 1974 | log.warn("VLAN ID required by simple next obj is missing. Abort."); |
| 1975 | fail(nextObjective, ObjectiveError.BADPARAMS); |
| 1976 | return; |
| 1977 | } |
| 1978 | |
| 1979 | List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObjective, assignedVlan); |
| 1980 | |
| 1981 | // There is only one L2 interface group in this case |
| 1982 | GroupDescription l2InterfaceGroupDesc = groupInfos.get(0).innerMostGroupDesc(); |
| 1983 | |
| 1984 | // Replace group bucket for L2 interface group |
| 1985 | groupService.setBucketsForGroup(deviceId, |
| 1986 | l2InterfaceGroupDesc.appCookie(), |
| 1987 | l2InterfaceGroupDesc.buckets(), |
| 1988 | l2InterfaceGroupDesc.appCookie(), |
| 1989 | l2InterfaceGroupDesc.appId()); |
| 1990 | |
| 1991 | // update store - synchronize access as there may be multiple threads |
| 1992 | // trying to remove buckets from the same group, each with its own |
| 1993 | // potentially stale copy of allActiveKeys |
| 1994 | synchronized (flowObjectiveStore) { |
Shibu Vijayakumar | d0c491f | 2018-03-01 15:45:59 -0800 | [diff] [blame^] | 1995 | // NOTE: The groupKey is computed by deviceId, VLAN and portNum. It remains the same when we modify L2IG. |
| 1996 | // Therefore we use the same groupKey of the existing group. |
| 1997 | List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data()); |
Jonghwan Hyun | f810a7a | 2018-02-12 16:43:45 +0900 | [diff] [blame] | 1998 | flowObjectiveStore.putNextGroup(nextObjective.id(), |
Shibu Vijayakumar | d0c491f | 2018-03-01 15:45:59 -0800 | [diff] [blame^] | 1999 | new OfdpaNextGroup(allActiveKeys, |
Jonghwan Hyun | f810a7a | 2018-02-12 16:43:45 +0900 | [diff] [blame] | 2000 | nextObjective)); |
| 2001 | } |
Ruchi Sahota | ef0761c | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 2002 | |
Jonghwan Hyun | f810a7a | 2018-02-12 16:43:45 +0900 | [diff] [blame] | 2003 | } |
| 2004 | |
Ruchi Sahota | ef0761c | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 2005 | protected void modifyBucketInL3Group(NextObjective nextObjective, NextGroup next) { |
| 2006 | |
| 2007 | //get l3 group |
| 2008 | GroupInfo groupInfo = prepareL3UnicastGroup(nextObjective, next); |
| 2009 | if (groupInfo == null) { |
| 2010 | log.warn("Null groupInfo retrieved for next obj. Abort."); |
| 2011 | fail(nextObjective, ObjectiveError.BADPARAMS); |
| 2012 | return; |
| 2013 | } |
| 2014 | |
| 2015 | GroupDescription l3UnicastGroupDesc = groupInfo.nextGroupDesc(); |
| 2016 | |
| 2017 | // Replace group bucket for L3 UC interface group |
| 2018 | groupService.setBucketsForGroup(deviceId, l3UnicastGroupDesc.appCookie(), |
| 2019 | l3UnicastGroupDesc.buckets(), l3UnicastGroupDesc.appCookie(), |
| 2020 | l3UnicastGroupDesc.appId()); |
| 2021 | |
| 2022 | // create object for local and distributed storage |
| 2023 | Deque<GroupKey> gkeyChain = new ArrayDeque<>(); |
| 2024 | gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie()); |
| 2025 | gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie()); |
| 2026 | List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList(); |
| 2027 | allGroupKeys.add(gkeyChain); |
| 2028 | OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObjective); |
| 2029 | // store l3groupkey with the ofdpaNextGroup for the nextObjective that depends on it |
| 2030 | updatePendingNextObjective(groupInfo.nextGroupDesc().appCookie(), ofdpaGrp); |
| 2031 | |
| 2032 | // update store - synchronize access as there may be multiple threads |
| 2033 | // trying to update bucket from the same group, each with its own |
| 2034 | // potentially stale copy of allActiveKeys |
| 2035 | synchronized (flowObjectiveStore) { |
| 2036 | |
| 2037 | List<Deque<GroupKey>> modifiedGroupKeys = Lists.newArrayList(); |
| 2038 | ArrayDeque<GroupKey> top = new ArrayDeque<>(); |
| 2039 | top.add(l3UnicastGroupDesc.appCookie()); |
| 2040 | top.add(groupInfo.innerMostGroupDesc().appCookie()); //l2 group key |
| 2041 | modifiedGroupKeys.add(top); |
| 2042 | |
| 2043 | flowObjectiveStore.putNextGroup(nextObjective.id(), |
| 2044 | new OfdpaNextGroup(modifiedGroupKeys, |
| 2045 | nextObjective)); |
| 2046 | } |
| 2047 | } |
| 2048 | |
| 2049 | |
Jonghwan Hyun | f810a7a | 2018-02-12 16:43:45 +0900 | [diff] [blame] | 2050 | /** |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 2051 | * Checks existing buckets in {@link NextGroup} to verify if they match |
| 2052 | * the buckets in the given {@link NextObjective}. Adds or removes buckets |
| 2053 | * to ensure that the buckets match up. |
| 2054 | * |
| 2055 | * @param nextObjective the next objective to verify |
| 2056 | * @param next the representation of the existing group which has to be |
| 2057 | * modified to match the given next objective |
| 2058 | */ |
| 2059 | protected void verifyGroup(NextObjective nextObjective, NextGroup next) { |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 2060 | if (nextObjective.type() == NextObjective.Type.SIMPLE) { |
| 2061 | log.warn("verification not supported for indirect group"); |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 2062 | fail(nextObjective, ObjectiveError.UNSUPPORTED); |
| 2063 | return; |
| 2064 | } |
Charles Chan | 1dd7557 | 2018-05-08 11:49:05 -0700 | [diff] [blame] | 2065 | log.trace("Call to verify device:{} nextId:{}", deviceId, nextObjective.id()); |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 2066 | List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data()); |
| 2067 | List<TrafficTreatment> bucketsToCreate = Lists.newArrayList(); |
| 2068 | List<Integer> indicesToRemove = Lists.newArrayList(); |
Pier Luigi | ec6ac42 | 2018-01-29 10:30:59 +0100 | [diff] [blame] | 2069 | |
| 2070 | // Iterating over the treatments of the next objective allows |
| 2071 | // to detect missing buckets and/or duplicate buckets (to be removed) |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 2072 | for (TrafficTreatment bkt : nextObjective.next()) { |
| 2073 | PortNumber portNumber = readOutPortFromTreatment(bkt); |
| 2074 | int label = readLabelFromTreatment(bkt); |
| 2075 | if (portNumber == null) { |
| 2076 | log.warn("treatment {} of next objective {} has no outport.. " |
Ravi Dewangan | f59ca9e | 2019-05-28 22:24:18 +0000 | [diff] [blame] | 2077 | + "cannot remove bucket from group in dev: {}", bkt, nextObjective.id(), deviceId); |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 2078 | fail(nextObjective, ObjectiveError.BADPARAMS); |
| 2079 | return; |
| 2080 | } |
Ravi Dewangan | f59ca9e | 2019-05-28 22:24:18 +0000 | [diff] [blame] | 2081 | List<Integer> existing = existingPortAndLabel(allActiveKeys, groupService, deviceId, portNumber, label); |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 2082 | if (existing.isEmpty()) { |
| 2083 | // if it doesn't exist, mark this bucket for creation |
| 2084 | bucketsToCreate.add(bkt); |
| 2085 | } |
| 2086 | if (existing.size() > 1) { |
| 2087 | // if it exists but there are duplicates, mark the others for removal |
| 2088 | existing.remove(0); |
| 2089 | indicesToRemove.addAll(existing); |
| 2090 | } |
| 2091 | } |
| 2092 | |
Pier Luigi | ec6ac42 | 2018-01-29 10:30:59 +0100 | [diff] [blame] | 2093 | // Detect situation where the next data has more buckets |
| 2094 | // (not duplicates) respect to the next objective |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 2095 | if (allActiveKeys.size() > nextObjective.next().size() && |
| 2096 | // ignore specific case of empty group |
| 2097 | !(nextObjective.next().size() == 0 && allActiveKeys.size() == 1 |
| 2098 | && allActiveKeys.get(0).size() == 1)) { |
Pier Luigi | ec6ac42 | 2018-01-29 10:30:59 +0100 | [diff] [blame] | 2099 | log.warn("Mismatch detected between next and flowobjstore for device {}: " + |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 2100 | "nextId:{}, nextObjective-size:{} next-size:{} .. correcting", |
| 2101 | deviceId, nextObjective.id(), nextObjective.next().size(), |
| 2102 | allActiveKeys.size()); |
pier | 4080a55 | 2020-01-27 17:45:03 +0100 | [diff] [blame] | 2103 | List<Integer> otherIndices = indicesToRemoveFromNextGroup(allActiveKeys, nextObjective, |
| 2104 | groupService, deviceId); |
Pier Luigi | ec6ac42 | 2018-01-29 10:30:59 +0100 | [diff] [blame] | 2105 | // Filter out the indices not present |
| 2106 | otherIndices = otherIndices.stream() |
| 2107 | .filter(index -> !indicesToRemove.contains(index)) |
| 2108 | .collect(Collectors.toList()); |
| 2109 | // Add all to the final list |
| 2110 | indicesToRemove.addAll(otherIndices); |
| 2111 | } |
| 2112 | |
Charles Chan | 1dd7557 | 2018-05-08 11:49:05 -0700 | [diff] [blame] | 2113 | log.trace("Buckets to create {}", bucketsToCreate); |
| 2114 | log.trace("Indices to remove {}", indicesToRemove); |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 2115 | |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 2116 | if (!bucketsToCreate.isEmpty()) { |
| 2117 | log.info("creating {} buckets as part of nextId: {} verification", |
| 2118 | bucketsToCreate.size(), nextObjective.id()); |
| 2119 | //create a nextObjective only with these buckets |
| 2120 | NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder() |
| 2121 | .withId(nextObjective.id()) |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 2122 | .withType(nextObjective.type()) |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 2123 | .withMeta(nextObjective.meta()) |
| 2124 | .fromApp(nextObjective.appId()); |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 2125 | bucketsToCreate.forEach(nextObjBuilder::addTreatment); |
| 2126 | // According to the next type we call the proper add function |
| 2127 | if (nextObjective.type() == NextObjective.Type.HASHED) { |
pier | cd2a3ef | 2018-11-22 15:44:57 +0100 | [diff] [blame] | 2128 | if (isL2Hash(nextObjective)) { |
| 2129 | addBucketToL2HashGroup(nextObjBuilder.addToExisting(), allActiveKeys); |
| 2130 | } else { |
| 2131 | addBucketToEcmpHashGroup(nextObjBuilder.addToExisting(), allActiveKeys); |
| 2132 | } |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 2133 | } else { |
| 2134 | addBucketToBroadcastGroup(nextObjBuilder.addToExisting(), allActiveKeys); |
| 2135 | } |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 2136 | } |
| 2137 | |
| 2138 | if (!indicesToRemove.isEmpty()) { |
| 2139 | log.info("removing {} buckets as part of nextId: {} verification", |
| 2140 | indicesToRemove.size(), nextObjective.id()); |
| 2141 | List<Deque<GroupKey>> chainsToRemove = Lists.newArrayList(); |
pier | 4080a55 | 2020-01-27 17:45:03 +0100 | [diff] [blame] | 2142 | indicesToRemove.forEach(index -> chainsToRemove.add(allActiveKeys.get(index))); |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 2143 | removeBucket(chainsToRemove, nextObjective); |
| 2144 | } |
| 2145 | |
pier | 4080a55 | 2020-01-27 17:45:03 +0100 | [diff] [blame] | 2146 | log.trace("Checking mismatch with GroupStore device:{} nextId:{}", |
| 2147 | deviceId, nextObjective.id()); |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 2148 | if (bucketsToCreate.isEmpty() && indicesToRemove.isEmpty()) { |
| 2149 | // flowObjective store record is in-sync with nextObjective passed-in |
| 2150 | // Nevertheless groupStore may not be in sync due to bug in the store |
| 2151 | // - see CORD-1844. XXX When this bug is fixed, the rest of this verify |
| 2152 | // method will not be required. |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 2153 | GroupKey topGroupKey = allActiveKeys.get(0).peekFirst(); |
| 2154 | Group topGroup = groupService.getGroup(deviceId, topGroupKey); |
Ravi Dewangan | f59ca9e | 2019-05-28 22:24:18 +0000 | [diff] [blame] | 2155 | // topGroup should not be null - adding guard |
| 2156 | if (topGroup == null) { |
| 2157 | log.warn("topGroup {} not found in GroupStore device:{}, nextId:{}", |
| 2158 | topGroupKey, deviceId, nextObjective.id()); |
pier | 9a1a797 | 2019-12-10 18:09:00 +0100 | [diff] [blame] | 2159 | fail(nextObjective, ObjectiveError.GROUPMISSING); |
Ravi Dewangan | f59ca9e | 2019-05-28 22:24:18 +0000 | [diff] [blame] | 2160 | return; |
| 2161 | } |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 2162 | int actualGroupSize = topGroup.buckets().buckets().size(); |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 2163 | int objGroupSize = nextObjective.next().size(); |
| 2164 | if (actualGroupSize != objGroupSize) { |
| 2165 | log.warn("Mismatch detected in device:{}, nextId:{}, nextObjective-size" |
| 2166 | + ":{} group-size:{} .. correcting", deviceId, nextObjective.id(), |
| 2167 | objGroupSize, actualGroupSize); |
| 2168 | } |
| 2169 | if (actualGroupSize > objGroupSize) { |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 2170 | // Group in the device has more chains |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 2171 | List<GroupBucket> bucketsToRemove = Lists.newArrayList(); |
| 2172 | //check every bucket in the actual group |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 2173 | for (GroupBucket bucket : topGroup.buckets().buckets()) { |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 2174 | GroupInstruction g = (GroupInstruction) bucket.treatment() |
| 2175 | .allInstructions().iterator().next(); |
| 2176 | GroupId gidToCheck = g.groupId(); // the group pointed to |
| 2177 | boolean matches = false; |
| 2178 | for (Deque<GroupKey> validChain : allActiveKeys) { |
| 2179 | if (validChain.size() < 2) { |
| 2180 | continue; |
| 2181 | } |
pier | 4080a55 | 2020-01-27 17:45:03 +0100 | [diff] [blame] | 2182 | GroupKey pointedGroupKey = validChain.stream().collect(Collectors.toList()).get(1); |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 2183 | Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey); |
| 2184 | if (pointedGroup != null && gidToCheck.equals(pointedGroup.id())) { |
| 2185 | matches = true; |
| 2186 | break; |
| 2187 | } |
| 2188 | } |
| 2189 | if (!matches) { |
| 2190 | log.warn("Removing bucket pointing to groupId:{}", gidToCheck); |
| 2191 | bucketsToRemove.add(bucket); |
| 2192 | } |
| 2193 | } |
| 2194 | // remove buckets for which there was no record in the obj store |
| 2195 | if (bucketsToRemove.isEmpty()) { |
| 2196 | log.warn("Mismatch detected but could not determine which" |
| 2197 | + "buckets to remove"); |
| 2198 | } else { |
| 2199 | GroupBuckets removeBuckets = new GroupBuckets(bucketsToRemove); |
pier | 4080a55 | 2020-01-27 17:45:03 +0100 | [diff] [blame] | 2200 | groupService.removeBucketsFromGroup(deviceId, topGroupKey, removeBuckets, topGroupKey, |
| 2201 | nextObjective.appId()); |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 2202 | } |
| 2203 | } else if (actualGroupSize < objGroupSize) { |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 2204 | // Group in the device has less chains |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 2205 | // should also add buckets not in group-store but in obj-store |
| 2206 | List<GroupBucket> bucketsToAdd = Lists.newArrayList(); |
| 2207 | //check every bucket in the obj |
| 2208 | for (Deque<GroupKey> validChain : allActiveKeys) { |
| 2209 | if (validChain.size() < 2) { |
| 2210 | continue; |
| 2211 | } |
pier | 4080a55 | 2020-01-27 17:45:03 +0100 | [diff] [blame] | 2212 | GroupKey pointedGroupKey = validChain.stream().collect(Collectors.toList()).get(1); |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 2213 | Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey); |
| 2214 | if (pointedGroup == null) { |
| 2215 | // group should exist, otherwise cannot be added as bucket |
| 2216 | continue; |
| 2217 | } |
| 2218 | boolean matches = false; |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 2219 | for (GroupBucket bucket : topGroup.buckets().buckets()) { |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 2220 | GroupInstruction g = (GroupInstruction) bucket.treatment() |
| 2221 | .allInstructions().iterator().next(); |
| 2222 | GroupId gidToCheck = g.groupId(); // the group pointed to |
| 2223 | if (pointedGroup.id().equals(gidToCheck)) { |
| 2224 | matches = true; |
| 2225 | break; |
| 2226 | } |
| 2227 | } |
| 2228 | if (!matches) { |
| 2229 | log.warn("Adding bucket pointing to groupId:{}", pointedGroup); |
| 2230 | TrafficTreatment t = DefaultTrafficTreatment.builder() |
| 2231 | .group(pointedGroup.id()) |
| 2232 | .build(); |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 2233 | // Create the proper bucket according to the next type |
| 2234 | if (nextObjective.type() == NextObjective.Type.HASHED) { |
| 2235 | bucketsToAdd.add(DefaultGroupBucket.createSelectGroupBucket(t)); |
| 2236 | } else { |
| 2237 | bucketsToAdd.add(DefaultGroupBucket.createAllGroupBucket(t)); |
| 2238 | } |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 2239 | } |
| 2240 | } |
| 2241 | if (bucketsToAdd.isEmpty()) { |
| 2242 | log.warn("Mismatch detected but could not determine which " |
| 2243 | + "buckets to add"); |
| 2244 | } else { |
| 2245 | GroupBuckets addBuckets = new GroupBuckets(bucketsToAdd); |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 2246 | groupService.addBucketsToGroup(deviceId, topGroupKey, |
| 2247 | addBuckets, topGroupKey, |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 2248 | nextObjective.appId()); |
| 2249 | } |
| 2250 | } |
| 2251 | } |
pier | 4080a55 | 2020-01-27 17:45:03 +0100 | [diff] [blame] | 2252 | log.trace("Verify done for device:{} nextId:{}", deviceId, nextObjective.id()); |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 2253 | pass(nextObjective); |
| 2254 | } |
| 2255 | |
| 2256 | ////////////////////////////////////// |
| 2257 | // Helper methods and classes |
| 2258 | ////////////////////////////////////// |
| 2259 | |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 2260 | protected void updatePendingNextObjective(GroupKey groupKey, OfdpaNextGroup nextGrp) { |
| 2261 | pendingAddNextObjectives.asMap().compute(groupKey, (k, val) -> { |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 2262 | if (val == null) { |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 2263 | val = new CopyOnWriteArrayList<>(); |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 2264 | } |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 2265 | val.add(nextGrp); |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 2266 | return val; |
| 2267 | }); |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 2268 | } |
| 2269 | |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 2270 | protected void updatePendingGroups(GroupKey groupKey, GroupChainElem gce) { |
| 2271 | pendingGroups.asMap().compute(groupKey, (k, val) -> { |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 2272 | if (val == null) { |
| 2273 | val = Sets.newConcurrentHashSet(); |
| 2274 | } |
| 2275 | val.add(gce); |
| 2276 | return val; |
| 2277 | }); |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 2278 | } |
| 2279 | |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 2280 | protected void addPendingUpdateNextObjective(GroupKey groupKey, |
| 2281 | NextObjective nextObjective) { |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 2282 | pendingUpdateNextObjectives.compute(groupKey, (gKey, nextObjs) -> { |
| 2283 | if (nextObjs != null) { |
| 2284 | nextObjs.add(nextObjective); |
| 2285 | } else { |
| 2286 | nextObjs = Sets.newHashSet(nextObjective); |
| 2287 | } |
| 2288 | return nextObjs; |
| 2289 | }); |
| 2290 | } |
| 2291 | |
Saurav Das | 2f2c9d0 | 2018-04-07 16:51:09 -0700 | [diff] [blame] | 2292 | protected void addPendingRemoveNextObjective(NextObjective nextObjective, |
| 2293 | List<GroupKey> groupKeys) { |
| 2294 | pendingRemoveNextObjectives.put(nextObjective, groupKeys); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 2295 | } |
| 2296 | |
Saurav Das | 2f2c9d0 | 2018-04-07 16:51:09 -0700 | [diff] [blame] | 2297 | protected int getNextAvailableIndex() { |
| 2298 | return (int) nextIndex.incrementAndGet(); |
| 2299 | } |
| 2300 | |
| 2301 | protected void processPendingUpdateNextObjs(GroupKey groupKey) { |
| 2302 | Set<NextObjective> nextObjs = pendingUpdateNextObjectives.remove(groupKey); |
| 2303 | if (nextObjs != null) { |
| 2304 | nextObjs.forEach(nextObj -> { |
| 2305 | log.debug("Group {} updated, update pending next objective {}.", |
| 2306 | groupKey, nextObj); |
| 2307 | pass(nextObj); |
| 2308 | }); |
| 2309 | } |
| 2310 | } |
| 2311 | |
| 2312 | protected void processPendingRemoveNextObjs(GroupKey key) { |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 2313 | pendingRemoveNextObjectives.asMap().forEach((nextObjective, groupKeys) -> { |
jaegonkim | 2e4d99e | 2018-07-09 23:23:10 +0900 | [diff] [blame] | 2314 | groupKeys.remove(key); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 2315 | if (groupKeys.isEmpty()) { |
| 2316 | pendingRemoveNextObjectives.invalidate(nextObjective); |
| 2317 | pass(nextObjective); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 2318 | } |
| 2319 | }); |
| 2320 | } |
| 2321 | |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 2322 | protected void processPendingAddGroupsOrNextObjs(GroupKey key, boolean added) { |
| 2323 | //first check for group chain |
| 2324 | Set<OfdpaGroupHandlerUtility.GroupChainElem> gceSet = pendingGroups.asMap().remove(key); |
| 2325 | if (gceSet != null) { |
| 2326 | for (GroupChainElem gce : gceSet) { |
| 2327 | log.debug("Group service {} group key {} in device {}. " |
| 2328 | + "Processing next group in group chain with group id 0x{}", |
| 2329 | (added) ? "ADDED" : "processed", |
| 2330 | key, deviceId, |
| 2331 | Integer.toHexString(gce.groupDescription().givenGroupId())); |
| 2332 | processGroupChain(gce); |
| 2333 | } |
| 2334 | } else { |
| 2335 | // otherwise chain complete - check for waiting nextObjectives |
| 2336 | List<OfdpaGroupHandlerUtility.OfdpaNextGroup> nextGrpList = |
| 2337 | pendingAddNextObjectives.getIfPresent(key); |
| 2338 | if (nextGrpList != null) { |
| 2339 | pendingAddNextObjectives.invalidate(key); |
| 2340 | nextGrpList.forEach(nextGrp -> { |
| 2341 | log.debug("Group service {} group key {} in device:{}. " |
| 2342 | + "Done implementing next objective: {} <<-->> gid:0x{}", |
| 2343 | (added) ? "ADDED" : "processed", |
| 2344 | key, deviceId, nextGrp.nextObjective().id(), |
| 2345 | Integer.toHexString(groupService.getGroup(deviceId, key) |
| 2346 | .givenGroupId())); |
| 2347 | pass(nextGrp.nextObjective()); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 2348 | updateFlowObjectiveStore(nextGrp.nextObjective().id(), nextGrp); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 2349 | |
| 2350 | // check if addBuckets waiting for this completion |
| 2351 | pendingBuckets.compute(nextGrp.nextObjective().id(), (nextId, pendBkts) -> { |
| 2352 | if (pendBkts != null) { |
| 2353 | pendBkts.forEach(pendBkt -> addBucketToGroup(pendBkt, nextGrp)); |
| 2354 | } |
| 2355 | return null; |
| 2356 | }); |
| 2357 | }); |
| 2358 | } |
| 2359 | } |
| 2360 | } |
| 2361 | |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 2362 | /** |
| 2363 | * Processes next element of a group chain. Assumption is that if this |
| 2364 | * group points to another group, the latter has already been created |
| 2365 | * and this driver has received notification for it. A second assumption is |
| 2366 | * that if there is another group waiting for this group then the appropriate |
| 2367 | * stores already have the information to act upon the notification for the |
| 2368 | * creation of this group. |
| 2369 | * <p> |
| 2370 | * The processing of the GroupChainElement depends on the number of groups |
| 2371 | * this element is waiting on. For all group types other than SIMPLE, a |
| 2372 | * GroupChainElement could be waiting on multiple groups. |
| 2373 | * |
| 2374 | * @param gce the group chain element to be processed next |
| 2375 | */ |
| 2376 | private void processGroupChain(GroupChainElem gce) { |
| 2377 | int waitOnGroups = gce.decrementAndGetGroupsWaitedOn(); |
| 2378 | if (waitOnGroups != 0) { |
| 2379 | log.debug("GCE: {} not ready to be processed", gce); |
| 2380 | return; |
| 2381 | } |
| 2382 | log.debug("GCE: {} ready to be processed", gce); |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 2383 | if (gce.addBucketToGroup()) { |
| 2384 | groupService.addBucketsToGroup(gce.groupDescription().deviceId(), |
| 2385 | gce.groupDescription().appCookie(), |
| 2386 | gce.groupDescription().buckets(), |
| 2387 | gce.groupDescription().appCookie(), |
| 2388 | gce.groupDescription().appId()); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 2389 | } else { |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 2390 | groupService.addGroup(gce.groupDescription()); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 2391 | } |
| 2392 | } |
| 2393 | |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 2394 | private void updateFlowObjectiveStore(Integer nextId, OfdpaNextGroup nextGrp) { |
| 2395 | synchronized (flowObjectiveStore) { |
| 2396 | // get fresh copy of what the store holds |
| 2397 | NextGroup next = flowObjectiveStore.getNextGroup(nextId); |
| 2398 | if (next == null || nextGrp.nextObjective().op() == Operation.ADD) { |
| 2399 | flowObjectiveStore.putNextGroup(nextId, nextGrp); |
| 2400 | return; |
| 2401 | } |
| 2402 | if (nextGrp.nextObjective().op() == Operation.ADD_TO_EXISTING) { |
| 2403 | List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data()); |
Pier Luigi | d008dbe | 2018-01-15 09:48:49 +0100 | [diff] [blame] | 2404 | allActiveKeys = Lists.newArrayList(allActiveKeys); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 2405 | // If active keys shows only the top-level group without a chain of groups, |
| 2406 | // then it represents an empty group. Update by replacing empty chain. |
| 2407 | if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) { |
| 2408 | allActiveKeys.clear(); |
| 2409 | } |
| 2410 | allActiveKeys.addAll(nextGrp.allKeys()); |
| 2411 | flowObjectiveStore.putNextGroup(nextId, |
| 2412 | new OfdpaNextGroup(allActiveKeys, nextGrp.nextObjective())); |
| 2413 | } |
| 2414 | } |
| 2415 | } |
| 2416 | |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 2417 | private class InnerGroupListener implements GroupListener { |
| 2418 | @Override |
| 2419 | public void event(GroupEvent event) { |
Charles Chan | fc5c780 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 2420 | switch (event.type()) { |
| 2421 | case GROUP_ADDED: |
| 2422 | processPendingAddGroupsOrNextObjs(event.subject().appCookie(), true); |
| 2423 | break; |
| 2424 | case GROUP_REMOVED: |
| 2425 | processPendingRemoveNextObjs(event.subject().appCookie()); |
| 2426 | break; |
Yi Tseng | 78f51f4 | 2017-02-02 13:54:58 -0800 | [diff] [blame] | 2427 | case GROUP_UPDATED: |
| 2428 | processPendingUpdateNextObjs(event.subject().appCookie()); |
| 2429 | break; |
Charles Chan | fc5c780 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 2430 | default: |
| 2431 | break; |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 2432 | } |
| 2433 | } |
| 2434 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 2435 | } |