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