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