Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1 | package org.onosproject.driver.pipeline; |
| 2 | |
| 3 | import com.google.common.cache.Cache; |
| 4 | import com.google.common.cache.CacheBuilder; |
| 5 | import com.google.common.cache.RemovalCause; |
| 6 | import com.google.common.cache.RemovalNotification; |
| 7 | import org.onlab.osgi.ServiceDirectory; |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 8 | import org.onlab.packet.MacAddress; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 9 | import org.onlab.packet.MplsLabel; |
| 10 | import org.onlab.packet.VlanId; |
| 11 | import org.onosproject.core.ApplicationId; |
| 12 | import org.onosproject.core.DefaultGroupId; |
| 13 | import org.onosproject.net.DeviceId; |
| 14 | import org.onosproject.net.PortNumber; |
| 15 | import org.onosproject.net.behaviour.NextGroup; |
| 16 | import org.onosproject.net.behaviour.PipelinerContext; |
| 17 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 18 | import org.onosproject.net.flow.TrafficSelector; |
| 19 | import org.onosproject.net.flow.TrafficTreatment; |
| 20 | import org.onosproject.net.flow.criteria.Criterion; |
| 21 | import org.onosproject.net.flow.criteria.VlanIdCriterion; |
| 22 | import org.onosproject.net.flow.instructions.Instruction; |
| 23 | import org.onosproject.net.flow.instructions.Instructions; |
| 24 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
| 25 | import org.onosproject.net.flowobjective.FlowObjectiveStore; |
| 26 | import org.onosproject.net.flowobjective.NextObjective; |
| 27 | import org.onosproject.net.flowobjective.ObjectiveError; |
| 28 | import org.onosproject.net.group.DefaultGroupBucket; |
| 29 | import org.onosproject.net.group.DefaultGroupDescription; |
| 30 | import org.onosproject.net.group.DefaultGroupKey; |
| 31 | import org.onosproject.net.group.Group; |
| 32 | import org.onosproject.net.group.GroupBucket; |
| 33 | import org.onosproject.net.group.GroupBuckets; |
| 34 | import org.onosproject.net.group.GroupDescription; |
| 35 | import org.onosproject.net.group.GroupEvent; |
| 36 | import org.onosproject.net.group.GroupKey; |
| 37 | import org.onosproject.net.group.GroupListener; |
| 38 | import org.onosproject.net.group.GroupService; |
| 39 | import org.slf4j.Logger; |
| 40 | |
| 41 | import java.util.ArrayDeque; |
| 42 | import java.util.ArrayList; |
| 43 | import java.util.Collection; |
| 44 | import java.util.Collections; |
| 45 | import java.util.Deque; |
| 46 | import java.util.List; |
| 47 | import java.util.Map; |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 48 | import java.util.Objects; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 49 | import java.util.Set; |
| 50 | import java.util.concurrent.ConcurrentHashMap; |
| 51 | import java.util.concurrent.CopyOnWriteArrayList; |
| 52 | import java.util.concurrent.Executors; |
| 53 | import java.util.concurrent.ScheduledExecutorService; |
| 54 | import java.util.concurrent.TimeUnit; |
| 55 | import java.util.concurrent.atomic.AtomicInteger; |
| 56 | import java.util.stream.Collectors; |
| 57 | |
| 58 | import static org.onlab.util.Tools.groupedThreads; |
| 59 | import static org.slf4j.LoggerFactory.getLogger; |
| 60 | |
| 61 | /** |
| 62 | * Group handler for OFDPA2 pipeline. |
| 63 | */ |
| 64 | public class OFDPA2GroupHandler { |
| 65 | /* |
| 66 | * OFDPA requires group-id's to have a certain form. |
| 67 | * L2 Interface Groups have <4bits-0><12bits-vlanid><16bits-portid> |
| 68 | * L3 Unicast Groups have <4bits-2><28bits-index> |
| 69 | * MPLS Interface Groups have <4bits-9><4bits:0><24bits-index> |
| 70 | * L3 ECMP Groups have <4bits-7><28bits-index> |
| 71 | * L2 Flood Groups have <4bits-4><12bits-vlanid><16bits-index> |
| 72 | * L3 VPN Groups have <4bits-9><4bits-2><24bits-index> |
| 73 | */ |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 74 | private static final int L2_INTERFACE_TYPE = 0x00000000; |
| 75 | private static final int L3_UNICAST_TYPE = 0x20000000; |
| 76 | private static final int MPLS_INTERFACE_TYPE = 0x90000000; |
| 77 | private static final int MPLS_L3VPN_SUBTYPE = 0x92000000; |
| 78 | private static final int L3_ECMP_TYPE = 0x70000000; |
| 79 | private static final int L2_FLOOD_TYPE = 0x40000000; |
| 80 | |
| 81 | private static final int TYPE_MASK = 0x0fffffff; |
| 82 | private static final int SUBTYPE_MASK = 0x00ffffff; |
| 83 | |
| 84 | private static final int PORT_LOWER_BITS_MASK = 0x3f; |
| 85 | private static final long PORT_HIGHER_BITS_MASK = ~PORT_LOWER_BITS_MASK; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 86 | |
| 87 | private final Logger log = getLogger(getClass()); |
| 88 | private ServiceDirectory serviceDirectory; |
| 89 | protected GroupService groupService; |
| 90 | |
| 91 | private DeviceId deviceId; |
| 92 | private FlowObjectiveStore flowObjectiveStore; |
| 93 | private Cache<GroupKey, List<OfdpaNextGroup>> pendingNextObjectives; |
| 94 | private ConcurrentHashMap<GroupKey, Set<GroupChainElem>> pendingGroups; |
| 95 | private ScheduledExecutorService groupChecker = |
| 96 | Executors.newScheduledThreadPool(2, groupedThreads("onos/pipeliner", "ofdpa2-%d")); |
| 97 | |
| 98 | // index number for group creation |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 99 | private AtomicInteger l3VpnIndex = new AtomicInteger(0); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 100 | |
| 101 | // local stores for port-vlan mapping |
| 102 | protected Map<PortNumber, VlanId> port2Vlan = new ConcurrentHashMap<>(); |
| 103 | protected Map<VlanId, Set<PortNumber>> vlan2Port = new ConcurrentHashMap<>(); |
| 104 | |
| 105 | // local store for pending bucketAdds - by design there can only be one |
| 106 | // pending bucket for a group |
| 107 | protected ConcurrentHashMap<Integer, NextObjective> pendingBuckets = new ConcurrentHashMap<>(); |
| 108 | |
| 109 | protected void init(DeviceId deviceId, PipelinerContext context) { |
| 110 | this.deviceId = deviceId; |
| 111 | this.flowObjectiveStore = context.store(); |
| 112 | this.serviceDirectory = context.directory(); |
| 113 | this.groupService = serviceDirectory.get(GroupService.class); |
| 114 | |
| 115 | pendingNextObjectives = CacheBuilder.newBuilder() |
| 116 | .expireAfterWrite(20, TimeUnit.SECONDS) |
| 117 | .removalListener(( |
| 118 | RemovalNotification<GroupKey, List<OfdpaNextGroup>> notification) -> { |
| 119 | if (notification.getCause() == RemovalCause.EXPIRED) { |
| 120 | notification.getValue().forEach(ofdpaNextGrp -> |
| 121 | OFDPA2Pipeline.fail(ofdpaNextGrp.nextObj, |
| 122 | ObjectiveError.GROUPINSTALLATIONFAILED)); |
| 123 | |
| 124 | } |
| 125 | }).build(); |
| 126 | pendingGroups = new ConcurrentHashMap<>(); |
| 127 | groupChecker.scheduleAtFixedRate(new GroupChecker(), 0, 500, TimeUnit.MILLISECONDS); |
| 128 | |
| 129 | groupService.addListener(new InnerGroupListener()); |
| 130 | } |
| 131 | |
| 132 | protected void addGroup(NextObjective nextObjective) { |
| 133 | switch (nextObjective.type()) { |
| 134 | case SIMPLE: |
| 135 | Collection<TrafficTreatment> treatments = nextObjective.next(); |
| 136 | if (treatments.size() != 1) { |
| 137 | log.error("Next Objectives of type Simple should only have a " |
| 138 | + "single Traffic Treatment. Next Objective Id:{}", |
| 139 | nextObjective.id()); |
| 140 | OFDPA2Pipeline.fail(nextObjective, ObjectiveError.BADPARAMS); |
| 141 | return; |
| 142 | } |
| 143 | processSimpleNextObjective(nextObjective); |
| 144 | break; |
| 145 | case BROADCAST: |
| 146 | processBroadcastNextObjective(nextObjective); |
| 147 | break; |
| 148 | case HASHED: |
| 149 | processHashedNextObjective(nextObjective); |
| 150 | break; |
| 151 | case FAILOVER: |
| 152 | OFDPA2Pipeline.fail(nextObjective, ObjectiveError.UNSUPPORTED); |
| 153 | log.warn("Unsupported next objective type {}", nextObjective.type()); |
| 154 | break; |
| 155 | default: |
| 156 | OFDPA2Pipeline.fail(nextObjective, ObjectiveError.UNKNOWN); |
| 157 | log.warn("Unknown next objective type {}", nextObjective.type()); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * As per the OFDPA 2.0 TTP, packets are sent out of ports by using |
| 163 | * a chain of groups. The simple Next Objective passed |
| 164 | * in by the application has to be broken up into a group chain |
| 165 | * comprising of an L3 Unicast Group that points to an L2 Interface |
| 166 | * Group which in-turn points to an output port. In some cases, the simple |
| 167 | * next Objective can just be an L2 interface without the need for chaining. |
| 168 | * |
| 169 | * @param nextObj the nextObjective of type SIMPLE |
| 170 | */ |
| 171 | private void processSimpleNextObjective(NextObjective nextObj) { |
| 172 | TrafficTreatment treatment = nextObj.next().iterator().next(); |
| 173 | // determine if plain L2 or L3->L2 |
| 174 | boolean plainL2 = true; |
| 175 | for (Instruction ins : treatment.allInstructions()) { |
| 176 | if (ins.type() == Instruction.Type.L2MODIFICATION) { |
| 177 | L2ModificationInstruction l2ins = (L2ModificationInstruction) ins; |
| 178 | if (l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_DST || |
| 179 | l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_SRC) { |
| 180 | plainL2 = false; |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | if (plainL2) { |
| 187 | createL2InterfaceGroup(nextObj); |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | // break up simple next objective to GroupChain objects |
| 192 | GroupInfo groupInfo = createL2L3Chain(treatment, nextObj.id(), |
| 193 | nextObj.appId(), false, |
| 194 | nextObj.meta()); |
| 195 | if (groupInfo == null) { |
| 196 | log.error("Could not process nextObj={} in dev:{}", nextObj.id(), deviceId); |
| 197 | return; |
| 198 | } |
| 199 | // create object for local and distributed storage |
| 200 | Deque<GroupKey> gkeyChain = new ArrayDeque<>(); |
| 201 | gkeyChain.addFirst(groupInfo.innerGrpDesc.appCookie()); |
| 202 | gkeyChain.addFirst(groupInfo.outerGrpDesc.appCookie()); |
| 203 | OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup( |
| 204 | Collections.singletonList(gkeyChain), |
| 205 | nextObj); |
| 206 | |
| 207 | // store l3groupkey with the ofdpaNextGroup for the nextObjective that depends on it |
| 208 | updatePendingNextObjective(groupInfo.outerGrpDesc.appCookie(), ofdpaGrp); |
| 209 | |
| 210 | // now we are ready to send the l2 groupDescription (inner), as all the stores |
| 211 | // that will get async replies have been updated. By waiting to update |
| 212 | // the stores, we prevent nasty race conditions. |
| 213 | groupService.addGroup(groupInfo.innerGrpDesc); |
| 214 | } |
| 215 | |
| 216 | private void updatePendingNextObjective(GroupKey key, OfdpaNextGroup value) { |
| 217 | List<OfdpaNextGroup> nextList = new CopyOnWriteArrayList<OfdpaNextGroup>(); |
| 218 | nextList.add(value); |
| 219 | List<OfdpaNextGroup> ret = pendingNextObjectives.asMap() |
| 220 | .putIfAbsent(key, nextList); |
| 221 | if (ret != null) { |
| 222 | ret.add(value); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | private void updatePendingGroups(GroupKey gkey, GroupChainElem gce) { |
| 227 | Set<GroupChainElem> gceSet = Collections.newSetFromMap( |
| 228 | new ConcurrentHashMap<GroupChainElem, Boolean>()); |
| 229 | gceSet.add(gce); |
| 230 | Set<GroupChainElem> retval = pendingGroups.putIfAbsent(gkey, gceSet); |
| 231 | if (retval != null) { |
| 232 | retval.add(gce); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Creates a simple L2 Interface Group. |
| 238 | * |
| 239 | * @param nextObj the next Objective |
| 240 | */ |
| 241 | private void createL2InterfaceGroup(NextObjective nextObj) { |
| 242 | // only allowed actions are vlan pop and outport |
| 243 | TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder(); |
| 244 | PortNumber portNum = null; |
| 245 | for (Instruction ins : nextObj.next().iterator().next().allInstructions()) { |
| 246 | if (ins.type() == Instruction.Type.L2MODIFICATION) { |
| 247 | L2ModificationInstruction l2ins = (L2ModificationInstruction) ins; |
| 248 | switch (l2ins.subtype()) { |
| 249 | case VLAN_POP: |
| 250 | ttb.add(l2ins); |
| 251 | break; |
| 252 | default: |
| 253 | break; |
| 254 | } |
| 255 | } else if (ins.type() == Instruction.Type.OUTPUT) { |
| 256 | portNum = ((Instructions.OutputInstruction) ins).port(); |
| 257 | ttb.add(ins); |
| 258 | } else { |
| 259 | log.warn("Driver does not handle this type of TrafficTreatment" |
| 260 | + " instruction in simple nextObjectives: {}", ins.type()); |
| 261 | } |
| 262 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 263 | |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 264 | VlanId vlanId = readVlanFromMeta(nextObj); |
| 265 | if (vlanId == null) { |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 266 | log.error("Driver cannot process an L2/L3 group chain without " |
| 267 | + "egress vlan information for dev: {} port:{}", |
| 268 | deviceId, portNum); |
| 269 | return; |
| 270 | } |
| 271 | |
| 272 | // assemble information for ofdpa l2interface group |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 273 | int l2groupId = L2_INTERFACE_TYPE | (vlanId.toShort() << 16) | (int) portNum.toLong(); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 274 | // a globally unique groupkey that is different for ports in the same devices |
| 275 | // but different for the same portnumber on different devices. Also different |
| 276 | // for the various group-types created out of the same next objective. |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 277 | int l2gk = l2InterfaceGroupKey(deviceId, vlanId, portNum.toLong()); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 278 | final GroupKey l2groupkey = new DefaultGroupKey(OFDPA2Pipeline.appKryo.serialize(l2gk)); |
| 279 | |
| 280 | // create group description for the l2interfacegroup |
| 281 | GroupBucket l2interfaceGroupBucket = |
| 282 | DefaultGroupBucket.createIndirectGroupBucket(ttb.build()); |
| 283 | GroupDescription l2groupDescription = |
| 284 | new DefaultGroupDescription( |
| 285 | deviceId, |
| 286 | GroupDescription.Type.INDIRECT, |
| 287 | new GroupBuckets(Collections.singletonList( |
| 288 | l2interfaceGroupBucket)), |
| 289 | l2groupkey, |
| 290 | l2groupId, |
| 291 | nextObj.appId()); |
| 292 | log.debug("Trying L2Interface: device:{} gid:{} gkey:{} nextId:{}", |
| 293 | deviceId, Integer.toHexString(l2groupId), |
| 294 | l2groupkey, nextObj.id()); |
| 295 | |
| 296 | // create object for local and distributed storage |
| 297 | Deque<GroupKey> singleKey = new ArrayDeque<>(); |
| 298 | singleKey.addFirst(l2groupkey); |
| 299 | OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup( |
| 300 | Collections.singletonList(singleKey), |
| 301 | nextObj); |
| 302 | |
| 303 | // store l2groupkey for the nextObjective that depends on it |
| 304 | updatePendingNextObjective(l2groupkey, ofdpaGrp); |
| 305 | // send the group description to the group service |
| 306 | groupService.addGroup(l2groupDescription); |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Creates one of two possible group-chains from the treatment |
| 311 | * passed in. Depending on the MPLS boolean, this method either creates |
| 312 | * an L3Unicast Group --> L2Interface Group, if mpls is false; |
| 313 | * or MPLSInterface Group --> L2Interface Group, if mpls is true; |
| 314 | * The returned 'inner' group description is always the L2 Interface group. |
| 315 | * |
| 316 | * @param treatment that needs to be broken up to create the group chain |
| 317 | * @param nextId of the next objective that needs this group chain |
| 318 | * @param appId of the application that sent this next objective |
| 319 | * @param mpls determines if L3Unicast or MPLSInterface group is created |
| 320 | * @param meta metadata passed in by the application as part of the nextObjective |
| 321 | * @return GroupInfo containing the GroupDescription of the |
| 322 | * L2Interface group(inner) and the GroupDescription of the (outer) |
| 323 | * L3Unicast/MPLSInterface group. May return null if there is an |
| 324 | * error in processing the chain |
| 325 | */ |
| 326 | private GroupInfo createL2L3Chain(TrafficTreatment treatment, int nextId, |
| 327 | ApplicationId appId, boolean mpls, |
| 328 | TrafficSelector meta) { |
| 329 | // for the l2interface group, get vlan and port info |
| 330 | // for the outer group, get the src/dst mac, and vlan info |
| 331 | TrafficTreatment.Builder outerTtb = DefaultTrafficTreatment.builder(); |
| 332 | TrafficTreatment.Builder innerTtb = DefaultTrafficTreatment.builder(); |
| 333 | VlanId vlanid = null; |
| 334 | long portNum = 0; |
| 335 | boolean setVlan = false, popVlan = false; |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 336 | MacAddress srcMac = MacAddress.ZERO; |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 337 | MacAddress dstMac = MacAddress.ZERO; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 338 | for (Instruction ins : treatment.allInstructions()) { |
| 339 | if (ins.type() == Instruction.Type.L2MODIFICATION) { |
| 340 | L2ModificationInstruction l2ins = (L2ModificationInstruction) ins; |
| 341 | switch (l2ins.subtype()) { |
| 342 | case ETH_DST: |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 343 | dstMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac(); |
| 344 | outerTtb.setEthDst(dstMac); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 345 | break; |
| 346 | case ETH_SRC: |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 347 | srcMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac(); |
| 348 | outerTtb.setEthSrc(srcMac); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 349 | break; |
| 350 | case VLAN_ID: |
| 351 | vlanid = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId(); |
| 352 | outerTtb.setVlanId(vlanid); |
| 353 | setVlan = true; |
| 354 | break; |
| 355 | case VLAN_POP: |
| 356 | innerTtb.popVlan(); |
| 357 | popVlan = true; |
| 358 | break; |
| 359 | case DEC_MPLS_TTL: |
| 360 | case MPLS_LABEL: |
| 361 | case MPLS_POP: |
| 362 | case MPLS_PUSH: |
| 363 | case VLAN_PCP: |
| 364 | case VLAN_PUSH: |
| 365 | default: |
| 366 | break; |
| 367 | } |
| 368 | } else if (ins.type() == Instruction.Type.OUTPUT) { |
| 369 | portNum = ((Instructions.OutputInstruction) ins).port().toLong(); |
| 370 | innerTtb.add(ins); |
| 371 | } else { |
| 372 | log.warn("Driver does not handle this type of TrafficTreatment" |
| 373 | + " instruction in nextObjectives: {}", ins.type()); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | if (vlanid == null && meta != null) { |
| 378 | // use metadata if available |
| 379 | Criterion vidCriterion = meta.getCriterion(Criterion.Type.VLAN_VID); |
| 380 | if (vidCriterion != null) { |
| 381 | vlanid = ((VlanIdCriterion) vidCriterion).vlanId(); |
| 382 | } |
| 383 | // if vlan is not set, use the vlan in metadata for outerTtb |
| 384 | if (vlanid != null && !setVlan) { |
| 385 | outerTtb.setVlanId(vlanid); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | if (vlanid == null) { |
| 390 | log.error("Driver cannot process an L2/L3 group chain without " |
| 391 | + "egress vlan information for dev: {} port:{}", |
| 392 | deviceId, portNum); |
| 393 | return null; |
| 394 | } |
| 395 | |
| 396 | if (!setVlan && !popVlan) { |
| 397 | // untagged outgoing port |
| 398 | TrafficTreatment.Builder temp = DefaultTrafficTreatment.builder(); |
| 399 | temp.popVlan(); |
| 400 | innerTtb.build().allInstructions().forEach(i -> temp.add(i)); |
| 401 | innerTtb = temp; |
| 402 | } |
| 403 | |
| 404 | // assemble information for ofdpa l2interface group |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 405 | int l2groupId = L2_INTERFACE_TYPE | (vlanid.toShort() << 16) | (int) portNum; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 406 | // a globally unique groupkey that is different for ports in the same devices |
| 407 | // but different for the same portnumber on different devices. Also different |
| 408 | // for the various group-types created out of the same next objective. |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 409 | int l2gk = l2InterfaceGroupKey(deviceId, vlanid, portNum); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 410 | final GroupKey l2groupkey = new DefaultGroupKey(OFDPA2Pipeline.appKryo.serialize(l2gk)); |
| 411 | |
| 412 | // assemble information for outer group |
| 413 | GroupDescription outerGrpDesc = null; |
| 414 | if (mpls) { |
| 415 | // outer group is MPLSInteface |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 416 | int mplsgroupId = MPLS_INTERFACE_TYPE | (int) portNum; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 417 | // using mplsinterfacemask in groupkey to differentiate from l2interface |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 418 | int mplsgk = MPLS_INTERFACE_TYPE | (SUBTYPE_MASK & (deviceId.hashCode() << 8 | (int) portNum)); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 419 | final GroupKey mplsgroupkey = new DefaultGroupKey(OFDPA2Pipeline.appKryo.serialize(mplsgk)); |
| 420 | outerTtb.group(new DefaultGroupId(l2groupId)); |
| 421 | // create the mpls-interface group description to wait for the |
| 422 | // l2 interface group to be processed |
| 423 | GroupBucket mplsinterfaceGroupBucket = |
| 424 | DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build()); |
| 425 | outerGrpDesc = new DefaultGroupDescription( |
| 426 | deviceId, |
| 427 | GroupDescription.Type.INDIRECT, |
| 428 | new GroupBuckets(Collections.singletonList( |
| 429 | mplsinterfaceGroupBucket)), |
| 430 | mplsgroupkey, |
| 431 | mplsgroupId, |
| 432 | appId); |
| 433 | log.debug("Trying MPLS-Interface: device:{} gid:{} gkey:{} nextid:{}", |
| 434 | deviceId, Integer.toHexString(mplsgroupId), |
| 435 | mplsgroupkey, nextId); |
| 436 | } else { |
| 437 | // outer group is L3Unicast |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 438 | int l3GroupIdHash = Objects.hash(srcMac, dstMac, portNum); |
| 439 | int l3groupId = L3_UNICAST_TYPE | (TYPE_MASK & l3GroupIdHash); |
| 440 | int l3GroupKeyHash = Objects.hash(deviceId, srcMac, dstMac, portNum); |
| 441 | int l3gk = L3_UNICAST_TYPE | (TYPE_MASK & l3GroupKeyHash); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 442 | final GroupKey l3groupkey = new DefaultGroupKey(OFDPA2Pipeline.appKryo.serialize(l3gk)); |
| 443 | outerTtb.group(new DefaultGroupId(l2groupId)); |
| 444 | // create the l3unicast group description to wait for the |
| 445 | // l2 interface group to be processed |
| 446 | GroupBucket l3unicastGroupBucket = |
| 447 | DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build()); |
| 448 | outerGrpDesc = new DefaultGroupDescription( |
| 449 | deviceId, |
| 450 | GroupDescription.Type.INDIRECT, |
| 451 | new GroupBuckets(Collections.singletonList( |
| 452 | l3unicastGroupBucket)), |
| 453 | l3groupkey, |
| 454 | l3groupId, |
| 455 | appId); |
| 456 | log.debug("Trying L3Unicast: device:{} gid:{} gkey:{} nextid:{}", |
| 457 | deviceId, Integer.toHexString(l3groupId), |
| 458 | l3groupkey, nextId); |
| 459 | } |
| 460 | |
| 461 | // store l2groupkey with the groupChainElem for the outer-group that depends on it |
| 462 | GroupChainElem gce = new GroupChainElem(outerGrpDesc, 1, false); |
| 463 | updatePendingGroups(l2groupkey, gce); |
| 464 | |
| 465 | // create group description for the inner l2interfacegroup |
| 466 | GroupBucket l2interfaceGroupBucket = |
| 467 | DefaultGroupBucket.createIndirectGroupBucket(innerTtb.build()); |
| 468 | GroupDescription l2groupDescription = |
| 469 | new DefaultGroupDescription( |
| 470 | deviceId, |
| 471 | GroupDescription.Type.INDIRECT, |
| 472 | new GroupBuckets(Collections.singletonList( |
| 473 | l2interfaceGroupBucket)), |
| 474 | l2groupkey, |
| 475 | l2groupId, |
| 476 | appId); |
| 477 | log.debug("Trying L2Interface: device:{} gid:{} gkey:{} nextId:{}", |
| 478 | deviceId, Integer.toHexString(l2groupId), |
| 479 | l2groupkey, nextId); |
| 480 | return new GroupInfo(l2groupDescription, outerGrpDesc); |
| 481 | |
| 482 | } |
| 483 | |
| 484 | /** |
| 485 | * As per the OFDPA 2.0 TTP, packets are sent out of ports by using |
| 486 | * a chain of groups. The broadcast Next Objective passed in by the application |
| 487 | * has to be broken up into a group chain comprising of an |
| 488 | * L2 Flood group whose buckets point to L2 Interface groups. |
| 489 | * |
| 490 | * @param nextObj the nextObjective of type BROADCAST |
| 491 | */ |
| 492 | private void processBroadcastNextObjective(NextObjective nextObj) { |
| 493 | // break up broadcast next objective to multiple groups |
| 494 | Collection<TrafficTreatment> buckets = nextObj.next(); |
| 495 | |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 496 | VlanId vlanId = readVlanFromMeta(nextObj); |
| 497 | if (vlanId == null) { |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 498 | log.warn("Required VLAN ID info in nextObj metadata but not found. Aborting"); |
| 499 | return; |
| 500 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 501 | |
| 502 | // each treatment is converted to an L2 interface group |
| 503 | List<GroupDescription> l2interfaceGroupDescs = new ArrayList<>(); |
| 504 | List<Deque<GroupKey>> allGroupKeys = new ArrayList<>(); |
| 505 | for (TrafficTreatment treatment : buckets) { |
| 506 | TrafficTreatment.Builder newTreatment = DefaultTrafficTreatment.builder(); |
| 507 | PortNumber portNum = null; |
| 508 | // ensure that the only allowed treatments are pop-vlan and output |
| 509 | for (Instruction ins : treatment.allInstructions()) { |
| 510 | if (ins.type() == Instruction.Type.L2MODIFICATION) { |
| 511 | L2ModificationInstruction l2ins = (L2ModificationInstruction) ins; |
| 512 | switch (l2ins.subtype()) { |
| 513 | case VLAN_POP: |
| 514 | newTreatment.add(l2ins); |
| 515 | break; |
| 516 | default: |
| 517 | log.debug("action {} not permitted for broadcast nextObj", |
| 518 | l2ins.subtype()); |
| 519 | break; |
| 520 | } |
| 521 | } else if (ins.type() == Instruction.Type.OUTPUT) { |
| 522 | portNum = ((Instructions.OutputInstruction) ins).port(); |
| 523 | newTreatment.add(ins); |
| 524 | } else { |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 525 | log.debug("TrafficTreatment of type {} not permitted in " + |
| 526 | " broadcast nextObjective", ins.type()); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 530 | // assemble info for l2 interface group |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 531 | int l2gk = l2InterfaceGroupKey(deviceId, vlanId, portNum.toLong()); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 532 | final GroupKey l2groupkey = new DefaultGroupKey(OFDPA2Pipeline.appKryo.serialize(l2gk)); |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 533 | int l2groupId = L2_INTERFACE_TYPE | (vlanId.toShort() << 16) | |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 534 | (int) portNum.toLong(); |
| 535 | GroupBucket l2interfaceGroupBucket = |
| 536 | DefaultGroupBucket.createIndirectGroupBucket(newTreatment.build()); |
| 537 | GroupDescription l2interfaceGroupDescription = |
| 538 | new DefaultGroupDescription( |
| 539 | deviceId, |
| 540 | GroupDescription.Type.INDIRECT, |
| 541 | new GroupBuckets(Collections.singletonList( |
| 542 | l2interfaceGroupBucket)), |
| 543 | l2groupkey, |
| 544 | l2groupId, |
| 545 | nextObj.appId()); |
| 546 | log.debug("Trying L2-Interface: device:{} gid:{} gkey:{} nextid:{}", |
| 547 | deviceId, Integer.toHexString(l2groupId), |
| 548 | l2groupkey, nextObj.id()); |
| 549 | |
| 550 | Deque<GroupKey> gkeyChain = new ArrayDeque<>(); |
| 551 | gkeyChain.addFirst(l2groupkey); |
| 552 | |
| 553 | // store the info needed to create this group |
| 554 | l2interfaceGroupDescs.add(l2interfaceGroupDescription); |
| 555 | allGroupKeys.add(gkeyChain); |
| 556 | } |
| 557 | |
| 558 | // assemble info for l2 flood group |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 559 | Integer l2floodgroupId = L2_FLOOD_TYPE | (vlanId.toShort() << 16) | nextObj.id(); |
| 560 | int l2floodgk = L2_FLOOD_TYPE | nextObj.id() << 12; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 561 | final GroupKey l2floodgroupkey = new DefaultGroupKey(OFDPA2Pipeline.appKryo.serialize(l2floodgk)); |
| 562 | // collection of group buckets pointing to all the l2 interface groups |
| 563 | List<GroupBucket> l2floodBuckets = new ArrayList<>(); |
| 564 | for (GroupDescription l2intGrpDesc : l2interfaceGroupDescs) { |
| 565 | TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder(); |
| 566 | ttb.group(new DefaultGroupId(l2intGrpDesc.givenGroupId())); |
| 567 | GroupBucket abucket = DefaultGroupBucket.createAllGroupBucket(ttb.build()); |
| 568 | l2floodBuckets.add(abucket); |
| 569 | } |
| 570 | // create the l2flood group-description to wait for all the |
| 571 | // l2interface groups to be processed |
| 572 | GroupDescription l2floodGroupDescription = |
| 573 | new DefaultGroupDescription( |
| 574 | deviceId, |
| 575 | GroupDescription.Type.ALL, |
| 576 | new GroupBuckets(l2floodBuckets), |
| 577 | l2floodgroupkey, |
| 578 | l2floodgroupId, |
| 579 | nextObj.appId()); |
| 580 | GroupChainElem gce = new GroupChainElem(l2floodGroupDescription, |
| 581 | l2interfaceGroupDescs.size(), |
| 582 | false); |
| 583 | log.debug("Trying L2-Flood: device:{} gid:{} gkey:{} nextid:{}", |
| 584 | deviceId, Integer.toHexString(l2floodgroupId), |
| 585 | l2floodgroupkey, nextObj.id()); |
| 586 | |
| 587 | // create objects for local and distributed storage |
| 588 | allGroupKeys.forEach(gkeyChain -> gkeyChain.addFirst(l2floodgroupkey)); |
| 589 | OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj); |
| 590 | |
| 591 | // store l2floodgroupkey with the ofdpaGroupChain for the nextObjective |
| 592 | // that depends on it |
| 593 | updatePendingNextObjective(l2floodgroupkey, ofdpaGrp); |
| 594 | |
| 595 | for (GroupDescription l2intGrpDesc : l2interfaceGroupDescs) { |
| 596 | // store all l2groupkeys with the groupChainElem for the l2floodgroup |
| 597 | // that depends on it |
| 598 | updatePendingGroups(l2intGrpDesc.appCookie(), gce); |
| 599 | // send groups for all l2 interface groups |
| 600 | groupService.addGroup(l2intGrpDesc); |
| 601 | } |
| 602 | } |
| 603 | |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 604 | /** |
| 605 | * As per the OFDPA 2.0 TTP, packets are sent out of ports by using |
| 606 | * a chain of groups. The hashed Next Objective passed in by the application |
| 607 | * has to be broken up into a group chain comprising of an |
| 608 | * L3 ECMP group as the top level group. Buckets of this group can point |
| 609 | * to a variety of groups in a group chain, depending on the whether |
| 610 | * MPLS labels are being pushed or not. |
| 611 | * <p> |
| 612 | * NOTE: We do not create MPLS ECMP groups as they are unimplemented in |
| 613 | * OF-DPA 2.0 (even though it is in the spec). Therefore we do not |
| 614 | * check the nextObjective meta to see what is matching before being |
| 615 | * sent to this nextObjective. |
| 616 | * |
| 617 | * @param nextObj the nextObjective of type HASHED |
| 618 | */ |
| 619 | private void processHashedNextObjective(NextObjective nextObj) { |
| 620 | // storage for all group keys in the chain of groups created |
| 621 | List<Deque<GroupKey>> allGroupKeys = new ArrayList<>(); |
| 622 | List<GroupInfo> unsentGroups = new ArrayList<>(); |
| 623 | createHashBucketChains(nextObj, allGroupKeys, unsentGroups); |
| 624 | |
| 625 | // now we can create the outermost L3 ECMP group |
| 626 | List<GroupBucket> l3ecmpGroupBuckets = new ArrayList<>(); |
| 627 | for (GroupInfo gi : unsentGroups) { |
| 628 | // create ECMP bucket to point to the outer group |
| 629 | TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder(); |
| 630 | ttb.group(new DefaultGroupId(gi.outerGrpDesc.givenGroupId())); |
| 631 | GroupBucket sbucket = DefaultGroupBucket |
| 632 | .createSelectGroupBucket(ttb.build()); |
| 633 | l3ecmpGroupBuckets.add(sbucket); |
| 634 | } |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 635 | int l3ecmpGroupId = L3_ECMP_TYPE | nextObj.id() << 12; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 636 | GroupKey l3ecmpGroupKey = new DefaultGroupKey(OFDPA2Pipeline.appKryo.serialize(l3ecmpGroupId)); |
| 637 | GroupDescription l3ecmpGroupDesc = |
| 638 | new DefaultGroupDescription( |
| 639 | deviceId, |
| 640 | GroupDescription.Type.SELECT, |
| 641 | new GroupBuckets(l3ecmpGroupBuckets), |
| 642 | l3ecmpGroupKey, |
| 643 | l3ecmpGroupId, |
| 644 | nextObj.appId()); |
| 645 | GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc, |
| 646 | l3ecmpGroupBuckets.size(), |
| 647 | false); |
| 648 | |
| 649 | // create objects for local and distributed storage |
| 650 | allGroupKeys.forEach(gkeyChain -> gkeyChain.addFirst(l3ecmpGroupKey)); |
| 651 | OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj); |
| 652 | |
| 653 | // store l3ecmpGroupKey with the ofdpaGroupChain for the nextObjective |
| 654 | // that depends on it |
| 655 | updatePendingNextObjective(l3ecmpGroupKey, ofdpaGrp); |
| 656 | |
| 657 | log.debug("Trying L3ECMP: device:{} gid:{} gkey:{} nextId:{}", |
| 658 | deviceId, Integer.toHexString(l3ecmpGroupId), |
| 659 | l3ecmpGroupKey, nextObj.id()); |
| 660 | // finally we are ready to send the innermost groups |
| 661 | for (GroupInfo gi : unsentGroups) { |
| 662 | log.debug("Sending innermost group {} in group chain on device {} ", |
| 663 | Integer.toHexString(gi.innerGrpDesc.givenGroupId()), deviceId); |
| 664 | updatePendingGroups(gi.outerGrpDesc.appCookie(), l3ecmpGce); |
| 665 | groupService.addGroup(gi.innerGrpDesc); |
| 666 | } |
| 667 | |
| 668 | } |
| 669 | |
| 670 | /** |
| 671 | * Creates group chains for all buckets in a hashed group, and stores the |
| 672 | * GroupInfos and GroupKeys for all the groups in the lists passed in, which |
| 673 | * should be empty. |
| 674 | * <p> |
| 675 | * Does not create the top level ECMP group. Does not actually send the |
| 676 | * groups to the groupService. |
| 677 | * |
| 678 | * @param nextObj the Next Objective with buckets that need to be converted |
| 679 | * to group chains |
| 680 | * @param allGroupKeys a list to store groupKey for each bucket-group-chain |
| 681 | * @param unsentGroups a list to store GroupInfo for each bucket-group-chain |
| 682 | */ |
| 683 | private void createHashBucketChains(NextObjective nextObj, |
| 684 | List<Deque<GroupKey>> allGroupKeys, |
| 685 | List<GroupInfo> unsentGroups) { |
| 686 | // break up hashed next objective to multiple groups |
| 687 | Collection<TrafficTreatment> buckets = nextObj.next(); |
| 688 | |
| 689 | for (TrafficTreatment bucket : buckets) { |
| 690 | //figure out how many labels are pushed in each bucket |
| 691 | int labelsPushed = 0; |
| 692 | MplsLabel innermostLabel = null; |
| 693 | for (Instruction ins : bucket.allInstructions()) { |
| 694 | if (ins.type() == Instruction.Type.L2MODIFICATION) { |
| 695 | L2ModificationInstruction l2ins = (L2ModificationInstruction) ins; |
| 696 | if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_PUSH) { |
| 697 | labelsPushed++; |
| 698 | } |
| 699 | if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_LABEL) { |
| 700 | if (innermostLabel == null) { |
| 701 | innermostLabel = ((L2ModificationInstruction.ModMplsLabelInstruction) l2ins).mplsLabel(); |
| 702 | } |
| 703 | } |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | Deque<GroupKey> gkeyChain = new ArrayDeque<>(); |
| 708 | // XXX we only deal with 0 and 1 label push right now |
| 709 | if (labelsPushed == 0) { |
| 710 | GroupInfo nolabelGroupInfo = createL2L3Chain(bucket, nextObj.id(), |
| 711 | nextObj.appId(), false, |
| 712 | nextObj.meta()); |
| 713 | if (nolabelGroupInfo == null) { |
| 714 | log.error("Could not process nextObj={} in dev:{}", |
| 715 | nextObj.id(), deviceId); |
| 716 | return; |
| 717 | } |
| 718 | gkeyChain.addFirst(nolabelGroupInfo.innerGrpDesc.appCookie()); |
| 719 | gkeyChain.addFirst(nolabelGroupInfo.outerGrpDesc.appCookie()); |
| 720 | |
| 721 | // we can't send the inner group description yet, as we have to |
| 722 | // create the dependent ECMP group first. So we store.. |
| 723 | unsentGroups.add(nolabelGroupInfo); |
| 724 | |
| 725 | } else if (labelsPushed == 1) { |
| 726 | GroupInfo onelabelGroupInfo = createL2L3Chain(bucket, nextObj.id(), |
| 727 | nextObj.appId(), true, |
| 728 | nextObj.meta()); |
| 729 | if (onelabelGroupInfo == null) { |
| 730 | log.error("Could not process nextObj={} in dev:{}", |
| 731 | nextObj.id(), deviceId); |
| 732 | return; |
| 733 | } |
| 734 | // we need to add another group to this chain - the L3VPN group |
| 735 | TrafficTreatment.Builder l3vpnTtb = DefaultTrafficTreatment.builder(); |
| 736 | l3vpnTtb.pushMpls() |
| 737 | .setMpls(innermostLabel) |
| 738 | .setMplsBos(true) |
| 739 | .copyTtlOut() |
| 740 | .group(new DefaultGroupId( |
| 741 | onelabelGroupInfo.outerGrpDesc.givenGroupId())); |
| 742 | GroupBucket l3vpnGrpBkt = |
| 743 | DefaultGroupBucket.createIndirectGroupBucket(l3vpnTtb.build()); |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 744 | int l3vpngroupId = MPLS_L3VPN_SUBTYPE | l3VpnIndex.incrementAndGet(); |
| 745 | int l3vpngk = MPLS_L3VPN_SUBTYPE | nextObj.id() << 12 | l3VpnIndex.get(); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 746 | GroupKey l3vpngroupkey = new DefaultGroupKey(OFDPA2Pipeline.appKryo.serialize(l3vpngk)); |
| 747 | GroupDescription l3vpnGroupDesc = |
| 748 | new DefaultGroupDescription( |
| 749 | deviceId, |
| 750 | GroupDescription.Type.INDIRECT, |
| 751 | new GroupBuckets(Collections.singletonList( |
| 752 | l3vpnGrpBkt)), |
| 753 | l3vpngroupkey, |
| 754 | l3vpngroupId, |
| 755 | nextObj.appId()); |
| 756 | GroupChainElem l3vpnGce = new GroupChainElem(l3vpnGroupDesc, 1, false); |
| 757 | updatePendingGroups(onelabelGroupInfo.outerGrpDesc.appCookie(), l3vpnGce); |
| 758 | |
| 759 | gkeyChain.addFirst(onelabelGroupInfo.innerGrpDesc.appCookie()); |
| 760 | gkeyChain.addFirst(onelabelGroupInfo.outerGrpDesc.appCookie()); |
| 761 | gkeyChain.addFirst(l3vpngroupkey); |
| 762 | |
| 763 | //now we can replace the outerGrpDesc with the one we just created |
| 764 | onelabelGroupInfo.outerGrpDesc = l3vpnGroupDesc; |
| 765 | |
| 766 | // we can't send the innermost group yet, as we have to create |
| 767 | // the dependent ECMP group first. So we store ... |
| 768 | unsentGroups.add(onelabelGroupInfo); |
| 769 | |
| 770 | log.debug("Trying L3VPN: device:{} gid:{} gkey:{} nextId:{}", |
| 771 | deviceId, Integer.toHexString(l3vpngroupId), |
| 772 | l3vpngroupkey, nextObj.id()); |
| 773 | |
| 774 | } else { |
| 775 | log.warn("Driver currently does not handle more than 1 MPLS " |
| 776 | + "labels. Not processing nextObjective {}", nextObj.id()); |
| 777 | return; |
| 778 | } |
| 779 | |
| 780 | // all groups in this chain |
| 781 | allGroupKeys.add(gkeyChain); |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | /** |
| 786 | * Adds a bucket to the top level group of a group-chain, and creates the chain. |
| 787 | * |
| 788 | * @param nextObjective the next group to add a bucket to |
| 789 | * @param next the representation of the existing group-chain for this next objective |
| 790 | */ |
| 791 | protected void addBucketToGroup(NextObjective nextObjective, NextGroup next) { |
| 792 | if (nextObjective.type() != NextObjective.Type.HASHED) { |
| 793 | log.warn("AddBuckets not applied to nextType:{} in dev:{} for next:{}", |
| 794 | nextObjective.type(), deviceId, nextObjective.id()); |
| 795 | return; |
| 796 | } |
| 797 | if (nextObjective.next().size() > 1) { |
| 798 | log.warn("Only one bucket can be added at a time"); |
| 799 | return; |
| 800 | } |
| 801 | // storage for all group keys in the chain of groups created |
| 802 | List<Deque<GroupKey>> allGroupKeys = new ArrayList<>(); |
| 803 | List<GroupInfo> unsentGroups = new ArrayList<>(); |
| 804 | createHashBucketChains(nextObjective, allGroupKeys, unsentGroups); |
| 805 | |
| 806 | // now we can create the outermost L3 ECMP group bucket to add |
| 807 | GroupInfo gi = unsentGroups.get(0); // only one bucket, so only one group-chain |
| 808 | TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder(); |
| 809 | ttb.group(new DefaultGroupId(gi.outerGrpDesc.givenGroupId())); |
| 810 | GroupBucket sbucket = DefaultGroupBucket.createSelectGroupBucket(ttb.build()); |
| 811 | |
| 812 | // recreate the original L3 ECMP group id and description |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 813 | int l3ecmpGroupId = L3_ECMP_TYPE | nextObjective.id() << 12; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 814 | GroupKey l3ecmpGroupKey = new DefaultGroupKey(OFDPA2Pipeline.appKryo.serialize(l3ecmpGroupId)); |
| 815 | |
| 816 | // Although GroupDescriptions are not necessary for adding buckets to |
| 817 | // existing groups, we use one in the GroupChainElem. When the latter is |
| 818 | // processed, the info will be extracted for the bucketAdd call to groupService |
| 819 | GroupDescription l3ecmpGroupDesc = |
| 820 | new DefaultGroupDescription( |
| 821 | deviceId, |
| 822 | GroupDescription.Type.SELECT, |
| 823 | new GroupBuckets(Collections.singletonList(sbucket)), |
| 824 | l3ecmpGroupKey, |
| 825 | l3ecmpGroupId, |
| 826 | nextObjective.appId()); |
| 827 | GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc, 1, true); |
| 828 | |
| 829 | // update original NextGroup with new bucket-chain |
| 830 | // don't need to update pendingNextObjectives -- group already exists |
| 831 | Deque<GroupKey> newBucketChain = allGroupKeys.get(0); |
| 832 | newBucketChain.addFirst(l3ecmpGroupKey); |
| 833 | List<Deque<GroupKey>> allOriginalKeys = OFDPA2Pipeline.appKryo.deserialize(next.data()); |
| 834 | allOriginalKeys.add(newBucketChain); |
| 835 | flowObjectiveStore.putNextGroup(nextObjective.id(), |
| 836 | new OfdpaNextGroup(allOriginalKeys, nextObjective)); |
| 837 | |
| 838 | log.debug("Adding to L3ECMP: device:{} gid:{} gkey:{} nextId:{}", |
| 839 | deviceId, Integer.toHexString(l3ecmpGroupId), |
| 840 | l3ecmpGroupKey, nextObjective.id()); |
| 841 | // send the innermost group |
| 842 | log.debug("Sending innermost group {} in group chain on device {} ", |
| 843 | Integer.toHexString(gi.innerGrpDesc.givenGroupId()), deviceId); |
| 844 | updatePendingGroups(gi.outerGrpDesc.appCookie(), l3ecmpGce); |
| 845 | groupService.addGroup(gi.innerGrpDesc); |
| 846 | |
| 847 | } |
| 848 | |
| 849 | /** |
| 850 | * Removes the bucket in the top level group of a possible group-chain. Does |
| 851 | * not remove the groups in a group-chain pointed to by this bucket, as they |
| 852 | * may be in use (referenced by other groups) elsewhere. |
| 853 | * |
| 854 | * @param nextObjective the next group to remove a bucket from |
| 855 | * @param next the representation of the existing group-chain for this next objective |
| 856 | */ |
| 857 | protected void removeBucketFromGroup(NextObjective nextObjective, NextGroup next) { |
| 858 | if (nextObjective.type() != NextObjective.Type.HASHED) { |
| 859 | log.warn("RemoveBuckets not applied to nextType:{} in dev:{} for next:{}", |
| 860 | nextObjective.type(), deviceId, nextObjective.id()); |
| 861 | return; |
| 862 | } |
| 863 | Collection<TrafficTreatment> treatments = nextObjective.next(); |
| 864 | TrafficTreatment treatment = treatments.iterator().next(); |
| 865 | // find the bucket to remove by noting the outport, and figuring out the |
| 866 | // top-level group in the group-chain that indirectly references the port |
| 867 | PortNumber outport = null; |
| 868 | for (Instruction ins : treatment.allInstructions()) { |
| 869 | if (ins instanceof Instructions.OutputInstruction) { |
| 870 | outport = ((Instructions.OutputInstruction) ins).port(); |
| 871 | break; |
| 872 | } |
| 873 | } |
| 874 | if (outport == null) { |
| 875 | log.error("next objective {} has no outport", nextObjective.id()); |
| 876 | return; |
| 877 | } |
| 878 | |
| 879 | List<Deque<GroupKey>> allgkeys = OFDPA2Pipeline.appKryo.deserialize(next.data()); |
| 880 | Deque<GroupKey> foundChain = null; |
| 881 | int index = 0; |
| 882 | for (Deque<GroupKey> gkeys : allgkeys) { |
| 883 | GroupKey groupWithPort = gkeys.peekLast(); |
| 884 | Group group = groupService.getGroup(deviceId, groupWithPort); |
| 885 | if (group == null) { |
| 886 | log.warn("Inconsistent group chain"); |
| 887 | continue; |
| 888 | } |
| 889 | // last group in group chain should have a single bucket pointing to port |
| 890 | List<Instruction> lastIns = group.buckets().buckets().iterator() |
| 891 | .next().treatment().allInstructions(); |
| 892 | for (Instruction i : lastIns) { |
| 893 | if (i instanceof Instructions.OutputInstruction) { |
| 894 | PortNumber lastport = ((Instructions.OutputInstruction) i).port(); |
| 895 | if (lastport.equals(outport)) { |
| 896 | foundChain = gkeys; |
| 897 | break; |
| 898 | } |
| 899 | } |
| 900 | } |
| 901 | if (foundChain != null) { |
| 902 | break; |
| 903 | } |
| 904 | index++; |
| 905 | } |
| 906 | if (foundChain != null) { |
| 907 | //first groupkey is the one we want to modify |
| 908 | GroupKey modGroupKey = foundChain.peekFirst(); |
| 909 | Group modGroup = groupService.getGroup(deviceId, modGroupKey); |
| 910 | //second groupkey is the one we wish to remove the reference to |
| 911 | GroupKey pointedGroupKey = null; |
| 912 | int i = 0; |
| 913 | for (GroupKey gk : foundChain) { |
| 914 | if (i++ == 1) { |
| 915 | pointedGroupKey = gk; |
| 916 | break; |
| 917 | } |
| 918 | } |
| 919 | Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey); |
| 920 | GroupBucket bucket = DefaultGroupBucket.createSelectGroupBucket( |
| 921 | DefaultTrafficTreatment.builder() |
| 922 | .group(pointedGroup.id()) |
| 923 | .build()); |
| 924 | GroupBuckets removeBuckets = new GroupBuckets(Collections |
| 925 | .singletonList(bucket)); |
| 926 | log.debug("Removing buckets from group id {} for next id {} in device {}", |
| 927 | modGroup.id(), nextObjective.id(), deviceId); |
| 928 | groupService.removeBucketsFromGroup(deviceId, modGroupKey, |
| 929 | removeBuckets, modGroupKey, |
| 930 | nextObjective.appId()); |
| 931 | //update store |
| 932 | allgkeys.remove(index); |
| 933 | flowObjectiveStore.putNextGroup(nextObjective.id(), |
| 934 | new OfdpaNextGroup(allgkeys, nextObjective)); |
| 935 | } else { |
| 936 | log.warn("Could not find appropriate group-chain for removing bucket" |
| 937 | + " for next id {} in dev:{}", nextObjective.id(), deviceId); |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | /** |
| 942 | * Removes all groups in multiple possible group-chains that represent the next |
| 943 | * objective. |
| 944 | * |
| 945 | * @param nextObjective the next objective to remove |
| 946 | * @param next the NextGroup that represents the existing group-chain for |
| 947 | * this next objective |
| 948 | */ |
| 949 | protected void removeGroup(NextObjective nextObjective, NextGroup next) { |
| 950 | List<Deque<GroupKey>> allgkeys = OFDPA2Pipeline.appKryo.deserialize(next.data()); |
| 951 | allgkeys.forEach(groupChain -> groupChain.forEach(groupKey -> |
| 952 | groupService.removeGroup(deviceId, groupKey, nextObjective.appId()))); |
| 953 | flowObjectiveStore.removeNextGroup(nextObjective.id()); |
| 954 | } |
| 955 | |
| 956 | /** |
| 957 | * Processes next element of a group chain. Assumption is that if this |
| 958 | * group points to another group, the latter has already been created |
| 959 | * and this driver has received notification for it. A second assumption is |
| 960 | * that if there is another group waiting for this group then the appropriate |
| 961 | * stores already have the information to act upon the notification for the |
| 962 | * creation of this group. |
| 963 | * <p> |
| 964 | * The processing of the GroupChainElement depends on the number of groups |
| 965 | * this element is waiting on. For all group types other than SIMPLE, a |
| 966 | * GroupChainElement could be waiting on multiple groups. |
| 967 | * |
| 968 | * @param gce the group chain element to be processed next |
| 969 | */ |
| 970 | private void processGroupChain(GroupChainElem gce) { |
| 971 | int waitOnGroups = gce.decrementAndGetGroupsWaitedOn(); |
| 972 | if (waitOnGroups != 0) { |
| 973 | log.debug("GCE: {} not ready to be processed", gce); |
| 974 | return; |
| 975 | } |
| 976 | log.debug("GCE: {} ready to be processed", gce); |
| 977 | if (gce.addBucketToGroup) { |
| 978 | groupService.addBucketsToGroup(gce.groupDescription.deviceId(), |
| 979 | gce.groupDescription.appCookie(), |
| 980 | gce.groupDescription.buckets(), |
| 981 | gce.groupDescription.appCookie(), |
| 982 | gce.groupDescription.appId()); |
| 983 | } else { |
| 984 | groupService.addGroup(gce.groupDescription); |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | private class GroupChecker implements Runnable { |
| 989 | @Override |
| 990 | public void run() { |
| 991 | Set<GroupKey> keys = pendingGroups.keySet().stream() |
| 992 | .filter(key -> groupService.getGroup(deviceId, key) != null) |
| 993 | .collect(Collectors.toSet()); |
| 994 | Set<GroupKey> otherkeys = pendingNextObjectives.asMap().keySet().stream() |
| 995 | .filter(otherkey -> groupService.getGroup(deviceId, otherkey) != null) |
| 996 | .collect(Collectors.toSet()); |
| 997 | keys.addAll(otherkeys); |
| 998 | |
| 999 | keys.stream().forEach(key -> |
| 1000 | processPendingGroupsOrNextObjectives(key, false)); |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | private void processPendingGroupsOrNextObjectives(GroupKey key, boolean added) { |
| 1005 | //first check for group chain |
| 1006 | Set<GroupChainElem> gceSet = pendingGroups.remove(key); |
| 1007 | if (gceSet != null) { |
| 1008 | for (GroupChainElem gce : gceSet) { |
| 1009 | log.info("Group service {} group key {} in device {}. " |
| 1010 | + "Processing next group in group chain with group id {}", |
| 1011 | (added) ? "ADDED" : "processed", |
| 1012 | key, deviceId, |
| 1013 | Integer.toHexString(gce.groupDescription.givenGroupId())); |
| 1014 | processGroupChain(gce); |
| 1015 | } |
| 1016 | } else { |
| 1017 | // otherwise chain complete - check for waiting nextObjectives |
| 1018 | List<OfdpaNextGroup> nextGrpList = pendingNextObjectives.getIfPresent(key); |
| 1019 | if (nextGrpList != null) { |
| 1020 | pendingNextObjectives.invalidate(key); |
| 1021 | nextGrpList.forEach(nextGrp -> { |
| 1022 | log.info("Group service {} group key {} in device:{}. " |
| 1023 | + "Done implementing next objective: {} <<-->> gid:{}", |
| 1024 | (added) ? "ADDED" : "processed", |
| 1025 | key, deviceId, nextGrp.nextObjective().id(), |
| 1026 | Integer.toHexString(groupService.getGroup(deviceId, key) |
| 1027 | .givenGroupId())); |
| 1028 | OFDPA2Pipeline.pass(nextGrp.nextObjective()); |
| 1029 | flowObjectiveStore.putNextGroup(nextGrp.nextObjective().id(), nextGrp); |
| 1030 | // check if addBuckets waiting for this completion |
| 1031 | NextObjective pendBkt = pendingBuckets |
| 1032 | .remove(nextGrp.nextObjective().id()); |
| 1033 | if (pendBkt != null) { |
| 1034 | addBucketToGroup(pendBkt, nextGrp); |
| 1035 | } |
| 1036 | }); |
| 1037 | } |
| 1038 | } |
| 1039 | } |
| 1040 | |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 1041 | private VlanId readVlanFromMeta(NextObjective nextObj) { |
| 1042 | TrafficSelector metadata = nextObj.meta(); |
| 1043 | Criterion criterion = metadata.getCriterion(Criterion.Type.VLAN_VID); |
| 1044 | return (criterion == null) |
| 1045 | ? null : ((VlanIdCriterion) criterion).vlanId(); |
| 1046 | } |
| 1047 | |
| 1048 | /** |
| 1049 | * Returns a hash as the L2 Interface Group Key. |
| 1050 | * |
| 1051 | * Keep the lower 6-bit for port since port number usually smaller than 64. |
| 1052 | * Hash other information into remaining 28 bits. |
| 1053 | * |
| 1054 | * @param deviceId Device ID |
| 1055 | * @param vlanId VLAN ID |
| 1056 | * @param portNumber Port number |
| 1057 | * @return L2 interface group key |
| 1058 | */ |
| 1059 | private int l2InterfaceGroupKey( |
| 1060 | DeviceId deviceId, VlanId vlanId, long portNumber) { |
| 1061 | int portLowerBits = (int) portNumber & PORT_LOWER_BITS_MASK; |
| 1062 | long portHigherBits = portNumber & PORT_HIGHER_BITS_MASK; |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 1063 | int hash = Objects.hash(deviceId, vlanId, portHigherBits); |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 1064 | return L2_INTERFACE_TYPE | (TYPE_MASK & hash << 6) | portLowerBits; |
| 1065 | } |
| 1066 | |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1067 | private class InnerGroupListener implements GroupListener { |
| 1068 | @Override |
| 1069 | public void event(GroupEvent event) { |
| 1070 | log.trace("received group event of type {}", event.type()); |
| 1071 | if (event.type() == GroupEvent.Type.GROUP_ADDED) { |
| 1072 | GroupKey key = event.subject().appCookie(); |
| 1073 | processPendingGroupsOrNextObjectives(key, true); |
| 1074 | } |
| 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | /** |
| 1079 | * Utility class for moving group information around. |
| 1080 | */ |
| 1081 | private class GroupInfo { |
| 1082 | private GroupDescription innerGrpDesc; |
| 1083 | private GroupDescription outerGrpDesc; |
| 1084 | |
| 1085 | GroupInfo(GroupDescription innerGrpDesc, GroupDescription outerGrpDesc) { |
| 1086 | this.innerGrpDesc = innerGrpDesc; |
| 1087 | this.outerGrpDesc = outerGrpDesc; |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | /** |
| 1092 | * Represents an entire group-chain that implements a Next-Objective from |
| 1093 | * the application. The objective is represented as a list of deques, where |
| 1094 | * each deque is a separate chain of groups. |
| 1095 | * <p> |
| 1096 | * For example, an ECMP group with 3 buckets, where each bucket points to |
| 1097 | * a group chain of L3 Unicast and L2 interface groups will look like this: |
| 1098 | * <ul> |
| 1099 | * <li>List[0] is a Deque of GroupKeyECMP(first)-GroupKeyL3(middle)-GroupKeyL2(last) |
| 1100 | * <li>List[1] is a Deque of GroupKeyECMP(first)-GroupKeyL3(middle)-GroupKeyL2(last) |
| 1101 | * <li>List[2] is a Deque of GroupKeyECMP(first)-GroupKeyL3(middle)-GroupKeyL2(last) |
| 1102 | * </ul> |
| 1103 | * where the first element of each deque is the same, representing the |
| 1104 | * top level ECMP group, while every other element represents a unique groupKey. |
| 1105 | * <p> |
| 1106 | * Also includes information about the next objective that |
| 1107 | * resulted in this group-chain. |
| 1108 | * |
| 1109 | */ |
| 1110 | protected class OfdpaNextGroup implements NextGroup { |
| 1111 | private final NextObjective nextObj; |
| 1112 | private final List<Deque<GroupKey>> gkeys; |
| 1113 | |
| 1114 | public OfdpaNextGroup(List<Deque<GroupKey>> gkeys, NextObjective nextObj) { |
| 1115 | this.gkeys = gkeys; |
| 1116 | this.nextObj = nextObj; |
| 1117 | } |
| 1118 | |
| 1119 | @SuppressWarnings("unused") |
| 1120 | public List<Deque<GroupKey>> groupKey() { |
| 1121 | return gkeys; |
| 1122 | } |
| 1123 | |
| 1124 | public NextObjective nextObjective() { |
| 1125 | return nextObj; |
| 1126 | } |
| 1127 | |
| 1128 | @Override |
| 1129 | public byte[] data() { |
| 1130 | return OFDPA2Pipeline.appKryo.serialize(gkeys); |
| 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | /** |
| 1135 | * Represents a group element that is part of a chain of groups. |
| 1136 | * Stores enough information to create a Group Description to add the group |
| 1137 | * to the switch by requesting the Group Service. Objects instantiating this |
| 1138 | * class are meant to be temporary and live as long as it is needed to wait for |
| 1139 | * preceding groups in the group chain to be created. |
| 1140 | */ |
| 1141 | private class GroupChainElem { |
| 1142 | private GroupDescription groupDescription; |
| 1143 | private AtomicInteger waitOnGroups; |
| 1144 | private boolean addBucketToGroup; |
| 1145 | |
| 1146 | GroupChainElem(GroupDescription groupDescription, int waitOnGroups, |
| 1147 | boolean addBucketToGroup) { |
| 1148 | this.groupDescription = groupDescription; |
| 1149 | this.waitOnGroups = new AtomicInteger(waitOnGroups); |
| 1150 | this.addBucketToGroup = addBucketToGroup; |
| 1151 | } |
| 1152 | |
| 1153 | /** |
| 1154 | * This methods atomically decrements the counter for the number of |
| 1155 | * groups this GroupChainElement is waiting on, for notifications from |
| 1156 | * the Group Service. When this method returns a value of 0, this |
| 1157 | * GroupChainElement is ready to be processed. |
| 1158 | * |
| 1159 | * @return integer indication of the number of notifications being waited on |
| 1160 | */ |
| 1161 | int decrementAndGetGroupsWaitedOn() { |
| 1162 | return waitOnGroups.decrementAndGet(); |
| 1163 | } |
| 1164 | |
| 1165 | @Override |
| 1166 | public String toString() { |
| 1167 | return (Integer.toHexString(groupDescription.givenGroupId()) + |
| 1168 | " groupKey: " + groupDescription.appCookie() + |
| 1169 | " waiting-on-groups: " + waitOnGroups.get() + |
| 1170 | " addBucketToGroup: " + addBucketToGroup + |
| 1171 | " device: " + deviceId); |
| 1172 | } |
| 1173 | } |
| 1174 | } |