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