Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [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; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 18 | |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 19 | import org.onosproject.cluster.ClusterService; |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 20 | import org.onosproject.grpc.utils.AbstractGrpcHandshaker; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 21 | import org.onosproject.net.MastershipRole; |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 22 | import org.onosproject.net.device.DeviceAgentListener; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 23 | import org.onosproject.net.device.DeviceHandshaker; |
pierventre | 64462f1 | 2022-02-24 10:13:06 -0800 | [diff] [blame^] | 24 | import org.onosproject.net.pi.model.PiPipeconf; |
| 25 | import org.onosproject.net.pi.service.PiPipeconfService; |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 26 | import org.onosproject.net.pi.service.PiPipeconfWatchdogService; |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 27 | import org.onosproject.net.provider.ProviderId; |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 28 | import org.onosproject.p4runtime.api.P4RuntimeClient; |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 29 | import org.onosproject.p4runtime.api.P4RuntimeController; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 30 | |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 31 | import java.math.BigInteger; |
pierventre | 64462f1 | 2022-02-24 10:13:06 -0800 | [diff] [blame^] | 32 | import java.util.Optional; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 33 | import java.util.concurrent.CompletableFuture; |
| 34 | |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 35 | import static java.util.concurrent.CompletableFuture.completedFuture; |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 36 | import static org.onosproject.drivers.p4runtime.P4RuntimeDriverUtils.extractP4DeviceId; |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 37 | |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 38 | /** |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 39 | * Implementation of DeviceHandshaker for P4Runtime. |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 40 | */ |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 41 | public class P4RuntimeHandshaker |
| 42 | extends AbstractGrpcHandshaker<P4RuntimeClient, P4RuntimeController> |
| 43 | implements DeviceHandshaker { |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 44 | |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 45 | // This is needed to compute an election ID based on mastership term and |
| 46 | // preference. At the time of writing the practical maximum cluster size is |
| 47 | // 9. Since election IDs are 128bit numbers, we should'nt be too worried of |
| 48 | // being conservative when setting a static max size here. Making the |
| 49 | // cluster size dynamic would likely cause conflicts when generating |
| 50 | // election IDs (e.g. two nodes seeing different cluster sizes). |
| 51 | private static final int MAX_CLUSTER_SIZE = 20; |
| 52 | |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 53 | private Long p4DeviceId; |
| 54 | |
| 55 | public P4RuntimeHandshaker() { |
| 56 | super(P4RuntimeController.class); |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 59 | @Override |
| 60 | protected boolean setupBehaviour(String opName) { |
| 61 | if (!super.setupBehaviour(opName)) { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 62 | return false; |
| 63 | } |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 64 | |
| 65 | p4DeviceId = extractP4DeviceId(mgmtUriFromNetcfg()); |
| 66 | if (p4DeviceId == null) { |
| 67 | log.warn("Unable to obtain the P4Runtime-internal device_id from " + |
| 68 | "config of {}, cannot perform {}", |
| 69 | deviceId, opName); |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 70 | return false; |
| 71 | } |
| 72 | return true; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Override |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 76 | public boolean isAvailable() { |
| 77 | // To be available, we require a session open (for packet in/out) and a |
| 78 | // pipeline config set. |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 79 | if (!setupBehaviour("isAvailable()") || |
| 80 | !client.isServerReachable() || |
| 81 | !client.isSessionOpen(p4DeviceId)) { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 82 | return false; |
| 83 | } |
| 84 | // Since we cannot probe the device, we rely on what's known by the |
| 85 | // pipeconf watchdog service. |
| 86 | return PiPipeconfWatchdogService.PipelineStatus.READY.equals( |
| 87 | handler().get(PiPipeconfWatchdogService.class) |
| 88 | .getStatus(data().deviceId())); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public CompletableFuture<Boolean> probeAvailability() { |
| 93 | // To be available, we require a session open (for packet in/out) and a |
| 94 | // pipeline config set. |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 95 | if (!setupBehaviour("probeAvailability()") || |
| 96 | !client.isServerReachable() || |
| 97 | !client.isSessionOpen(p4DeviceId)) { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 98 | return completedFuture(false); |
| 99 | } |
pierventre | 64462f1 | 2022-02-24 10:13:06 -0800 | [diff] [blame^] | 100 | |
| 101 | PiPipeconfService piPipeconfService = handler().get(PiPipeconfService.class); |
| 102 | final Optional<PiPipeconf> optionalPiPipeconf = piPipeconfService.getPipeconf(deviceId); |
| 103 | if (optionalPiPipeconf.isEmpty()) { |
| 104 | return completedFuture(false); |
| 105 | } |
| 106 | |
| 107 | if (!PiPipeconfWatchdogService.PipelineStatus.READY.equals( |
| 108 | handler().get(PiPipeconfWatchdogService.class) |
| 109 | .getStatus(data().deviceId()))) { |
| 110 | return completedFuture(false); |
| 111 | } |
| 112 | |
| 113 | return client.isPipelineConfigSet(p4DeviceId, optionalPiPipeconf.get()); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | @Override |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 117 | public void roleChanged(MastershipRole newRole) { |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 118 | if (!setupBehaviour("roleChanged()")) { |
| 119 | return; |
| 120 | } |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 121 | if (newRole.equals(MastershipRole.NONE)) { |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 122 | log.info("Notified role NONE, closing session..."); |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 123 | client.closeSession(p4DeviceId); |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 124 | } else { |
| 125 | throw new UnsupportedOperationException( |
| 126 | "Use preference-based way for setting MASTER or STANDBY roles"); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public void roleChanged(int preference, long term) { |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 132 | if (!setupBehaviour("roleChanged()")) { |
| 133 | return; |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 134 | } |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 135 | final int clusterSize = handler().get(ClusterService.class) |
| 136 | .getNodes().size(); |
| 137 | if (clusterSize > MAX_CLUSTER_SIZE) { |
| 138 | throw new IllegalStateException( |
| 139 | "Cluster too big! Maz size supported is " + MAX_CLUSTER_SIZE); |
| 140 | } |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 141 | final BigInteger electionId = BigInteger.valueOf(term) |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 142 | .multiply(BigInteger.valueOf(MAX_CLUSTER_SIZE)) |
| 143 | .subtract(BigInteger.valueOf(preference)); |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 144 | client.setMastership(p4DeviceId, preference == 0, electionId); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 145 | } |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 146 | |
| 147 | @Override |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 148 | public MastershipRole getRole() { |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 149 | if (!setupBehaviour("getRole()") || |
| 150 | !client.isServerReachable() || |
| 151 | !client.isSessionOpen(p4DeviceId)) { |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 152 | return MastershipRole.NONE; |
| 153 | } |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 154 | return client.isMaster(p4DeviceId) |
| 155 | ? MastershipRole.MASTER : MastershipRole.STANDBY; |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | @Override |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 159 | public void addDeviceAgentListener(ProviderId providerId, DeviceAgentListener listener) { |
| 160 | // Don't use controller/deviceId class variables as they might be uninitialized. |
| 161 | handler().get(P4RuntimeController.class) |
| 162 | .addDeviceAgentListener(data().deviceId(), providerId, listener); |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public void removeDeviceAgentListener(ProviderId providerId) { |
Carmelo Cascone | 7044efd | 2018-07-06 13:01:36 +0200 | [diff] [blame] | 167 | // Don't use controller/deviceId class variable as they might be uninitialized. |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 168 | handler().get(P4RuntimeController.class) |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 169 | .removeDeviceAgentListener(data().deviceId(), providerId); |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 170 | } |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 171 | } |