Carmelo Cascone | 770507f | 2017-09-14 20:58:04 +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.p4tutorial.pipeconf; |
| 18 | |
| 19 | import com.google.common.collect.Maps; |
| 20 | import com.google.common.collect.Sets; |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | import org.onosproject.net.device.DefaultPortStatistics; |
| 23 | import org.onosproject.net.device.DeviceService; |
| 24 | import org.onosproject.net.device.PortStatistics; |
| 25 | import org.onosproject.net.device.PortStatisticsDiscovery; |
| 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 | 770507f | 2017-09-14 20:58:04 +0200 | [diff] [blame] | 28 | import org.onosproject.net.pi.model.PiPipeconf; |
| 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 | 770507f | 2017-09-14 20:58:04 +0200 | [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; |
| 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; |
Carmelo Cascone | 770507f | 2017-09-14 20:58:04 +0200 | [diff] [blame] | 45 | |
| 46 | /** |
Carmelo Cascone | 700648c | 2018-04-11 12:02:16 -0700 | [diff] [blame] | 47 | * Implementation of the PortStatisticsDiscovery behaviour for the mytunnel.p4 program. This behaviour works by using a |
Carmelo Cascone | 770507f | 2017-09-14 20:58:04 +0200 | [diff] [blame] | 48 | * P4Runtime client to read the values of the ingress/egress port counters defined in the P4 program. |
| 49 | */ |
| 50 | public final class PortStatisticsDiscoveryImpl extends AbstractHandlerBehaviour implements PortStatisticsDiscovery { |
| 51 | |
| 52 | private static final Logger log = LoggerFactory.getLogger(PortStatisticsDiscoveryImpl.class); |
| 53 | |
Carmelo Cascone | 700648c | 2018-04-11 12:02:16 -0700 | [diff] [blame] | 54 | private static final PiCounterId INGRESS_COUNTER_ID = PiCounterId.of("c_ingress.rx_port_counter"); |
| 55 | private static final PiCounterId EGRESS_COUNTER_ID = PiCounterId.of("c_ingress.tx_port_counter"); |
Carmelo Cascone | 770507f | 2017-09-14 20:58:04 +0200 | [diff] [blame] | 56 | |
| 57 | @Override |
| 58 | public Collection<PortStatistics> discoverPortStatistics() { |
| 59 | |
| 60 | DeviceService deviceService = this.handler().get(DeviceService.class); |
| 61 | DeviceId deviceId = this.data().deviceId(); |
| 62 | |
| 63 | // Get a client for this device. |
| 64 | P4RuntimeController controller = handler().get(P4RuntimeController.class); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 65 | P4RuntimeClient client = controller.getClient(deviceId); |
| 66 | if (client == null) { |
Carmelo Cascone | 770507f | 2017-09-14 20:58:04 +0200 | [diff] [blame] | 67 | log.warn("Unable to find client for {}, aborting operation", deviceId); |
| 68 | return Collections.emptyList(); |
| 69 | } |
Carmelo Cascone | 770507f | 2017-09-14 20:58:04 +0200 | [diff] [blame] | 70 | |
| 71 | // Get the pipeconf of this device. |
| 72 | PiPipeconfService piPipeconfService = handler().get(PiPipeconfService.class); |
| 73 | if (!piPipeconfService.ofDevice(deviceId).isPresent() || |
| 74 | !piPipeconfService.getPipeconf(piPipeconfService.ofDevice(deviceId).get()).isPresent()) { |
| 75 | log.warn("Unable to get the pipeconf of {}, aborting operation", deviceId); |
| 76 | return Collections.emptyList(); |
| 77 | } |
| 78 | PiPipeconf pipeconf = piPipeconfService.getPipeconf(piPipeconfService.ofDevice(deviceId).get()).get(); |
| 79 | |
| 80 | // Prepare PortStatistics objects to return, one per port of this device. |
| 81 | Map<Long, DefaultPortStatistics.Builder> portStatBuilders = Maps.newHashMap(); |
| 82 | deviceService |
| 83 | .getPorts(deviceId) |
| 84 | .forEach(p -> portStatBuilders.put(p.number().toLong(), |
| 85 | DefaultPortStatistics.builder() |
| 86 | .setPort(p.number()) |
| 87 | .setDeviceId(deviceId))); |
| 88 | |
| 89 | // Generate the counter cell IDs. |
| 90 | Set<PiCounterCellId> counterCellIds = Sets.newHashSet(); |
| 91 | portStatBuilders.keySet().forEach(p -> { |
| 92 | // Counter cell/index = port number. |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 93 | counterCellIds.add(PiCounterCellId.ofIndirect(INGRESS_COUNTER_ID, p)); |
| 94 | counterCellIds.add(PiCounterCellId.ofIndirect(EGRESS_COUNTER_ID, p)); |
Carmelo Cascone | 770507f | 2017-09-14 20:58:04 +0200 | [diff] [blame] | 95 | }); |
| 96 | |
| 97 | // Query the device. |
| 98 | Collection<PiCounterCellData> counterEntryResponse; |
| 99 | try { |
| 100 | counterEntryResponse = client.readCounterCells(counterCellIds, pipeconf).get(); |
| 101 | } catch (InterruptedException | ExecutionException e) { |
| 102 | log.warn("Exception while reading port counters from {}: {}", deviceId, e.toString()); |
| 103 | log.debug("", e); |
| 104 | return Collections.emptyList(); |
| 105 | } |
| 106 | |
| 107 | // Process response. |
| 108 | counterEntryResponse.forEach(counterData -> { |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 109 | if (counterData.cellId().counterType() != INDIRECT) { |
| 110 | log.warn("Invalid counter data type {}, skipping", counterData.cellId().counterType()); |
Carmelo Cascone | 770507f | 2017-09-14 20:58:04 +0200 | [diff] [blame] | 111 | return; |
| 112 | } |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 113 | if (!portStatBuilders.containsKey(counterData.cellId().index())) { |
Carmelo Cascone | 770507f | 2017-09-14 20:58:04 +0200 | [diff] [blame] | 114 | log.warn("Unrecognized counter index {}, skipping", counterData); |
| 115 | return; |
| 116 | } |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 117 | DefaultPortStatistics.Builder statsBuilder = portStatBuilders.get(counterData.cellId().index()); |
Carmelo Cascone | 770507f | 2017-09-14 20:58:04 +0200 | [diff] [blame] | 118 | if (counterData.cellId().counterId().equals(INGRESS_COUNTER_ID)) { |
| 119 | statsBuilder.setPacketsReceived(counterData.packets()); |
| 120 | statsBuilder.setBytesReceived(counterData.bytes()); |
| 121 | } else if (counterData.cellId().counterId().equals(EGRESS_COUNTER_ID)) { |
| 122 | statsBuilder.setPacketsSent(counterData.packets()); |
| 123 | statsBuilder.setBytesSent(counterData.bytes()); |
| 124 | } else { |
| 125 | log.warn("Unrecognized counter ID {}, skipping", counterData); |
| 126 | } |
| 127 | }); |
| 128 | |
| 129 | return portStatBuilders |
| 130 | .values() |
| 131 | .stream() |
| 132 | .map(DefaultPortStatistics.Builder::build) |
| 133 | .collect(Collectors.toList()); |
| 134 | } |
| 135 | } |