blob: 425f8fe9d6039b4e2293e39fb7a7a53106b6ba5b [file] [log] [blame]
Carmelo Casconeb045ddc2017-09-01 01:26:35 +02001/*
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 Casconeca94bcf2017-10-27 14:16:59 -070017package org.onosproject.pipelines.basic;
Carmelo Casconeb045ddc2017-09-01 01:26:35 +020018
19import com.google.common.collect.Maps;
20import com.google.common.collect.Sets;
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070021import org.onosproject.net.DeviceId;
Carmelo Casconeb045ddc2017-09-01 01:26:35 +020022import org.onosproject.net.device.DefaultPortStatistics;
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070023import org.onosproject.net.device.DeviceService;
Carmelo Casconeb045ddc2017-09-01 01:26:35 +020024import org.onosproject.net.device.PortStatistics;
25import org.onosproject.net.device.PortStatisticsDiscovery;
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070026import org.onosproject.net.driver.AbstractHandlerBehaviour;
27import org.onosproject.net.pi.model.PiPipeconf;
Carmelo Casconeb045ddc2017-09-01 01:26:35 +020028import org.onosproject.net.pi.runtime.PiCounterCellData;
29import org.onosproject.net.pi.runtime.PiCounterCellId;
30import org.onosproject.net.pi.runtime.PiCounterId;
Carmelo Cascone7f75be42017-09-07 14:37:02 +020031import org.onosproject.net.pi.runtime.PiIndirectCounterCellId;
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070032import org.onosproject.net.pi.runtime.PiPipeconfService;
33import org.onosproject.p4runtime.api.P4RuntimeClient;
34import org.onosproject.p4runtime.api.P4RuntimeController;
35import org.slf4j.Logger;
36import org.slf4j.LoggerFactory;
Carmelo Casconeb045ddc2017-09-01 01:26:35 +020037
38import java.util.Collection;
39import java.util.Collections;
40import java.util.Map;
41import java.util.Set;
42import java.util.concurrent.ExecutionException;
43import java.util.stream.Collectors;
44
Carmelo Cascone7f75be42017-09-07 14:37:02 +020045import static org.onosproject.net.pi.runtime.PiCounterType.INDIRECT;
46
Carmelo Casconeb045ddc2017-09-01 01:26:35 +020047/**
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070048 * Implementation of the PortStatisticsBehaviour for basic.p4.
Carmelo Casconeb045ddc2017-09-01 01:26:35 +020049 */
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070050public class PortStatisticsDiscoveryImpl extends AbstractHandlerBehaviour implements PortStatisticsDiscovery {
Carmelo Casconeb045ddc2017-09-01 01:26:35 +020051
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070052 protected final Logger log = LoggerFactory.getLogger(getClass());
53
54 private static final String SCOPE = "port_counters_control";
55 private static final PiCounterId INGRESS_COUNTER_ID = PiCounterId.of("port_counters_ingress",
56 "ingress_port_counter", INDIRECT);
57 private static final PiCounterId EGRESS_COUNTER_ID = PiCounterId.of("port_counters_egress",
58 "egress_port_counter", INDIRECT);
Carmelo Casconecb0a49c2017-10-03 14:32:23 +020059
60 /**
Carmelo Cascone2b057522017-10-04 14:35:29 +020061 * Returns the ID of the ingress port counter.
Carmelo Casconecb0a49c2017-10-03 14:32:23 +020062 *
Carmelo Cascone2b057522017-10-04 14:35:29 +020063 * @return counter ID
Carmelo Casconecb0a49c2017-10-03 14:32:23 +020064 */
Carmelo Cascone2b057522017-10-04 14:35:29 +020065 public PiCounterId ingressCounterId() {
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070066 return INGRESS_COUNTER_ID;
Carmelo Cascone2b057522017-10-04 14:35:29 +020067 }
68
69 /**
70 * Returns the ID of the egress port counter.
71 *
72 * @return counter ID
73 */
74 public PiCounterId egressCounterId() {
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070075 return EGRESS_COUNTER_ID;
Carmelo Casconecb0a49c2017-10-03 14:32:23 +020076 }
Carmelo Casconeb045ddc2017-09-01 01:26:35 +020077
78 @Override
79 public Collection<PortStatistics> discoverPortStatistics() {
80
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070081 DeviceService deviceService = this.handler().get(DeviceService.class);
82 DeviceId deviceId = this.data().deviceId();
83
84 PiPipeconfService piPipeconfService = handler().get(PiPipeconfService.class);
85 if (!piPipeconfService.ofDevice(deviceId).isPresent() ||
86 !piPipeconfService.getPipeconf(piPipeconfService.ofDevice(deviceId).get()).isPresent()) {
87 log.warn("Unable to get the pipeconf of {}, aborting operation", deviceId);
Carmelo Casconeb045ddc2017-09-01 01:26:35 +020088 return Collections.emptyList();
89 }
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070090 PiPipeconf pipeconf = piPipeconfService.getPipeconf(piPipeconfService.ofDevice(deviceId).get()).get();
91
92 P4RuntimeController controller = handler().get(P4RuntimeController.class);
93 if (!controller.hasClient(deviceId)) {
94 log.warn("Unable to find client for {}, aborting operation", deviceId);
95 return Collections.emptyList();
96 }
97 P4RuntimeClient client = controller.getClient(deviceId);
Carmelo Casconeb045ddc2017-09-01 01:26:35 +020098
99 Map<Long, DefaultPortStatistics.Builder> portStatBuilders = Maps.newHashMap();
Carmelo Casconeb045ddc2017-09-01 01:26:35 +0200100 deviceService.getPorts(deviceId)
101 .forEach(p -> portStatBuilders.put(p.number().toLong(),
102 DefaultPortStatistics.builder()
Carmelo Cascone7f75be42017-09-07 14:37:02 +0200103 .setPort(p.number())
Carmelo Casconeb045ddc2017-09-01 01:26:35 +0200104 .setDeviceId(deviceId)));
105
106 Set<PiCounterCellId> counterCellIds = Sets.newHashSet();
107 portStatBuilders.keySet().forEach(p -> {
108 // Counter cell/index = port number.
Carmelo Cascone2b057522017-10-04 14:35:29 +0200109 counterCellIds.add(PiIndirectCounterCellId.of(ingressCounterId(), p));
110 counterCellIds.add(PiIndirectCounterCellId.of(egressCounterId(), p));
Carmelo Casconeb045ddc2017-09-01 01:26:35 +0200111 });
112
113 Collection<PiCounterCellData> counterEntryResponse;
114 try {
115 counterEntryResponse = client.readCounterCells(counterCellIds, pipeconf).get();
116 } catch (InterruptedException | ExecutionException e) {
117 log.warn("Exception while reading port counters from {}: {}", deviceId, e.toString());
118 log.debug("", e);
119 return Collections.emptyList();
120 }
121
Carmelo Cascone7f75be42017-09-07 14:37:02 +0200122 counterEntryResponse.forEach(counterData -> {
123 if (counterData.cellId().type() != INDIRECT) {
124 log.warn("Invalid counter data type {}, skipping", counterData.cellId().type());
Carmelo Casconeb045ddc2017-09-01 01:26:35 +0200125 return;
126 }
Carmelo Cascone7f75be42017-09-07 14:37:02 +0200127 PiIndirectCounterCellId indCellId = (PiIndirectCounterCellId) counterData.cellId();
128 if (!portStatBuilders.containsKey(indCellId.index())) {
129 log.warn("Unrecognized counter index {}, skipping", counterData);
130 return;
131 }
132 DefaultPortStatistics.Builder statsBuilder = portStatBuilders.get(indCellId.index());
Carmelo Cascone2b057522017-10-04 14:35:29 +0200133 if (counterData.cellId().counterId().equals(ingressCounterId())) {
Carmelo Cascone7f75be42017-09-07 14:37:02 +0200134 statsBuilder.setPacketsReceived(counterData.packets());
135 statsBuilder.setBytesReceived(counterData.bytes());
Carmelo Cascone2b057522017-10-04 14:35:29 +0200136 } else if (counterData.cellId().counterId().equals(egressCounterId())) {
Carmelo Cascone7f75be42017-09-07 14:37:02 +0200137 statsBuilder.setPacketsSent(counterData.packets());
138 statsBuilder.setBytesSent(counterData.bytes());
Carmelo Casconeb045ddc2017-09-01 01:26:35 +0200139 } else {
Carmelo Cascone7f75be42017-09-07 14:37:02 +0200140 log.warn("Unrecognized counter ID {}, skipping", counterData);
Carmelo Casconeb045ddc2017-09-01 01:26:35 +0200141 }
142 });
143
144 return portStatBuilders
145 .values()
146 .stream()
147 .map(DefaultPortStatistics.Builder::build)
148 .collect(Collectors.toList());
149 }
150}