Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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 | */ |
| 16 | |
| 17 | package org.onosproject.net.pi.impl; |
| 18 | |
Carmelo Cascone | a73f106 | 2018-07-16 22:49:46 +0200 | [diff] [blame] | 19 | import com.google.common.collect.Sets; |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 20 | import org.onosproject.net.Device; |
| 21 | import org.onosproject.net.group.Group; |
| 22 | import org.onosproject.net.group.GroupBucket; |
Carmelo Cascone | a73f106 | 2018-07-16 22:49:46 +0200 | [diff] [blame] | 23 | import org.onosproject.net.group.GroupDescription; |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 24 | import org.onosproject.net.pi.model.PiPipeconf; |
| 25 | import org.onosproject.net.pi.model.PiPipelineInterpreter; |
| 26 | import org.onosproject.net.pi.runtime.PiAction; |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame^] | 27 | import org.onosproject.net.pi.runtime.PiActionProfileGroup; |
| 28 | import org.onosproject.net.pi.runtime.PiActionProfileGroupId; |
| 29 | import org.onosproject.net.pi.runtime.PiActionProfileMember; |
| 30 | import org.onosproject.net.pi.runtime.PiActionProfileMemberId; |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 31 | import org.onosproject.net.pi.runtime.PiGroupKey; |
| 32 | import org.onosproject.net.pi.runtime.PiTableAction; |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 33 | import org.onosproject.net.pi.service.PiTranslationException; |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 34 | |
| 35 | import java.nio.ByteBuffer; |
Carmelo Cascone | a73f106 | 2018-07-16 22:49:46 +0200 | [diff] [blame] | 36 | import java.util.Set; |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 37 | |
| 38 | import static java.lang.String.format; |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 39 | import static org.onosproject.net.pi.impl.PiFlowRuleTranslatorImpl.translateTreatment; |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 40 | import static org.onosproject.net.pi.impl.PiUtils.getInterpreterOrNull; |
| 41 | import static org.onosproject.net.pi.runtime.PiTableAction.Type.ACTION; |
| 42 | |
| 43 | /** |
| 44 | * Implementation of group translation logic. |
| 45 | */ |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 46 | final class PiGroupTranslatorImpl { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 47 | |
Carmelo Cascone | a73f106 | 2018-07-16 22:49:46 +0200 | [diff] [blame] | 48 | private static final Set<GroupDescription.Type> SUPPORTED_GROUP_TYPES = |
| 49 | Sets.immutableEnumSet( |
| 50 | GroupDescription.Type.SELECT, |
| 51 | GroupDescription.Type.INDIRECT); |
| 52 | |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 53 | private PiGroupTranslatorImpl() { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 54 | // Hides constructor. |
| 55 | } |
| 56 | |
| 57 | /** |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame^] | 58 | * Returns a PI action profile group equivalent to the given group, for the given pipeconf and device. |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 59 | * |
| 60 | * @param group group |
| 61 | * @param pipeconf pipeconf |
| 62 | * @param device device |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame^] | 63 | * @return PI action profile group |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 64 | * @throws PiTranslationException if the group cannot be translated |
| 65 | */ |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame^] | 66 | static PiActionProfileGroup translate(Group group, PiPipeconf pipeconf, Device device) |
| 67 | throws PiTranslationException { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 68 | |
Carmelo Cascone | a73f106 | 2018-07-16 22:49:46 +0200 | [diff] [blame] | 69 | if (!SUPPORTED_GROUP_TYPES.contains(group.type())) { |
| 70 | throw new PiTranslationException(format( |
| 71 | "group type %s not supported", group.type())); |
| 72 | } |
| 73 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 74 | final PiPipelineInterpreter interpreter = getInterpreterOrNull(device, pipeconf); |
| 75 | |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame^] | 76 | final PiActionProfileGroup.Builder piActionGroupBuilder = PiActionProfileGroup.builder() |
| 77 | .withId(PiActionProfileGroupId.of(group.id().id())); |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 78 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 79 | if (!(group.appCookie() instanceof PiGroupKey)) { |
Carmelo Cascone | a73f106 | 2018-07-16 22:49:46 +0200 | [diff] [blame] | 80 | throw new PiTranslationException("group app cookie is not PI (class should be PiGroupKey)"); |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 81 | } |
| 82 | final PiGroupKey groupKey = (PiGroupKey) group.appCookie(); |
| 83 | |
| 84 | piActionGroupBuilder.withActionProfileId(groupKey.actionProfileId()); |
| 85 | |
| 86 | // Translate group buckets to PI group members |
| 87 | short bucketIdx = 0; |
| 88 | for (GroupBucket bucket : group.buckets().buckets()) { |
| 89 | /* |
| 90 | FIXME: the way member IDs are computed can cause collisions! |
| 91 | Problem: |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame^] | 92 | In P4Runtime action profile members, i.e. action buckets, are associated to a numeric ID chosen |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 93 | at member insertion time. This ID must be unique for the whole action profile (i.e. the group table in |
| 94 | OpenFlow). In ONOS, GroupBucket doesn't specify any ID. |
| 95 | |
| 96 | Solutions: |
| 97 | - Change GroupBucket API to force application wanting to perform group operations to specify a member id. |
| 98 | - Maintain state to dynamically allocate/deallocate member IDs, e.g. in a dedicated service, or in a |
| 99 | P4Runtime Group Provider. |
| 100 | |
| 101 | Hack: |
| 102 | Statically derive member ID by combining groupId and position of the bucket in the list. |
| 103 | */ |
| 104 | ByteBuffer bb = ByteBuffer.allocate(4) |
| 105 | .putShort((short) (group.id().id() & 0xffff)) |
| 106 | .putShort(bucketIdx); |
| 107 | bb.rewind(); |
| 108 | int memberId = bb.getInt(); |
| 109 | bucketIdx++; |
| 110 | |
| 111 | final PiTableAction tableAction = translateTreatment(bucket.treatment(), interpreter, groupKey.tableId(), |
| 112 | pipeconf.pipelineModel()); |
wu | 66460c9 | 2018-10-23 11:36:30 +0800 | [diff] [blame] | 113 | if (tableAction == null) { |
| 114 | throw new PiTranslationException("The PI table action returned by the interpreter is null"); |
| 115 | } |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 116 | |
| 117 | if (tableAction.type() != ACTION) { |
| 118 | throw new PiTranslationException(format( |
| 119 | "PI table action of type %s is not supported in groups", tableAction.type())); |
| 120 | } |
| 121 | |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame^] | 122 | piActionGroupBuilder.addMember(PiActionProfileMember.builder() |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 123 | .forActionProfile(groupKey.actionProfileId()) |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame^] | 124 | .withId(PiActionProfileMemberId.of(memberId)) |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 125 | .withAction((PiAction) tableAction) |
| 126 | .withWeight(bucket.weight()) |
| 127 | .build()); |
| 128 | } |
| 129 | |
| 130 | return piActionGroupBuilder.build(); |
| 131 | } |
| 132 | } |