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