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