Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [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 | */ |
| 16 | |
Andrea Campanella | 0288c87 | 2017-08-07 18:32:51 +0200 | [diff] [blame] | 17 | package org.onosproject.drivers.p4runtime; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 18 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 19 | import com.google.common.collect.Maps; |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 20 | import org.onosproject.drivers.p4runtime.mirror.P4RuntimeGroupMirror; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 21 | import org.onosproject.net.DeviceId; |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 22 | import org.onosproject.net.group.DefaultGroup; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 23 | import org.onosproject.net.group.Group; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 24 | import org.onosproject.net.group.GroupOperation; |
| 25 | import org.onosproject.net.group.GroupOperations; |
| 26 | import org.onosproject.net.group.GroupProgrammable; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 27 | import org.onosproject.net.group.GroupStore; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 28 | import org.onosproject.net.pi.model.PiActionProfileId; |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 29 | import org.onosproject.net.pi.model.PiActionProfileModel; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 30 | import org.onosproject.net.pi.runtime.PiActionGroup; |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 31 | import org.onosproject.net.pi.runtime.PiActionGroupHandle; |
| 32 | import org.onosproject.net.pi.service.PiGroupTranslator; |
| 33 | import org.onosproject.net.pi.service.PiTranslatedEntity; |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 34 | import org.onosproject.net.pi.service.PiTranslationException; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 35 | import org.slf4j.Logger; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 36 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 37 | import java.util.Collection; |
| 38 | import java.util.Collections; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 39 | import java.util.Map; |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 40 | import java.util.Objects; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 41 | import java.util.concurrent.CompletableFuture; |
| 42 | import java.util.concurrent.ExecutionException; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 43 | import java.util.concurrent.locks.Lock; |
| 44 | import java.util.concurrent.locks.ReentrantLock; |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 45 | import java.util.stream.Collectors; |
| 46 | import java.util.stream.Stream; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 47 | |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 48 | import static org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType.DELETE; |
| 49 | import static org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType.INSERT; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 50 | import static org.slf4j.LoggerFactory.getLogger; |
| 51 | |
| 52 | /** |
| 53 | * Implementation of the group programmable behaviour for P4Runtime. |
| 54 | */ |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 55 | public class P4RuntimeGroupProgrammable |
| 56 | extends AbstractP4RuntimeHandlerBehaviour |
| 57 | implements GroupProgrammable { |
| 58 | |
| 59 | private enum Operation { |
| 60 | APPLY, REMOVE |
| 61 | } |
| 62 | |
| 63 | private static final String ACT_GRP_MEMS_STR = "action group members"; |
| 64 | private static final String DELETE_STR = "delete"; |
| 65 | private static final String ACT_GRP_STR = "action group"; |
| 66 | private static final String INSERT_STR = "insert"; |
| 67 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 68 | private static final Logger log = getLogger(P4RuntimeGroupProgrammable.class); |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 69 | |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 70 | // If true, we ignore re-installing groups that are already known in the |
| 71 | // device mirror. |
| 72 | private boolean checkMirrorBeforeUpdate = true; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 73 | |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 74 | private GroupStore groupStore; |
| 75 | private P4RuntimeGroupMirror groupMirror; |
| 76 | private PiGroupTranslator translator; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 77 | |
| 78 | // Needed to synchronize operations over the same group. |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 79 | private static final Map<PiActionGroupHandle, Lock> GROUP_LOCKS = |
| 80 | Maps.newConcurrentMap(); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 81 | |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 82 | @Override |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 83 | protected boolean setupBehaviour() { |
| 84 | if (!super.setupBehaviour()) { |
| 85 | return false; |
| 86 | } |
| 87 | groupMirror = this.handler().get(P4RuntimeGroupMirror.class); |
| 88 | groupStore = handler().get(GroupStore.class); |
| 89 | translator = piTranslationService.groupTranslator(); |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public void performGroupOperation(DeviceId deviceId, |
| 95 | GroupOperations groupOps) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 96 | if (!setupBehaviour()) { |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 97 | return; |
| 98 | } |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 99 | groupOps.operations().forEach(op -> processGroupOp(deviceId, op)); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 102 | @Override |
| 103 | public Collection<Group> getGroups() { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 104 | if (!setupBehaviour()) { |
| 105 | return Collections.emptyList(); |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 106 | } |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 107 | return pipeconf.pipelineModel().actionProfiles().stream() |
| 108 | .map(PiActionProfileModel::id) |
| 109 | .flatMap(this::streamGroupsFromDevice) |
| 110 | .collect(Collectors.toList()); |
| 111 | } |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 112 | |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 113 | private void processGroupOp(DeviceId deviceId, GroupOperation groupOp) { |
| 114 | final Group pdGroup = groupStore.getGroup(deviceId, groupOp.groupId()); |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 115 | |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 116 | final PiActionGroup piGroup; |
| 117 | try { |
| 118 | piGroup = translator.translate(pdGroup, pipeconf); |
| 119 | } catch (PiTranslationException e) { |
| 120 | log.warn("Unable translate group, aborting {} operation: {}", |
| 121 | groupOp.opType(), e.getMessage()); |
| 122 | return; |
| 123 | } |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 124 | |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 125 | final PiActionGroupHandle handle = PiActionGroupHandle.of(deviceId, piGroup); |
| 126 | |
| 127 | final PiActionGroup groupOnDevice = groupMirror.get(handle) == null |
| 128 | ? null |
| 129 | : groupMirror.get(handle).entry(); |
| 130 | |
| 131 | final Lock lock = GROUP_LOCKS.computeIfAbsent(handle, k -> new ReentrantLock()); |
| 132 | lock.lock(); |
| 133 | try { |
| 134 | final Operation operation; |
| 135 | switch (groupOp.opType()) { |
| 136 | case ADD: |
| 137 | case MODIFY: |
| 138 | operation = Operation.APPLY; |
| 139 | break; |
| 140 | case DELETE: |
| 141 | operation = Operation.REMOVE; |
| 142 | break; |
| 143 | default: |
| 144 | log.warn("Group operation {} not supported", groupOp.opType()); |
| 145 | return; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 146 | } |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 147 | processPiGroup(handle, piGroup, |
| 148 | groupOnDevice, pdGroup, operation); |
| 149 | } finally { |
| 150 | lock.unlock(); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 154 | private void processPiGroup(PiActionGroupHandle handle, |
| 155 | PiActionGroup groupToApply, |
| 156 | PiActionGroup groupOnDevice, |
| 157 | Group pdGroup, Operation operation) { |
| 158 | if (operation == Operation.APPLY) { |
| 159 | if (groupOnDevice != null) { |
| 160 | if (checkMirrorBeforeUpdate |
| 161 | && groupOnDevice.equals(groupToApply)) { |
| 162 | // Group on device has the same members, ignore operation. |
| 163 | return; |
| 164 | } |
| 165 | // Remove before adding it. |
| 166 | processPiGroup(handle, groupToApply, groupOnDevice, |
| 167 | pdGroup, Operation.REMOVE); |
| 168 | } |
| 169 | if (writeGroupToDevice(groupToApply)) { |
| 170 | groupMirror.put(handle, groupToApply); |
| 171 | translator.learn(handle, new PiTranslatedEntity<>( |
| 172 | pdGroup, groupToApply, handle)); |
| 173 | } |
| 174 | } else { |
| 175 | if (deleteGroupFromDevice(groupToApply)) { |
| 176 | groupMirror.remove(handle); |
| 177 | translator.forget(handle); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | private boolean writeGroupToDevice(PiActionGroup groupToApply) { |
| 183 | // First insert members, then group. |
| 184 | // The operation is deemed successful if both operations are successful. |
| 185 | // FIXME: add transactional semantics, i.e. remove members if group fails. |
| 186 | final boolean membersSuccess = completeFuture( |
| 187 | client.writeActionGroupMembers(groupToApply, INSERT, pipeconf), |
| 188 | ACT_GRP_MEMS_STR, INSERT_STR); |
| 189 | return membersSuccess && completeFuture( |
| 190 | client.writeActionGroup(groupToApply, INSERT, pipeconf), |
| 191 | ACT_GRP_STR, INSERT_STR); |
| 192 | } |
| 193 | |
| 194 | private boolean deleteGroupFromDevice(PiActionGroup piActionGroup) { |
| 195 | // First delete group, then members. |
| 196 | // The operation is deemed successful if both operations are successful. |
| 197 | final boolean groupSuccess = completeFuture( |
| 198 | client.writeActionGroup(piActionGroup, DELETE, pipeconf), |
| 199 | ACT_GRP_STR, DELETE_STR); |
| 200 | return groupSuccess && completeFuture( |
| 201 | client.writeActionGroupMembers(piActionGroup, DELETE, pipeconf), |
| 202 | ACT_GRP_MEMS_STR, DELETE_STR); |
| 203 | } |
| 204 | |
| 205 | private boolean completeFuture(CompletableFuture<Boolean> completableFuture, |
| 206 | String topic, String action) { |
| 207 | try { |
| 208 | if (completableFuture.get()) { |
| 209 | return true; |
| 210 | } else { |
| 211 | log.warn("Unable to {} {}", action, topic); |
| 212 | return false; |
| 213 | } |
| 214 | } catch (InterruptedException | ExecutionException e) { |
| 215 | log.warn("Exception while performing {} {}: {}", action, topic, e.getMessage()); |
| 216 | log.debug("Exception", e); |
| 217 | return false; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | private Stream<Group> streamGroupsFromDevice(PiActionProfileId actProfId) { |
| 222 | try { |
| 223 | // Read PI groups and return original PD one. |
| 224 | return client.dumpGroups(actProfId, pipeconf).get().stream() |
| 225 | .map(this::forgeGroupEntry) |
| 226 | .filter(Objects::nonNull); |
| 227 | } catch (ExecutionException | InterruptedException e) { |
| 228 | log.error("Exception while dumping groups from action profile '{}' on {}: {}", |
| 229 | actProfId.id(), deviceId, e); |
| 230 | return Stream.empty(); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | private Group forgeGroupEntry(PiActionGroup piGroup) { |
| 235 | final PiActionGroupHandle handle = PiActionGroupHandle.of(deviceId, piGroup); |
| 236 | if (!translator.lookup(handle).isPresent()) { |
| 237 | log.warn("Missing PI group from translation store: {} - {}:{}", |
| 238 | pipeconf.id(), piGroup.actionProfileId(), |
| 239 | piGroup.id()); |
| 240 | return null; |
| 241 | } |
| 242 | final long life = groupMirror.get(handle) != null |
| 243 | ? groupMirror.get(handle).lifeSec() : 0; |
| 244 | final Group original = translator.lookup(handle).get().original(); |
| 245 | final DefaultGroup forgedGroup = new DefaultGroup(original.id(), original); |
| 246 | forgedGroup.setState(Group.GroupState.ADDED); |
| 247 | forgedGroup.setLife(life); |
| 248 | return forgedGroup; |
| 249 | } |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 250 | } |