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 | |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 20 | import com.google.common.collect.Sets; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 21 | import com.google.common.util.concurrent.Striped; |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 22 | import org.onosproject.drivers.p4runtime.mirror.P4RuntimeGroupMirror; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 23 | import org.onosproject.drivers.p4runtime.mirror.P4RuntimeMulticastGroupMirror; |
Yi Tseng | 76737cf | 2018-01-31 17:13:21 -0800 | [diff] [blame] | 24 | import org.onosproject.drivers.p4runtime.mirror.TimedEntry; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 25 | import org.onosproject.net.DeviceId; |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 26 | import org.onosproject.net.group.DefaultGroup; |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 27 | import org.onosproject.net.group.DefaultGroupDescription; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 28 | import org.onosproject.net.group.Group; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 29 | import org.onosproject.net.group.GroupDescription; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 30 | import org.onosproject.net.group.GroupOperation; |
| 31 | import org.onosproject.net.group.GroupOperations; |
| 32 | import org.onosproject.net.group.GroupProgrammable; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 33 | import org.onosproject.net.group.GroupStore; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 34 | import org.onosproject.net.pi.model.PiActionProfileId; |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 35 | import org.onosproject.net.pi.model.PiActionProfileModel; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 36 | import org.onosproject.net.pi.runtime.PiActionGroup; |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 37 | import org.onosproject.net.pi.runtime.PiActionGroupHandle; |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 38 | import org.onosproject.net.pi.runtime.PiActionGroupMember; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 39 | import org.onosproject.net.pi.runtime.PiMulticastGroupEntry; |
| 40 | import org.onosproject.net.pi.runtime.PiMulticastGroupEntryHandle; |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 41 | import org.onosproject.net.pi.service.PiGroupTranslator; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 42 | import org.onosproject.net.pi.service.PiMulticastGroupTranslator; |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 43 | import org.onosproject.net.pi.service.PiTranslatedEntity; |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 44 | import org.onosproject.net.pi.service.PiTranslationException; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 45 | import org.onosproject.p4runtime.api.P4RuntimeClient; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 46 | import org.slf4j.Logger; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 47 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 48 | import java.util.Collection; |
| 49 | import java.util.Collections; |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 50 | import java.util.Objects; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 51 | import java.util.concurrent.CompletableFuture; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 52 | import java.util.concurrent.locks.Lock; |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 53 | import java.util.stream.Collectors; |
| 54 | import java.util.stream.Stream; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 55 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 56 | import static java.lang.String.format; |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 57 | import static org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType.DELETE; |
| 58 | import static org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType.INSERT; |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 59 | import static org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType.MODIFY; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 60 | import static org.slf4j.LoggerFactory.getLogger; |
| 61 | |
| 62 | /** |
| 63 | * Implementation of the group programmable behaviour for P4Runtime. |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 64 | * <p> |
| 65 | * This implementation distinguishes between ALL groups, and other types. ALL |
| 66 | * groups are handled via PRE multicast group programming, while other types are |
| 67 | * handled via action profile group programming. |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 68 | */ |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 69 | public class P4RuntimeGroupProgrammable |
| 70 | extends AbstractP4RuntimeHandlerBehaviour |
| 71 | implements GroupProgrammable { |
| 72 | |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 73 | private static final String ACT_GRP_MEMS_STR = "action group members"; |
| 74 | private static final String DELETE_STR = "delete"; |
| 75 | private static final String ACT_GRP_STR = "action group"; |
| 76 | private static final String INSERT_STR = "insert"; |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 77 | private static final String MODIFY_STR = "modify"; |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 78 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 79 | private static final Logger log = getLogger(P4RuntimeGroupProgrammable.class); |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 80 | |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 81 | // If true, we ignore re-installing groups that are already known in the |
| 82 | // device mirror. |
Yi Tseng | f325a60 | 2018-06-27 18:26:33 +0800 | [diff] [blame] | 83 | private static final String CHECK_MIRROR_BEFORE_UPDATE = "checkMirrorBeforeUpdate"; |
| 84 | private static final boolean DEFAULT_CHECK_MIRROR_BEFORE_UPDATE = true; |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 85 | |
Yi Tseng | 76737cf | 2018-01-31 17:13:21 -0800 | [diff] [blame] | 86 | // If true, we avoid querying the device and return what's already known by |
| 87 | // the ONOS store. |
Yi Tseng | f325a60 | 2018-06-27 18:26:33 +0800 | [diff] [blame] | 88 | private static final String IGNORE_DEVICE_WHEN_GET = "ignoreDeviceWhenGet"; |
| 89 | private static final boolean DEFAULT_IGNORE_DEVICE_WHEN_GET = false; |
Yi Tseng | 76737cf | 2018-01-31 17:13:21 -0800 | [diff] [blame] | 90 | |
Esin Karaman | 971fb7f | 2017-12-28 13:44:52 +0000 | [diff] [blame] | 91 | protected GroupStore groupStore; |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 92 | private P4RuntimeGroupMirror groupMirror; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 93 | private PiGroupTranslator groupTranslator; |
| 94 | private P4RuntimeMulticastGroupMirror mcGroupMirror; |
| 95 | private PiMulticastGroupTranslator mcGroupTranslator; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 96 | |
| 97 | // Needed to synchronize operations over the same group. |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 98 | private static final Striped<Lock> STRIPED_LOCKS = Striped.lock(30); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 99 | |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 100 | @Override |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 101 | protected boolean setupBehaviour() { |
| 102 | if (!super.setupBehaviour()) { |
| 103 | return false; |
| 104 | } |
| 105 | groupMirror = this.handler().get(P4RuntimeGroupMirror.class); |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 106 | mcGroupMirror = this.handler().get(P4RuntimeMulticastGroupMirror.class); |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 107 | groupStore = handler().get(GroupStore.class); |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 108 | groupTranslator = piTranslationService.groupTranslator(); |
| 109 | mcGroupTranslator = piTranslationService.multicastGroupTranslator(); |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 110 | return true; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public void performGroupOperation(DeviceId deviceId, |
| 115 | GroupOperations groupOps) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 116 | if (!setupBehaviour()) { |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 117 | return; |
| 118 | } |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 119 | groupOps.operations().forEach(op -> { |
| 120 | // ONOS-7785 We need app cookie (action profile id) from the group |
| 121 | Group groupOnStore = groupStore.getGroup(deviceId, op.groupId()); |
| 122 | GroupDescription groupDesc = new DefaultGroupDescription(deviceId, |
| 123 | op.groupType(), |
| 124 | op.buckets(), |
| 125 | groupOnStore.appCookie(), |
| 126 | op.groupId().id(), |
| 127 | groupOnStore.appId()); |
| 128 | DefaultGroup groupToApply = new DefaultGroup(op.groupId(), groupDesc); |
| 129 | if (op.groupType().equals(GroupDescription.Type.ALL)) { |
| 130 | processMcGroupOp(deviceId, groupToApply, op.opType()); |
| 131 | } else { |
| 132 | |
| 133 | processGroupOp(deviceId, groupToApply, op.opType()); |
| 134 | } |
| 135 | }); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 138 | @Override |
| 139 | public Collection<Group> getGroups() { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 140 | if (!setupBehaviour()) { |
| 141 | return Collections.emptyList(); |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 142 | } |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 143 | final ImmutableList.Builder<Group> groups = ImmutableList.builder(); |
| 144 | |
Yi Tseng | f325a60 | 2018-06-27 18:26:33 +0800 | [diff] [blame] | 145 | if (!driverBoolProperty(IGNORE_DEVICE_WHEN_GET, DEFAULT_IGNORE_DEVICE_WHEN_GET)) { |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 146 | groups.addAll(pipeconf.pipelineModel().actionProfiles().stream() |
| 147 | .map(PiActionProfileModel::id) |
| 148 | .flatMap(this::streamGroupsFromDevice) |
| 149 | .iterator()); |
| 150 | // FIXME: enable reading MC groups from device once reading from |
| 151 | // PRE is supported in PI |
| 152 | // groups.addAll(getMcGroupsFromDevice()); |
Yi Tseng | 76737cf | 2018-01-31 17:13:21 -0800 | [diff] [blame] | 153 | } else { |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 154 | groups.addAll(groupMirror.getAll(deviceId).stream() |
| 155 | .map(TimedEntry::entry) |
| 156 | .map(this::forgeGroupEntry) |
| 157 | .iterator()); |
Yi Tseng | 76737cf | 2018-01-31 17:13:21 -0800 | [diff] [blame] | 158 | } |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 159 | // FIXME: same as before.. |
| 160 | groups.addAll(mcGroupMirror.getAll(deviceId).stream() |
| 161 | .map(TimedEntry::entry) |
| 162 | .map(this::forgeMcGroupEntry) |
| 163 | .iterator()); |
| 164 | |
| 165 | return groups.build(); |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 166 | } |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 167 | |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 168 | private void processGroupOp(DeviceId deviceId, Group pdGroup, GroupOperation.Type opType) { |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 169 | final PiActionGroup piGroup; |
| 170 | try { |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 171 | piGroup = groupTranslator.translate(pdGroup, pipeconf); |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 172 | } catch (PiTranslationException e) { |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 173 | log.warn("Unable to translate group, aborting {} operation: {} [{}]", |
| 174 | opType, e.getMessage(), pdGroup); |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 175 | return; |
| 176 | } |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 177 | final PiActionGroupHandle handle = PiActionGroupHandle.of(deviceId, piGroup); |
| 178 | |
| 179 | final PiActionGroup groupOnDevice = groupMirror.get(handle) == null |
| 180 | ? null |
| 181 | : groupMirror.get(handle).entry(); |
| 182 | |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 183 | final Lock lock = STRIPED_LOCKS.get(handle); |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 184 | lock.lock(); |
| 185 | try { |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 186 | processPiGroup(handle, piGroup, |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 187 | groupOnDevice, pdGroup, opType); |
| 188 | } finally { |
| 189 | lock.unlock(); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | private void processMcGroupOp(DeviceId deviceId, Group pdGroup, GroupOperation.Type opType) { |
| 194 | final PiMulticastGroupEntry mcGroup; |
| 195 | try { |
| 196 | mcGroup = mcGroupTranslator.translate(pdGroup, pipeconf); |
| 197 | } catch (PiTranslationException e) { |
| 198 | log.warn("Unable to translate multicast group, aborting {} operation: {} [{}]", |
| 199 | opType, e.getMessage(), pdGroup); |
| 200 | return; |
| 201 | } |
| 202 | final PiMulticastGroupEntryHandle handle = PiMulticastGroupEntryHandle.of( |
| 203 | deviceId, mcGroup); |
| 204 | final PiMulticastGroupEntry groupOnDevice = mcGroupMirror.get(handle) == null |
| 205 | ? null |
| 206 | : mcGroupMirror.get(handle).entry(); |
| 207 | final Lock lock = STRIPED_LOCKS.get(handle); |
| 208 | lock.lock(); |
| 209 | try { |
| 210 | processMcGroup(handle, mcGroup, |
| 211 | groupOnDevice, pdGroup, opType); |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 212 | } finally { |
| 213 | lock.unlock(); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 217 | private void processPiGroup(PiActionGroupHandle handle, |
| 218 | PiActionGroup groupToApply, |
| 219 | PiActionGroup groupOnDevice, |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 220 | Group pdGroup, GroupOperation.Type operationType) { |
| 221 | if (operationType == GroupOperation.Type.ADD) { |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 222 | if (groupOnDevice != null) { |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 223 | log.warn("Unable to add group {} since group already on device {}", |
| 224 | groupToApply.id(), deviceId); |
| 225 | log.debug("To apply: {}", groupToApply); |
| 226 | log.debug("On device: {}", groupOnDevice); |
| 227 | return; |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 228 | } |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 229 | |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 230 | if (writeGroupToDevice(groupToApply)) { |
| 231 | groupMirror.put(handle, groupToApply); |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 232 | groupTranslator.learn(handle, new PiTranslatedEntity<>( |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 233 | pdGroup, groupToApply, handle)); |
| 234 | } |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 235 | } else if (operationType == GroupOperation.Type.MODIFY) { |
| 236 | if (groupOnDevice == null) { |
| 237 | log.warn("Group {} does not exists on device {}, can not modify it", |
| 238 | groupToApply.id(), deviceId); |
| 239 | return; |
| 240 | } |
Yi Tseng | f325a60 | 2018-06-27 18:26:33 +0800 | [diff] [blame] | 241 | if (driverBoolProperty(CHECK_MIRROR_BEFORE_UPDATE, DEFAULT_CHECK_MIRROR_BEFORE_UPDATE) |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 242 | && groupOnDevice.equals(groupToApply)) { |
| 243 | // Group on device has the same members, ignore operation. |
| 244 | return; |
| 245 | } |
| 246 | if (modifyGroupFromDevice(groupToApply, groupOnDevice)) { |
| 247 | groupMirror.put(handle, groupToApply); |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 248 | groupTranslator.learn(handle, |
| 249 | new PiTranslatedEntity<>(pdGroup, groupToApply, handle)); |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 250 | } |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 251 | } else { |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 252 | if (groupOnDevice == null) { |
| 253 | log.warn("Unable to remove group {} from device {} since it does" + |
| 254 | "not exists on device.", groupToApply.id(), deviceId); |
| 255 | return; |
| 256 | } |
| 257 | if (deleteGroupFromDevice(groupOnDevice)) { |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 258 | groupMirror.remove(handle); |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 259 | groupTranslator.forget(handle); |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 264 | private void processMcGroup(PiMulticastGroupEntryHandle handle, |
| 265 | PiMulticastGroupEntry groupToApply, |
| 266 | PiMulticastGroupEntry groupOnDevice, |
| 267 | Group pdGroup, GroupOperation.Type opType) { |
Carmelo Cascone | 055e9b2 | 2018-09-10 01:59:15 -0700 | [diff] [blame^] | 268 | switch (opType) { |
| 269 | case ADD: |
| 270 | robustMcGroupAdd(handle, groupToApply, pdGroup); |
| 271 | return; |
| 272 | case MODIFY: |
| 273 | // Since reading multicast groups is not supported yet on |
| 274 | // PI/Stratum, we cannot trust groupOnDevic) as we don't have a |
| 275 | // mechanism to enforce consistency of the mirror with the |
| 276 | // device state. |
| 277 | // if (driverBoolProperty(CHECK_MIRROR_BEFORE_UPDATE, |
| 278 | // DEFAULT_CHECK_MIRROR_BEFORE_UPDATE) |
| 279 | // && p4OpType == MODIFY |
| 280 | // && groupOnDevice != null |
| 281 | // && groupOnDevice.equals(groupToApply)) { |
| 282 | // // Ignore. |
| 283 | // return; |
| 284 | // } |
| 285 | robustMcGroupModify(handle, groupToApply, pdGroup); |
| 286 | return; |
| 287 | case DELETE: |
| 288 | mcGroupApply(handle, groupToApply, pdGroup, DELETE); |
| 289 | return; |
| 290 | default: |
| 291 | log.error("Unknown group operation type {}, " + |
| 292 | "cannot process multicast group", opType); |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
| 296 | private boolean writeMcGroupOnDevice(PiMulticastGroupEntry group, P4RuntimeClient.WriteOperationType opType) { |
| 297 | return getFutureWithDeadline( |
| 298 | client.writePreMulticastGroupEntries( |
| 299 | Collections.singleton(group), opType), |
| 300 | "performing multicast group " + opType, false); |
| 301 | } |
| 302 | |
Carmelo Cascone | 055e9b2 | 2018-09-10 01:59:15 -0700 | [diff] [blame^] | 303 | private boolean mcGroupApply(PiMulticastGroupEntryHandle handle, |
| 304 | PiMulticastGroupEntry piGroup, |
| 305 | Group pdGroup, |
| 306 | P4RuntimeClient.WriteOperationType opType) { |
| 307 | switch (opType) { |
| 308 | case DELETE: |
| 309 | if (writeMcGroupOnDevice(piGroup, DELETE)) { |
| 310 | mcGroupMirror.remove(handle); |
| 311 | mcGroupTranslator.forget(handle); |
| 312 | return true; |
| 313 | } else { |
| 314 | return false; |
| 315 | } |
| 316 | case INSERT: |
| 317 | case MODIFY: |
| 318 | if (writeMcGroupOnDevice(piGroup, opType)) { |
| 319 | mcGroupMirror.put(handle, piGroup); |
| 320 | mcGroupTranslator.learn(handle, new PiTranslatedEntity<>( |
| 321 | pdGroup, piGroup, handle)); |
| 322 | return true; |
| 323 | } else { |
| 324 | return false; |
| 325 | } |
| 326 | default: |
| 327 | log.warn("Unknown operation type {}, cannot apply group", opType); |
| 328 | return false; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | private void robustMcGroupAdd(PiMulticastGroupEntryHandle handle, |
| 333 | PiMulticastGroupEntry piGroup, |
| 334 | Group pdGroup) { |
| 335 | if (mcGroupApply(handle, piGroup, pdGroup, INSERT)) { |
| 336 | return; |
| 337 | } |
| 338 | // Try to delete (perhaps it already exists) and re-add... |
| 339 | mcGroupApply(handle, piGroup, pdGroup, DELETE); |
| 340 | mcGroupApply(handle, piGroup, pdGroup, INSERT); |
| 341 | } |
| 342 | |
| 343 | private void robustMcGroupModify(PiMulticastGroupEntryHandle handle, |
| 344 | PiMulticastGroupEntry piGroup, |
| 345 | Group pdGroup) { |
| 346 | if (mcGroupApply(handle, piGroup, pdGroup, MODIFY)) { |
| 347 | return; |
| 348 | } |
| 349 | // Not sure for which reason it cannot be modified, so try to delete and insert instead... |
| 350 | mcGroupApply(handle, piGroup, pdGroup, DELETE); |
| 351 | mcGroupApply(handle, piGroup, pdGroup, INSERT); |
| 352 | } |
| 353 | |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 354 | private boolean modifyGroupFromDevice(PiActionGroup groupToApply, PiActionGroup groupOnDevice) { |
| 355 | PiActionProfileId groupProfileId = groupToApply.actionProfileId(); |
| 356 | Collection<PiActionGroupMember> membersToRemove = Sets.newHashSet(groupOnDevice.members()); |
| 357 | membersToRemove.removeAll(groupToApply.members()); |
| 358 | Collection<PiActionGroupMember> membersToAdd = Sets.newHashSet(groupToApply.members()); |
| 359 | membersToAdd.removeAll(groupOnDevice.members()); |
| 360 | |
| 361 | if (!membersToAdd.isEmpty() && |
| 362 | !completeFuture(client.writeActionGroupMembers(groupProfileId, membersToAdd, INSERT, pipeconf), |
| 363 | ACT_GRP_MEMS_STR, INSERT_STR)) { |
| 364 | // remove what we added |
| 365 | completeFuture(client.writeActionGroupMembers(groupProfileId, membersToAdd, DELETE, pipeconf), |
| 366 | ACT_GRP_MEMS_STR, INSERT_STR); |
| 367 | return false; |
| 368 | } |
| 369 | |
| 370 | if (!completeFuture(client.writeActionGroup(groupToApply, MODIFY, pipeconf), |
| 371 | ACT_GRP_STR, MODIFY_STR)) { |
| 372 | // recover group information |
| 373 | completeFuture(client.writeActionGroup(groupOnDevice, MODIFY, pipeconf), |
| 374 | ACT_GRP_STR, MODIFY_STR); |
| 375 | // remove what we added |
| 376 | completeFuture(client.writeActionGroupMembers(groupProfileId, membersToAdd, DELETE, pipeconf), |
| 377 | ACT_GRP_MEMS_STR, INSERT_STR); |
| 378 | return false; |
| 379 | } |
| 380 | |
| 381 | if (!membersToRemove.isEmpty() && |
| 382 | !completeFuture(client.writeActionGroupMembers(groupProfileId, membersToRemove, DELETE, pipeconf), |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 383 | ACT_GRP_MEMS_STR, DELETE_STR)) { |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 384 | // add what we removed |
| 385 | completeFuture(client.writeActionGroupMembers(groupProfileId, membersToRemove, INSERT, pipeconf), |
| 386 | ACT_GRP_MEMS_STR, DELETE_STR); |
| 387 | // recover group information |
| 388 | completeFuture(client.writeActionGroup(groupOnDevice, MODIFY, pipeconf), |
| 389 | ACT_GRP_STR, MODIFY_STR); |
| 390 | // remove what we added |
| 391 | completeFuture(client.writeActionGroupMembers(groupProfileId, membersToAdd, DELETE, pipeconf), |
| 392 | ACT_GRP_MEMS_STR, INSERT_STR); |
| 393 | return false; |
| 394 | } |
| 395 | |
| 396 | return true; |
| 397 | } |
| 398 | |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 399 | private boolean writeGroupToDevice(PiActionGroup groupToApply) { |
| 400 | // First insert members, then group. |
| 401 | // The operation is deemed successful if both operations are successful. |
| 402 | // FIXME: add transactional semantics, i.e. remove members if group fails. |
| 403 | final boolean membersSuccess = completeFuture( |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 404 | client.writeActionGroupMembers(groupToApply.actionProfileId(), |
| 405 | groupToApply.members(), |
| 406 | INSERT, pipeconf), |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 407 | ACT_GRP_MEMS_STR, INSERT_STR); |
| 408 | return membersSuccess && completeFuture( |
| 409 | client.writeActionGroup(groupToApply, INSERT, pipeconf), |
| 410 | ACT_GRP_STR, INSERT_STR); |
| 411 | } |
| 412 | |
| 413 | private boolean deleteGroupFromDevice(PiActionGroup piActionGroup) { |
| 414 | // First delete group, then members. |
| 415 | // The operation is deemed successful if both operations are successful. |
| 416 | final boolean groupSuccess = completeFuture( |
| 417 | client.writeActionGroup(piActionGroup, DELETE, pipeconf), |
| 418 | ACT_GRP_STR, DELETE_STR); |
| 419 | return groupSuccess && completeFuture( |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 420 | client.writeActionGroupMembers(piActionGroup.actionProfileId(), |
| 421 | piActionGroup.members(), |
| 422 | DELETE, pipeconf), |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 423 | ACT_GRP_MEMS_STR, DELETE_STR); |
| 424 | } |
| 425 | |
| 426 | private boolean completeFuture(CompletableFuture<Boolean> completableFuture, |
| 427 | String topic, String action) { |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 428 | return getFutureWithDeadline( |
| 429 | completableFuture, format("performing %s %s", action, topic), false); |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | private Stream<Group> streamGroupsFromDevice(PiActionProfileId actProfId) { |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 433 | // Read PI groups and return original PD one. |
| 434 | Collection<PiActionGroup> groups = getFutureWithDeadline( |
| 435 | client.dumpGroups(actProfId, pipeconf), |
| 436 | "dumping groups", Collections.emptyList()); |
| 437 | return groups.stream() |
| 438 | .map(this::forgeGroupEntry) |
| 439 | .filter(Objects::nonNull); |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 440 | } |
| 441 | |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 442 | private Collection<Group> getMcGroupsFromDevice() { |
| 443 | Collection<PiMulticastGroupEntry> groups = getFutureWithDeadline( |
| 444 | client.readAllMulticastGroupEntries(), |
| 445 | "dumping multicast groups", Collections.emptyList()); |
| 446 | return groups.stream() |
| 447 | .map(this::forgeMcGroupEntry) |
| 448 | .filter(Objects::nonNull) |
| 449 | .collect(Collectors.toList()); |
| 450 | } |
| 451 | |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 452 | private Group forgeGroupEntry(PiActionGroup piGroup) { |
| 453 | final PiActionGroupHandle handle = PiActionGroupHandle.of(deviceId, piGroup); |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 454 | if (!groupTranslator.lookup(handle).isPresent()) { |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 455 | log.warn("Missing PI group from translation store: {} - {}:{}", |
| 456 | pipeconf.id(), piGroup.actionProfileId(), |
| 457 | piGroup.id()); |
| 458 | return null; |
| 459 | } |
| 460 | final long life = groupMirror.get(handle) != null |
| 461 | ? groupMirror.get(handle).lifeSec() : 0; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 462 | final Group original = groupTranslator.lookup(handle).get().original(); |
| 463 | return addedGroup(original, life); |
| 464 | } |
| 465 | |
| 466 | private Group forgeMcGroupEntry(PiMulticastGroupEntry mcGroup) { |
| 467 | final PiMulticastGroupEntryHandle handle = PiMulticastGroupEntryHandle.of( |
| 468 | deviceId, mcGroup); |
| 469 | if (!mcGroupTranslator.lookup(handle).isPresent()) { |
| 470 | log.warn("Missing PI multicast group {} from translation store", |
| 471 | mcGroup.groupId()); |
| 472 | return null; |
| 473 | } |
| 474 | final long life = mcGroupMirror.get(handle) != null |
| 475 | ? mcGroupMirror.get(handle).lifeSec() : 0; |
| 476 | final Group original = mcGroupTranslator.lookup(handle).get().original(); |
| 477 | return addedGroup(original, life); |
| 478 | } |
| 479 | |
| 480 | private Group addedGroup(Group original, long life) { |
Carmelo Cascone | e75b794 | 2017-11-21 17:14:49 -0800 | [diff] [blame] | 481 | final DefaultGroup forgedGroup = new DefaultGroup(original.id(), original); |
| 482 | forgedGroup.setState(Group.GroupState.ADDED); |
| 483 | forgedGroup.setLife(life); |
| 484 | return forgedGroup; |
| 485 | } |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 486 | } |