Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +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 | |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 17 | package org.onosproject.pipelines.basic; |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 18 | |
| 19 | import com.google.common.collect.Maps; |
| 20 | import com.google.common.collect.Sets; |
Carmelo Cascone | 07bc58e | 2018-08-21 17:06:38 -0700 | [diff] [blame^] | 21 | import org.apache.commons.lang3.tuple.Pair; |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 22 | import org.onosproject.net.DeviceId; |
Carmelo Cascone | 07bc58e | 2018-08-21 17:06:38 -0700 | [diff] [blame^] | 23 | import org.onosproject.net.PortNumber; |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 24 | import org.onosproject.net.device.DefaultPortStatistics; |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 25 | import org.onosproject.net.device.DeviceService; |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 26 | import org.onosproject.net.device.PortStatistics; |
| 27 | import org.onosproject.net.device.PortStatisticsDiscovery; |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 28 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 29 | import org.onosproject.net.pi.model.PiCounterId; |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 30 | import org.onosproject.net.pi.model.PiPipeconf; |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 31 | import org.onosproject.net.pi.runtime.PiCounterCellData; |
| 32 | import org.onosproject.net.pi.runtime.PiCounterCellId; |
Carmelo Cascone | 39c28ca | 2017-11-15 13:03:57 -0800 | [diff] [blame] | 33 | import org.onosproject.net.pi.service.PiPipeconfService; |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 34 | import org.onosproject.p4runtime.api.P4RuntimeClient; |
| 35 | import org.onosproject.p4runtime.api.P4RuntimeController; |
| 36 | import org.slf4j.Logger; |
| 37 | import org.slf4j.LoggerFactory; |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 38 | |
| 39 | import java.util.Collection; |
| 40 | import java.util.Collections; |
| 41 | import java.util.Map; |
| 42 | import java.util.Set; |
| 43 | import java.util.concurrent.ExecutionException; |
| 44 | import java.util.stream.Collectors; |
| 45 | |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 46 | import static org.onosproject.net.pi.model.PiCounterType.INDIRECT; |
| 47 | import static org.onosproject.pipelines.basic.BasicConstants.CNT_EGRESS_PORT_COUNTER_ID; |
| 48 | import static org.onosproject.pipelines.basic.BasicConstants.CNT_INGRESS_PORT_COUNTER_ID; |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 49 | |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 50 | /** |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 51 | * Implementation of the PortStatisticsBehaviour for basic.p4. |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 52 | */ |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 53 | public class PortStatisticsDiscoveryImpl extends AbstractHandlerBehaviour implements PortStatisticsDiscovery { |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 54 | |
Carmelo Cascone | 07bc58e | 2018-08-21 17:06:38 -0700 | [diff] [blame^] | 55 | private static final Map<Pair<DeviceId, PortNumber>, Long> PORT_START_TIMES = |
| 56 | Maps.newConcurrentMap(); |
| 57 | |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 58 | protected final Logger log = LoggerFactory.getLogger(getClass()); |
| 59 | |
Carmelo Cascone | cb0a49c | 2017-10-03 14:32:23 +0200 | [diff] [blame] | 60 | /** |
Carmelo Cascone | 2b05752 | 2017-10-04 14:35:29 +0200 | [diff] [blame] | 61 | * Returns the ID of the ingress port counter. |
Carmelo Cascone | cb0a49c | 2017-10-03 14:32:23 +0200 | [diff] [blame] | 62 | * |
Carmelo Cascone | 2b05752 | 2017-10-04 14:35:29 +0200 | [diff] [blame] | 63 | * @return counter ID |
Carmelo Cascone | cb0a49c | 2017-10-03 14:32:23 +0200 | [diff] [blame] | 64 | */ |
Carmelo Cascone | 2b05752 | 2017-10-04 14:35:29 +0200 | [diff] [blame] | 65 | public PiCounterId ingressCounterId() { |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 66 | return CNT_INGRESS_PORT_COUNTER_ID; |
Carmelo Cascone | 2b05752 | 2017-10-04 14:35:29 +0200 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Returns the ID of the egress port counter. |
| 71 | * |
| 72 | * @return counter ID |
| 73 | */ |
| 74 | public PiCounterId egressCounterId() { |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 75 | return CNT_EGRESS_PORT_COUNTER_ID; |
Carmelo Cascone | cb0a49c | 2017-10-03 14:32:23 +0200 | [diff] [blame] | 76 | } |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 77 | |
| 78 | @Override |
| 79 | public Collection<PortStatistics> discoverPortStatistics() { |
| 80 | |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 81 | DeviceService deviceService = this.handler().get(DeviceService.class); |
| 82 | DeviceId deviceId = this.data().deviceId(); |
| 83 | |
| 84 | PiPipeconfService piPipeconfService = handler().get(PiPipeconfService.class); |
| 85 | if (!piPipeconfService.ofDevice(deviceId).isPresent() || |
| 86 | !piPipeconfService.getPipeconf(piPipeconfService.ofDevice(deviceId).get()).isPresent()) { |
| 87 | log.warn("Unable to get the pipeconf of {}, aborting operation", deviceId); |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 88 | return Collections.emptyList(); |
| 89 | } |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 90 | PiPipeconf pipeconf = piPipeconfService.getPipeconf(piPipeconfService.ofDevice(deviceId).get()).get(); |
| 91 | |
| 92 | P4RuntimeController controller = handler().get(P4RuntimeController.class); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 93 | P4RuntimeClient client = controller.getClient(deviceId); |
| 94 | if (client == null) { |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 95 | log.warn("Unable to find client for {}, aborting operation", deviceId); |
| 96 | return Collections.emptyList(); |
| 97 | } |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 98 | |
| 99 | Map<Long, DefaultPortStatistics.Builder> portStatBuilders = Maps.newHashMap(); |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 100 | deviceService.getPorts(deviceId) |
Carmelo Cascone | 07bc58e | 2018-08-21 17:06:38 -0700 | [diff] [blame^] | 101 | .forEach(p -> portStatBuilders.put( |
| 102 | p.number().toLong(), |
| 103 | DefaultPortStatistics.builder() |
| 104 | .setPort(p.number()) |
| 105 | .setDeviceId(deviceId) |
| 106 | .setDurationSec(getDuration(p.number())))); |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 107 | |
| 108 | Set<PiCounterCellId> counterCellIds = Sets.newHashSet(); |
| 109 | portStatBuilders.keySet().forEach(p -> { |
| 110 | // Counter cell/index = port number. |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 111 | counterCellIds.add(PiCounterCellId.ofIndirect(ingressCounterId(), p)); |
| 112 | counterCellIds.add(PiCounterCellId.ofIndirect(egressCounterId(), p)); |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 113 | }); |
| 114 | |
| 115 | Collection<PiCounterCellData> counterEntryResponse; |
| 116 | try { |
| 117 | counterEntryResponse = client.readCounterCells(counterCellIds, pipeconf).get(); |
| 118 | } catch (InterruptedException | ExecutionException e) { |
| 119 | log.warn("Exception while reading port counters from {}: {}", deviceId, e.toString()); |
| 120 | log.debug("", e); |
| 121 | return Collections.emptyList(); |
| 122 | } |
| 123 | |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 124 | counterEntryResponse.forEach(counterData -> { |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 125 | if (counterData.cellId().counterType() != INDIRECT) { |
| 126 | log.warn("Invalid counter data type {}, skipping", counterData.cellId().counterType()); |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 127 | return; |
| 128 | } |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 129 | PiCounterCellId indCellId = counterData.cellId(); |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 130 | if (!portStatBuilders.containsKey(indCellId.index())) { |
| 131 | log.warn("Unrecognized counter index {}, skipping", counterData); |
| 132 | return; |
| 133 | } |
| 134 | DefaultPortStatistics.Builder statsBuilder = portStatBuilders.get(indCellId.index()); |
Carmelo Cascone | 2b05752 | 2017-10-04 14:35:29 +0200 | [diff] [blame] | 135 | if (counterData.cellId().counterId().equals(ingressCounterId())) { |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 136 | statsBuilder.setPacketsReceived(counterData.packets()); |
| 137 | statsBuilder.setBytesReceived(counterData.bytes()); |
Carmelo Cascone | 2b05752 | 2017-10-04 14:35:29 +0200 | [diff] [blame] | 138 | } else if (counterData.cellId().counterId().equals(egressCounterId())) { |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 139 | statsBuilder.setPacketsSent(counterData.packets()); |
| 140 | statsBuilder.setBytesSent(counterData.bytes()); |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 141 | } else { |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 142 | log.warn("Unrecognized counter ID {}, skipping", counterData); |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 143 | } |
Carmelo Cascone | 07bc58e | 2018-08-21 17:06:38 -0700 | [diff] [blame^] | 144 | |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 145 | }); |
| 146 | |
| 147 | return portStatBuilders |
| 148 | .values() |
| 149 | .stream() |
| 150 | .map(DefaultPortStatistics.Builder::build) |
| 151 | .collect(Collectors.toList()); |
| 152 | } |
Carmelo Cascone | 07bc58e | 2018-08-21 17:06:38 -0700 | [diff] [blame^] | 153 | |
| 154 | private long getDuration(PortNumber port) { |
| 155 | // FIXME: This is a workaround since we cannot determine the port |
| 156 | // duration from a P4 counter. We'll be fixed by gNMI. |
| 157 | final long now = System.currentTimeMillis() / 1000; |
| 158 | final Long startTime = PORT_START_TIMES.putIfAbsent( |
| 159 | Pair.of(data().deviceId(), port), now); |
| 160 | return startTime == null ? now : now - startTime; |
| 161 | } |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 162 | } |