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