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