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 | |
| 17 | package org.onosproject.drivers.p4runtime; |
| 18 | |
| 19 | import com.google.common.collect.Maps; |
| 20 | import com.google.common.collect.Sets; |
| 21 | import org.onosproject.net.device.DefaultPortStatistics; |
| 22 | import org.onosproject.net.device.PortStatistics; |
| 23 | import org.onosproject.net.device.PortStatisticsDiscovery; |
| 24 | import org.onosproject.net.pi.runtime.PiCounterCellData; |
| 25 | import org.onosproject.net.pi.runtime.PiCounterCellId; |
| 26 | import org.onosproject.net.pi.runtime.PiCounterId; |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 27 | import org.onosproject.net.pi.runtime.PiIndirectCounterCellId; |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 28 | |
| 29 | import java.util.Collection; |
| 30 | import java.util.Collections; |
| 31 | import java.util.Map; |
| 32 | import java.util.Set; |
| 33 | import java.util.concurrent.ExecutionException; |
| 34 | import java.util.stream.Collectors; |
| 35 | |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 36 | import static org.onosproject.net.pi.runtime.PiCounterType.INDIRECT; |
| 37 | |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 38 | /** |
| 39 | * Implementation of a PortStatisticsBehaviour that can be used for any P4 program based on default.p4 (i.e. those |
| 40 | * under onos/tools/test/p4src). |
| 41 | */ |
| 42 | public class DefaultP4PortStatisticsDiscovery extends AbstractP4RuntimeHandlerBehaviour |
| 43 | implements PortStatisticsDiscovery { |
| 44 | |
Carmelo Cascone | cb0a49c | 2017-10-03 14:32:23 +0200 | [diff] [blame^] | 45 | // FIXME: hard-coding the scope here will break support for the P4_14 version of the program. |
| 46 | // With P4_14, counter names in the generated P4Info won't have any scope. |
| 47 | // A solution could be that of dynamically building counter IDs based on the P4Info (as in DefaultP4Interpreter). |
| 48 | private static final String DEFAULT_SCOPE = "port_counters_control"; |
| 49 | private static final String INGRESS_COUNTER = "ingress_port_counter"; |
| 50 | private static final String EGRESS_COUNTER = "egress_port_counter"; |
| 51 | |
| 52 | /** |
| 53 | * Returns the scope string to be used for the counter IDs. |
| 54 | * |
| 55 | * @return scope string |
| 56 | */ |
| 57 | public String scope() { |
| 58 | return DEFAULT_SCOPE; |
| 59 | } |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 60 | |
| 61 | @Override |
| 62 | public Collection<PortStatistics> discoverPortStatistics() { |
| 63 | |
| 64 | if (!super.setupBehaviour()) { |
| 65 | return Collections.emptyList(); |
| 66 | } |
| 67 | |
Carmelo Cascone | cb0a49c | 2017-10-03 14:32:23 +0200 | [diff] [blame^] | 68 | final PiCounterId ingressCounterId = PiCounterId.of(scope(), INGRESS_COUNTER, INDIRECT); |
| 69 | final PiCounterId egressCounterId = PiCounterId.of(scope(), EGRESS_COUNTER, INDIRECT); |
| 70 | |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 71 | Map<Long, DefaultPortStatistics.Builder> portStatBuilders = Maps.newHashMap(); |
| 72 | |
| 73 | deviceService.getPorts(deviceId) |
| 74 | .forEach(p -> portStatBuilders.put(p.number().toLong(), |
| 75 | DefaultPortStatistics.builder() |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 76 | .setPort(p.number()) |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 77 | .setDeviceId(deviceId))); |
| 78 | |
| 79 | Set<PiCounterCellId> counterCellIds = Sets.newHashSet(); |
| 80 | portStatBuilders.keySet().forEach(p -> { |
| 81 | // Counter cell/index = port number. |
Carmelo Cascone | cb0a49c | 2017-10-03 14:32:23 +0200 | [diff] [blame^] | 82 | counterCellIds.add(PiIndirectCounterCellId.of(ingressCounterId, p)); |
| 83 | counterCellIds.add(PiIndirectCounterCellId.of(egressCounterId, p)); |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 84 | }); |
| 85 | |
| 86 | Collection<PiCounterCellData> counterEntryResponse; |
| 87 | try { |
| 88 | counterEntryResponse = client.readCounterCells(counterCellIds, pipeconf).get(); |
| 89 | } catch (InterruptedException | ExecutionException e) { |
| 90 | log.warn("Exception while reading port counters from {}: {}", deviceId, e.toString()); |
| 91 | log.debug("", e); |
| 92 | return Collections.emptyList(); |
| 93 | } |
| 94 | |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 95 | counterEntryResponse.forEach(counterData -> { |
| 96 | if (counterData.cellId().type() != INDIRECT) { |
| 97 | log.warn("Invalid counter data type {}, skipping", counterData.cellId().type()); |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 98 | return; |
| 99 | } |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 100 | PiIndirectCounterCellId indCellId = (PiIndirectCounterCellId) counterData.cellId(); |
| 101 | if (!portStatBuilders.containsKey(indCellId.index())) { |
| 102 | log.warn("Unrecognized counter index {}, skipping", counterData); |
| 103 | return; |
| 104 | } |
| 105 | DefaultPortStatistics.Builder statsBuilder = portStatBuilders.get(indCellId.index()); |
Carmelo Cascone | cb0a49c | 2017-10-03 14:32:23 +0200 | [diff] [blame^] | 106 | if (counterData.cellId().counterId().equals(ingressCounterId)) { |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 107 | statsBuilder.setPacketsReceived(counterData.packets()); |
| 108 | statsBuilder.setBytesReceived(counterData.bytes()); |
Carmelo Cascone | cb0a49c | 2017-10-03 14:32:23 +0200 | [diff] [blame^] | 109 | } else if (counterData.cellId().counterId().equals(egressCounterId)) { |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 110 | statsBuilder.setPacketsSent(counterData.packets()); |
| 111 | statsBuilder.setBytesSent(counterData.bytes()); |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 112 | } else { |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 113 | log.warn("Unrecognized counter ID {}, skipping", counterData); |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 114 | } |
| 115 | }); |
| 116 | |
| 117 | return portStatBuilders |
| 118 | .values() |
| 119 | .stream() |
| 120 | .map(DefaultPortStatistics.Builder::build) |
| 121 | .collect(Collectors.toList()); |
| 122 | } |
| 123 | } |