blob: 4ddaf40e4789453d023f2fcab58ed9235d618c42 [file] [log] [blame]
Boyoung Jeong9e8faec2018-06-17 21:19:23 +09001/*
2 * Copyright 2018-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 */
16package org.onosproject.openstacktelemetry.impl;
17
Boyoung Jeong1cca5e82018-08-01 21:00:08 +090018import com.google.common.collect.Maps;
Jian Li0bbbb1c2018-06-22 22:01:17 +090019import com.google.common.collect.Sets;
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090020import org.onlab.packet.IpAddress;
Jian Li753280e2018-07-03 02:24:34 +090021import org.onlab.packet.IpPrefix;
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090022import org.onlab.packet.MacAddress;
23import org.onlab.packet.VlanId;
Jian Lia4947682018-07-07 14:53:32 +090024import org.onlab.util.SharedScheduledExecutors;
Jian Lie6110b72018-07-06 19:06:36 +090025import org.onosproject.cfg.ComponentConfigService;
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090026import org.onosproject.core.ApplicationId;
27import org.onosproject.core.CoreService;
Jian Li85573f42018-06-27 22:29:14 +090028import org.onosproject.mastership.MastershipService;
Jian Lif8b8c7f2018-08-27 18:49:04 +090029import org.onosproject.net.ConnectPoint;
30import org.onosproject.net.Device;
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090031import org.onosproject.net.DeviceId;
32import org.onosproject.net.Host;
Jian Lia4947682018-07-07 14:53:32 +090033import org.onosproject.net.PortNumber;
34import org.onosproject.net.device.DeviceService;
35import org.onosproject.net.device.PortStatistics;
Jian Lif8b8c7f2018-08-27 18:49:04 +090036import org.onosproject.net.driver.Driver;
37import org.onosproject.net.driver.DriverService;
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090038import org.onosproject.net.flow.DefaultFlowRule;
39import org.onosproject.net.flow.DefaultTrafficSelector;
40import org.onosproject.net.flow.DefaultTrafficTreatment;
41import org.onosproject.net.flow.FlowEntry;
42import org.onosproject.net.flow.FlowRule;
43import org.onosproject.net.flow.FlowRuleOperations;
44import org.onosproject.net.flow.FlowRuleOperationsContext;
45import org.onosproject.net.flow.FlowRuleService;
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090046import org.onosproject.net.flow.TrafficSelector;
47import org.onosproject.net.flow.TrafficTreatment;
Jian Lif8b8c7f2018-08-27 18:49:04 +090048import org.onosproject.net.flow.criteria.Criterion;
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090049import org.onosproject.net.flow.criteria.IPCriterion;
50import org.onosproject.net.flow.criteria.IPProtocolCriterion;
Jian Lif8b8c7f2018-08-27 18:49:04 +090051import org.onosproject.net.flow.criteria.PortCriterion;
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090052import org.onosproject.net.flow.criteria.TcpPortCriterion;
53import org.onosproject.net.flow.criteria.UdpPortCriterion;
54import org.onosproject.net.host.HostService;
Jian Lia4947682018-07-07 14:53:32 +090055import org.onosproject.openstacknetworking.api.InstancePort;
56import org.onosproject.openstacknetworking.api.InstancePortService;
Jian Li753280e2018-07-03 02:24:34 +090057import org.onosproject.openstacknetworking.api.OpenstackNetworkService;
Jian Lia4947682018-07-07 14:53:32 +090058import org.onosproject.openstacknode.api.OpenstackNode;
59import org.onosproject.openstacknode.api.OpenstackNodeService;
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090060import org.onosproject.openstacktelemetry.api.FlowInfo;
Jian Li0bbbb1c2018-06-22 22:01:17 +090061import org.onosproject.openstacktelemetry.api.OpenstackTelemetryService;
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090062import org.onosproject.openstacktelemetry.api.StatsFlowRule;
63import org.onosproject.openstacktelemetry.api.StatsFlowRuleAdminService;
64import org.onosproject.openstacktelemetry.api.StatsInfo;
Jian Li753280e2018-07-03 02:24:34 +090065import org.osgi.service.component.ComponentContext;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070066import org.osgi.service.component.annotations.Activate;
67import org.osgi.service.component.annotations.Component;
68import org.osgi.service.component.annotations.Deactivate;
69import org.osgi.service.component.annotations.Modified;
70import org.osgi.service.component.annotations.Reference;
71import org.osgi.service.component.annotations.ReferenceCardinality;
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090072import org.slf4j.Logger;
73import org.slf4j.LoggerFactory;
74
Jian Lif8b8c7f2018-08-27 18:49:04 +090075import java.util.ArrayList;
Jian Li753280e2018-07-03 02:24:34 +090076import java.util.Dictionary;
Boyoung Jeong1cca5e82018-08-01 21:00:08 +090077import java.util.LinkedList;
Jian Lia4947682018-07-07 14:53:32 +090078import java.util.List;
Boyoung Jeong1cca5e82018-08-01 21:00:08 +090079import java.util.Map;
Jian Li85573f42018-06-27 22:29:14 +090080import java.util.Optional;
Boyoung Jeong1cca5e82018-08-01 21:00:08 +090081import java.util.Queue;
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090082import java.util.Set;
Jian Lia4947682018-07-07 14:53:32 +090083import java.util.concurrent.ScheduledFuture;
84import java.util.concurrent.TimeUnit;
85import java.util.stream.Collectors;
Boyoung Jeong9e8faec2018-06-17 21:19:23 +090086
Jian Li0bbbb1c2018-06-22 22:01:17 +090087import static org.onlab.packet.Ethernet.TYPE_IPV4;
88import static org.onlab.packet.IPv4.PROTOCOL_TCP;
89import static org.onlab.packet.IPv4.PROTOCOL_UDP;
Jian Lif8b8c7f2018-08-27 18:49:04 +090090import static org.onosproject.net.Device.Type.SWITCH;
Jian Li0bbbb1c2018-06-22 22:01:17 +090091import static org.onosproject.net.flow.criteria.Criterion.Type.IPV4_DST;
92import static org.onosproject.net.flow.criteria.Criterion.Type.IPV4_SRC;
93import static org.onosproject.net.flow.criteria.Criterion.Type.IP_PROTO;
94import static org.onosproject.net.flow.criteria.Criterion.Type.TCP_DST;
95import static org.onosproject.net.flow.criteria.Criterion.Type.TCP_SRC;
96import static org.onosproject.net.flow.criteria.Criterion.Type.UDP_DST;
97import static org.onosproject.net.flow.criteria.Criterion.Type.UDP_SRC;
Jian Li753280e2018-07-03 02:24:34 +090098import static org.onosproject.openstacknetworking.api.Constants.STAT_FLAT_OUTBOUND_TABLE;
Jian Li0bbbb1c2018-06-22 22:01:17 +090099import static org.onosproject.openstacknetworking.api.Constants.STAT_INBOUND_TABLE;
100import static org.onosproject.openstacknetworking.api.Constants.STAT_OUTBOUND_TABLE;
Jian Li753280e2018-07-03 02:24:34 +0900101import static org.onosproject.openstacknetworking.api.Constants.VTAP_FLAT_OUTBOUND_TABLE;
Jian Li87ded822018-07-02 18:31:22 +0900102import static org.onosproject.openstacknetworking.api.Constants.VTAP_INBOUND_TABLE;
103import static org.onosproject.openstacknetworking.api.Constants.VTAP_OUTBOUND_TABLE;
Jian Lia4947682018-07-07 14:53:32 +0900104import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.COMPUTE;
Jian Lif8b8c7f2018-08-27 18:49:04 +0900105import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.CONTROLLER;
Boyoung Jeong1cca5e82018-08-01 21:00:08 +0900106import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_DATA_POINT_SIZE;
Jian Lie6110b72018-07-06 19:06:36 +0900107import static org.onosproject.openstacktelemetry.api.Constants.FLAT;
Boyoung Jeong9e8faec2018-06-17 21:19:23 +0900108import static org.onosproject.openstacktelemetry.api.Constants.OPENSTACK_TELEMETRY_APP_ID;
Jian Lie6110b72018-07-06 19:06:36 +0900109import static org.onosproject.openstacktelemetry.api.Constants.VLAN;
110import static org.onosproject.openstacktelemetry.api.Constants.VXLAN;
Ray Milkey8e406512018-10-24 15:56:50 -0700111import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_EGRESS_STATS;
112import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_EGRESS_STATS_DEFAULT;
113import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_MONITOR_OVERLAY;
114import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_MONITOR_OVERLAY_DEFAULT;
115import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_MONITOR_UNDERLAY;
116import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_MONITOR_UNDERLAY_DEFAULT;
117import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_PORT_STATS;
118import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_PORT_STATS_DEFAULT;
119import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_REVERSE_PATH_STATS;
120import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_REVERSE_PATH_STATS_DEFAULT;
Jian Li753280e2018-07-03 02:24:34 +0900121import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.getBooleanProperty;
Boyoung Jeong9e8faec2018-06-17 21:19:23 +0900122
Boyoung Jeong9e8faec2018-06-17 21:19:23 +0900123/**
124 * Flow rule manager for network statistics of a VM.
125 */
Ray Milkey8e406512018-10-24 15:56:50 -0700126@Component(
127 immediate = true,
128 service = StatsFlowRuleAdminService.class,
129 property = {
130 PROP_REVERSE_PATH_STATS + ":Boolean=" + PROP_REVERSE_PATH_STATS_DEFAULT,
131 PROP_EGRESS_STATS + ":Boolean=" + PROP_EGRESS_STATS_DEFAULT,
132 PROP_PORT_STATS + ":Boolean=" + PROP_PORT_STATS_DEFAULT,
133 PROP_MONITOR_OVERLAY + ":Boolean=" + PROP_MONITOR_OVERLAY_DEFAULT,
134 PROP_MONITOR_UNDERLAY + ":Boolean=" + PROP_MONITOR_UNDERLAY_DEFAULT
135 }
136)
Boyoung Jeong9e8faec2018-06-17 21:19:23 +0900137public class StatsFlowRuleManager implements StatsFlowRuleAdminService {
138
139 private final Logger log = LoggerFactory.getLogger(getClass());
140
141 private static final byte FLOW_TYPE_SONA = 1; // VLAN
142
Ray Milkeybcc53d32018-07-02 10:22:57 -0700143 private static final long MILLISECONDS = 1000L;
Jian Lia4947682018-07-07 14:53:32 +0900144 private static final long INITIAL_DELAY = 5L;
Ray Milkeybcc53d32018-07-02 10:22:57 -0700145 private static final long REFRESH_INTERVAL = 5L;
Jian Lia4947682018-07-07 14:53:32 +0900146 private static final TimeUnit TIME_UNIT_SECOND = TimeUnit.SECONDS;
Boyoung Jeong9e8faec2018-06-17 21:19:23 +0900147
Jian Lif8b8c7f2018-08-27 18:49:04 +0900148 private static final String OVS_DRIVER_NAME = "ovs";
149
Jian Lia4947682018-07-07 14:53:32 +0900150 private static final String ARBITRARY_IP = "0.0.0.0/32";
boyoung27b444122018-09-01 17:28:13 +0900151 private static final int ARBITRARY_PROTOCOL = 0x0;
Jian Lia4947682018-07-07 14:53:32 +0900152 private static final int ARBITRARY_LENGTH = 32;
153 private static final String ARBITRARY_MAC = "00:00:00:00:00:00";
Jian Lif8b8c7f2018-08-27 18:49:04 +0900154 private static final IpAddress NO_HOST_IP = IpAddress.valueOf("255.255.255.255");
Boyoung Jeong1cca5e82018-08-01 21:00:08 +0900155 private static final MacAddress NO_HOST_MAC = MacAddress.valueOf(ARBITRARY_MAC);
Jian Lia4947682018-07-07 14:53:32 +0900156 private static final int ARBITRARY_IN_INTF = 0;
157 private static final int ARBITRARY_OUT_INTF = 0;
158
159 private static final boolean RECOVER_FROM_FAILURE = true;
160
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700161 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Boyoung Jeong9e8faec2018-06-17 21:19:23 +0900162 protected CoreService coreService;
163
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700164 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Boyoung Jeong9e8faec2018-06-17 21:19:23 +0900165 protected FlowRuleService flowRuleService;
166
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700167 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Boyoung Jeong9e8faec2018-06-17 21:19:23 +0900168 protected HostService hostService;
169
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700170 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jian Lia4947682018-07-07 14:53:32 +0900171 protected DeviceService deviceService;
172
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700173 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jian Lif8b8c7f2018-08-27 18:49:04 +0900174 protected DriverService driverService;
175
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700176 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jian Lie6110b72018-07-06 19:06:36 +0900177 protected ComponentConfigService componentConfigService;
Jian Li0bbbb1c2018-06-22 22:01:17 +0900178
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700179 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jian Li85573f42018-06-27 22:29:14 +0900180 protected MastershipService mastershipService;
181
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700182 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jian Li753280e2018-07-03 02:24:34 +0900183 protected OpenstackNetworkService osNetworkService;
184
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700185 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jian Lia4947682018-07-07 14:53:32 +0900186 protected InstancePortService instPortService;
187
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700188 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jian Lia4947682018-07-07 14:53:32 +0900189 protected OpenstackNodeService osNodeService;
190
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700191 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jian Li0bbbb1c2018-06-22 22:01:17 +0900192 protected OpenstackTelemetryService telemetryService;
193
Ray Milkey8e406512018-10-24 15:56:50 -0700194 /** A flag which indicates whether to install the rules for collecting the flow-based stats for reversed path. */
195 private boolean reversePathStats = PROP_REVERSE_PATH_STATS_DEFAULT;
Jian Li753280e2018-07-03 02:24:34 +0900196
Ray Milkey8e406512018-10-24 15:56:50 -0700197 /** A flag which indicates whether to install the rules for collecting the flow-based stats for egress port. */
198 private boolean egressStats = PROP_EGRESS_STATS_DEFAULT;
Jian Li753280e2018-07-03 02:24:34 +0900199
Ray Milkey8e406512018-10-24 15:56:50 -0700200 /** A flag which indicates whether to collect port TX & RX stats. */
201 private boolean portStats = PROP_PORT_STATS_DEFAULT;
Jian Lia4947682018-07-07 14:53:32 +0900202
Ray Milkey8e406512018-10-24 15:56:50 -0700203 /** A flag which indicates whether to monitor overlay network port stats. */
204 private boolean monitorOverlay = PROP_MONITOR_OVERLAY_DEFAULT;
Jian Lif8b8c7f2018-08-27 18:49:04 +0900205
Ray Milkey8e406512018-10-24 15:56:50 -0700206 /** A flag which indicates whether to monitor underlay network port stats. */
207 private boolean monitorUnderlay = PROP_MONITOR_UNDERLAY_DEFAULT;
Jian Lif8b8c7f2018-08-27 18:49:04 +0900208
209 private ApplicationId telemetryAppId;
Jian Lia4947682018-07-07 14:53:32 +0900210 private TelemetryCollector collector;
Jian Lia4947682018-07-07 14:53:32 +0900211 private ScheduledFuture result;
Boyoung Jeong9e8faec2018-06-17 21:19:23 +0900212
Jian Li0bbbb1c2018-06-22 22:01:17 +0900213 private final Set<FlowInfo> gFlowInfoSet = Sets.newHashSet();
Boyoung Jeong1cca5e82018-08-01 21:00:08 +0900214 private final Map<String, Queue<FlowInfo>> flowInfoMap = Maps.newConcurrentMap();
Boyoung Jeong9e8faec2018-06-17 21:19:23 +0900215
216 private static final int SOURCE_ID = 1;
217 private static final int TARGET_ID = 2;
218 private static final int PRIORITY_BASE = 10000;
219 private static final int METRIC_PRIORITY_SOURCE = SOURCE_ID * PRIORITY_BASE;
220 private static final int METRIC_PRIORITY_TARGET = TARGET_ID * PRIORITY_BASE;
221
Boyoung Jeong9e8faec2018-06-17 21:19:23 +0900222 @Activate
223 protected void activate() {
Jian Lif8b8c7f2018-08-27 18:49:04 +0900224 telemetryAppId = coreService.registerApplication(OPENSTACK_TELEMETRY_APP_ID);
225
Jian Lie6110b72018-07-06 19:06:36 +0900226 componentConfigService.registerProperties(getClass());
Jian Libd295cd2018-07-22 11:53:57 +0900227 start();
Jian Li0bbbb1c2018-06-22 22:01:17 +0900228
229 log.info("Started");
Boyoung Jeong9e8faec2018-06-17 21:19:23 +0900230 }
231
232 @Deactivate
233 protected void deactivate() {
Jian Lie6110b72018-07-06 19:06:36 +0900234 componentConfigService.unregisterProperties(getClass(), false);
Jian Lif8b8c7f2018-08-27 18:49:04 +0900235 flowRuleService.removeFlowRulesById(telemetryAppId);
Jian Libd295cd2018-07-22 11:53:57 +0900236 stop();
Jian Lia4947682018-07-07 14:53:32 +0900237
Jian Li0bbbb1c2018-06-22 22:01:17 +0900238 log.info("Stopped");
Boyoung Jeong9e8faec2018-06-17 21:19:23 +0900239 }
240
Jian Li753280e2018-07-03 02:24:34 +0900241 @Modified
242 protected void modified(ComponentContext context) {
243 readComponentConfiguration(context);
244
245 log.info("Modified");
246 }
247
Boyoung Jeong9e8faec2018-06-17 21:19:23 +0900248 @Override
249 public void start() {
250 log.info("Start publishing thread");
Jian Lia4947682018-07-07 14:53:32 +0900251 collector = new TelemetryCollector();
252
Jian Libd295cd2018-07-22 11:53:57 +0900253 result = SharedScheduledExecutors.getSingleThreadExecutor()
254 .scheduleAtFixedRate(collector, INITIAL_DELAY,
Jian Lia4947682018-07-07 14:53:32 +0900255 REFRESH_INTERVAL, TIME_UNIT_SECOND, RECOVER_FROM_FAILURE);
Boyoung Jeong9e8faec2018-06-17 21:19:23 +0900256 }
257
258 @Override
259 public void stop() {
260 log.info("Stop data publishing thread");
Jian Lia4947682018-07-07 14:53:32 +0900261 result.cancel(true);
262 collector = null;
Boyoung Jeong9e8faec2018-06-17 21:19:23 +0900263 }
264
Jian Li0bbbb1c2018-06-22 22:01:17 +0900265 @Override
boyoung27b444122018-09-01 17:28:13 +0900266 public void setStatFlowL2Rule(String srcIp, String dstIp, Boolean install) {
267 StatsFlowRule statsFlowRule = DefaultStatsFlowRule.builder()
268 .srcIpPrefix(IpPrefix.valueOf(IpAddress.valueOf(srcIp), ARBITRARY_LENGTH))
269 .dstIpPrefix(IpPrefix.valueOf(IpAddress.valueOf(dstIp), ARBITRARY_LENGTH))
270 .ipProtocol((byte) ARBITRARY_PROTOCOL)
271 .build();
272 setStatFlowRule(statsFlowRule, install);
273 }
Boyoung Jeong9e8faec2018-06-17 21:19:23 +0900274
boyoung27b444122018-09-01 17:28:13 +0900275 @Override
276 public void createStatFlowRule(StatsFlowRule statsFlowRule) {
Jian Li0bbbb1c2018-06-22 22:01:17 +0900277 setStatFlowRule(statsFlowRule, true);
Jian Li0bbbb1c2018-06-22 22:01:17 +0900278 }
279
280 @Override
281 public void deleteStatFlowRule(StatsFlowRule statsFlowRule) {
Jian Li0bbbb1c2018-06-22 22:01:17 +0900282 setStatFlowRule(statsFlowRule, false);
Jian Li0bbbb1c2018-06-22 22:01:17 +0900283 }
284
Jian Lif8b8c7f2018-08-27 18:49:04 +0900285
Jian Lia4947682018-07-07 14:53:32 +0900286 @Override
Jian Lif8b8c7f2018-08-27 18:49:04 +0900287 public Map<String, Queue<FlowInfo>> getFlowInfoMap() {
288 return flowInfoMap;
289 }
290
291
292 @Override
293 public Set<FlowInfo> getUnderlayFlowInfos() {
294
295 Set<FlowInfo> flowInfos = Sets.newConcurrentHashSet();
296
297 for (Device device : getUnderlayDevices()) {
298
299 if (!isEdgeSwitch(device.id())) {
300 continue;
301 }
302
303 for (FlowEntry entry : flowRuleService.getFlowEntries(device.id())) {
304 FlowInfo.Builder fBuilder = new DefaultFlowInfo.DefaultBuilder();
305 TrafficSelector selector = entry.selector();
306 Criterion inPort = selector.getCriterion(Criterion.Type.IN_PORT);
307 Criterion dstIpCriterion = selector.getCriterion(Criterion.Type.IPV4_DST);
308 if (inPort != null && dstIpCriterion != null) {
309 IpAddress srcIp = getIpAddress(device, (PortCriterion) inPort);
310 IpAddress dstIp = ((IPCriterion) dstIpCriterion).ip().address();
311
312 if (srcIp == null) {
313 continue;
314 }
315
316 fBuilder.withFlowType(FLOW_TYPE_SONA)
317 .withSrcIp(IpPrefix.valueOf(srcIp, ARBITRARY_LENGTH))
318 .withDstIp(IpPrefix.valueOf(dstIp, ARBITRARY_LENGTH))
319 .withSrcMac(getMacAddress(srcIp))
320 .withDstMac(getMacAddress(dstIp))
321 .withInputInterfaceId(getInterfaceId(srcIp))
322 .withOutputInterfaceId(getInterfaceId(dstIp))
323 .withDeviceId(entry.deviceId());
324
325 StatsInfo.Builder sBuilder = new DefaultStatsInfo.DefaultBuilder();
326
327 sBuilder.withStartupTime(System.currentTimeMillis())
328 .withFstPktArrTime(System.currentTimeMillis())
329 .withLstPktOffset((int) (REFRESH_INTERVAL * MILLISECONDS))
330 .withCurrAccPkts((int) entry.packets())
331 .withCurrAccBytes(entry.bytes())
332 .withErrorPkts((short) 0)
333 .withDropPkts((short) 0);
334
335 fBuilder.withStatsInfo(sBuilder.build());
336
337 FlowInfo flowInfo = mergeFlowInfo(fBuilder.build(), fBuilder, sBuilder);
338
339 flowInfos.add(flowInfo);
340 }
341 }
342 }
343
344 return flowInfos;
345 }
346
347 @Override
348 public Set<FlowInfo> getOverlayFlowInfos() {
349
Jian Lia4947682018-07-07 14:53:32 +0900350 Set<FlowInfo> flowInfos = Sets.newConcurrentHashSet();
351
352 // obtain all flow rule entries installed by telemetry app
Jian Lif8b8c7f2018-08-27 18:49:04 +0900353 for (FlowEntry entry : flowRuleService.getFlowEntriesById(telemetryAppId)) {
Jian Lia4947682018-07-07 14:53:32 +0900354 FlowInfo.Builder fBuilder = new DefaultFlowInfo.DefaultBuilder();
355 TrafficSelector selector = entry.selector();
Jian Lia4947682018-07-07 14:53:32 +0900356 IPCriterion srcIp = (IPCriterion) selector.getCriterion(IPV4_SRC);
357 IPCriterion dstIp = (IPCriterion) selector.getCriterion(IPV4_DST);
358 IPProtocolCriterion ipProtocol =
359 (IPProtocolCriterion) selector.getCriterion(IP_PROTO);
360
Jian Lia4947682018-07-07 14:53:32 +0900361 fBuilder.withFlowType(FLOW_TYPE_SONA)
362 .withSrcIp(srcIp.ip())
363 .withDstIp(dstIp.ip());
364
365 if (ipProtocol != null) {
366 fBuilder.withProtocol((byte) ipProtocol.protocol());
367
368 if (ipProtocol.protocol() == PROTOCOL_TCP) {
369 TcpPortCriterion tcpSrc =
370 (TcpPortCriterion) selector.getCriterion(TCP_SRC);
371 TcpPortCriterion tcpDst =
372 (TcpPortCriterion) selector.getCriterion(TCP_DST);
Jian Lia4947682018-07-07 14:53:32 +0900373 fBuilder.withSrcPort(tcpSrc.tcpPort());
374 fBuilder.withDstPort(tcpDst.tcpPort());
Jian Lia4947682018-07-07 14:53:32 +0900375 } else if (ipProtocol.protocol() == PROTOCOL_UDP) {
Jian Lia4947682018-07-07 14:53:32 +0900376 UdpPortCriterion udpSrc =
377 (UdpPortCriterion) selector.getCriterion(UDP_SRC);
378 UdpPortCriterion udpDst =
379 (UdpPortCriterion) selector.getCriterion(UDP_DST);
Jian Lia4947682018-07-07 14:53:32 +0900380 fBuilder.withSrcPort(udpSrc.udpPort());
381 fBuilder.withDstPort(udpDst.udpPort());
382 } else {
383 log.debug("Other protocol: {}", ipProtocol.protocol());
384 }
385 }
386
387 fBuilder.withSrcMac(getMacAddress(srcIp.ip().address()))
388 .withDstMac(getMacAddress(dstIp.ip().address()))
389 .withInputInterfaceId(getInterfaceId(srcIp.ip().address()))
390 .withOutputInterfaceId(getInterfaceId(dstIp.ip().address()))
391 .withVlanId(getVlanId(srcIp.ip().address()))
392 .withDeviceId(entry.deviceId());
393
394 StatsInfo.Builder sBuilder = new DefaultStatsInfo.DefaultBuilder();
395
Jian Lia4947682018-07-07 14:53:32 +0900396 sBuilder.withStartupTime(System.currentTimeMillis())
397 .withFstPktArrTime(System.currentTimeMillis())
398 .withLstPktOffset((int) (REFRESH_INTERVAL * MILLISECONDS))
399 .withCurrAccPkts((int) entry.packets())
400 .withCurrAccBytes(entry.bytes())
401 .withErrorPkts((short) 0)
402 .withDropPkts((short) 0);
403
404 fBuilder.withStatsInfo(sBuilder.build());
405
406 FlowInfo flowInfo = mergeFlowInfo(fBuilder.build(), fBuilder, sBuilder);
407
408 flowInfos.add(flowInfo);
409
410 log.debug("FlowInfo: \n{}", flowInfo.toString());
411 }
412
413 return flowInfos;
414 }
415
416 /**
Jian Lif8b8c7f2018-08-27 18:49:04 +0900417 * Gets a set of flow infos by referring to overlay destination VM port.
Jian Lia4947682018-07-07 14:53:32 +0900418 *
419 * @return flow infos
420 */
Jian Lif8b8c7f2018-08-27 18:49:04 +0900421 private Set<FlowInfo> getOverlayDstPortBasedFlowInfos() {
Jian Lia4947682018-07-07 14:53:32 +0900422 Set<FlowInfo> flowInfos = Sets.newConcurrentHashSet();
423 Set<PortNumber> instPortNums = instPortService.instancePorts()
424 .stream()
425 .map(InstancePort::portNumber)
426 .collect(Collectors.toSet());
427 Set<DeviceId> deviceIds = osNodeService.completeNodes(COMPUTE)
428 .stream()
429 .map(OpenstackNode::intgBridge)
430 .collect(Collectors.toSet());
431
432 deviceIds.forEach(d -> {
433 List<PortStatistics> stats =
434 deviceService.getPortStatistics(d)
435 .stream()
436 .filter(s -> instPortNums.contains(s.portNumber()))
437 .collect(Collectors.toList());
438
439 stats.forEach(s -> {
440 InstancePort instPort = getInstancePort(d, s.portNumber());
Jian Lif8b8c7f2018-08-27 18:49:04 +0900441 flowInfos.add(buildTxFlowInfoFromInstancePort(instPort, s));
442 flowInfos.add(buildRxFlowInfoFromInstancePort(instPort, s));
Jian Lia4947682018-07-07 14:53:32 +0900443 });
444 });
445
446 return flowInfos;
447 }
448
449 /**
Jian Lif8b8c7f2018-08-27 18:49:04 +0900450 * Gets a set of flow infos by referring to underlay destination port.
451 *
452 * @return flow infos
453 */
454 private Set<FlowInfo> getUnderlayDstPortBasedFlowInfos() {
455 Set<FlowInfo> flowInfos = Sets.newConcurrentHashSet();
456
457 for (Device d : getUnderlayDevices()) {
458 List<PortStatistics> stats =
459 new ArrayList<>(deviceService.getPortStatistics(d.id()));
460 stats.forEach(s -> {
461 Host host = hostService.getConnectedHosts(new ConnectPoint(d.id(), s.portNumber()))
462 .stream().findFirst().orElse(null);
463 if (host != null) {
464 flowInfos.add(buildTxFlowInfoFromHost(host, s));
465 flowInfos.add(buildRxFlowInfoFromHost(host, s));
466 }
467 });
468 }
469
470 return flowInfos;
471 }
472
473 /**
474 * Obtains a set of device instances which construct underlay network.
475 *
476 * @return a set of device instances
477 */
478 private Set<Device> getUnderlayDevices() {
479
480 Set<Device> underlayDevices = Sets.newConcurrentHashSet();
481
482 Set<DeviceId> overlayDeviceIds = osNodeService.completeNodes()
483 .stream()
484 .filter(n -> n.type() != CONTROLLER)
485 .map(OpenstackNode::intgBridge)
486 .collect(Collectors.toSet());
487
488 for (Device d : deviceService.getAvailableDevices(SWITCH)) {
489 if (overlayDeviceIds.contains(d.id())) {
490 continue;
491 }
492
493 underlayDevices.add(d);
494 }
495
496 return underlayDevices;
497 }
498
499 /**
500 * Checks whether the given drivers contains OVS driver.
501 *
502 * @param drivers a set of drivers
503 * @return true if the given drivers contain any OVS driver, false otherwise
504 */
505 private boolean hasOvsDriver(List<Driver> drivers) {
506
507 for (Driver driver : drivers) {
508 if (OVS_DRIVER_NAME.equals(driver.name())) {
509 return true;
510 }
511 }
512
513 return false;
514 }
515
516 /**
517 * Obtains the flow info generated by TX port from instance port.
Jian Lia4947682018-07-07 14:53:32 +0900518 *
519 * @param instPort instance port
520 * @param stat port statistics
521 * @return flow info
522 */
Jian Lif8b8c7f2018-08-27 18:49:04 +0900523 private FlowInfo buildTxFlowInfoFromInstancePort(InstancePort instPort,
524 PortStatistics stat) {
525 return buildTxFlowInfo(instPort.ipAddress(), instPort.macAddress(),
526 instPort.deviceId(), stat);
527 }
528
529 /**
530 * Obtains the flow info generated from RX port from instance port.
531 *
532 * @param instPort instance port
533 * @param stat port statistics
534 * @return flow info
535 */
536 private FlowInfo buildRxFlowInfoFromInstancePort(InstancePort instPort,
537 PortStatistics stat) {
538 return buildRxFlowInfo(instPort.ipAddress(), instPort.macAddress(),
539 instPort.deviceId(), stat);
540 }
541
542 /**
543 * Obtains the flow info generated by TX port from host.
544 *
545 * @param host host
546 * @param stat port statistics
547 * @return flow info
548 */
549 private FlowInfo buildTxFlowInfoFromHost(Host host, PortStatistics stat) {
550 IpAddress ip = host.ipAddresses().stream().findFirst().orElse(null);
551
552 if (ip != null) {
553 return buildTxFlowInfo(ip, host.mac(), host.location().deviceId(), stat);
554 }
555 return null;
556 }
557
558 /**
559 * Obtains the flow info generated by RX @param host host.
560 *
561 * @param host host
562 * @param stat port statistics
563 * @return flow info
564 */
565 private FlowInfo buildRxFlowInfoFromHost(Host host, PortStatistics stat) {
566 IpAddress ip = host.ipAddresses().stream().findFirst().orElse(null);
567
568 if (ip != null) {
569 return buildRxFlowInfo(ip, host.mac(), host.location().deviceId(), stat);
570 }
571 return null;
572 }
573
574 /**
575 * Obtains the flow info generated from TX port.
576 *
577 * @param ipAddress IP address
578 * @param macAddress MAC address
579 * @param deviceId device identifier
580 * @param stat port statistics
581 * @return flow info
582 */
583 private FlowInfo buildTxFlowInfo(IpAddress ipAddress,
584 MacAddress macAddress,
585 DeviceId deviceId,
586 PortStatistics stat) {
Jian Lia4947682018-07-07 14:53:32 +0900587 FlowInfo.Builder fBuilder = new DefaultFlowInfo.DefaultBuilder();
588
589 fBuilder.withFlowType(FLOW_TYPE_SONA)
Jian Lif8b8c7f2018-08-27 18:49:04 +0900590 .withSrcIp(IpPrefix.valueOf(ipAddress, ARBITRARY_LENGTH))
Jian Lia4947682018-07-07 14:53:32 +0900591 .withDstIp(IpPrefix.valueOf(ARBITRARY_IP))
Jian Lif8b8c7f2018-08-27 18:49:04 +0900592 .withSrcMac(macAddress)
Boyoung Jeong1cca5e82018-08-01 21:00:08 +0900593 .withDstMac(NO_HOST_MAC)
Jian Lif8b8c7f2018-08-27 18:49:04 +0900594 .withDeviceId(deviceId)
Jian Lia4947682018-07-07 14:53:32 +0900595 .withInputInterfaceId(ARBITRARY_IN_INTF)
596 .withOutputInterfaceId(ARBITRARY_OUT_INTF)
597 .withVlanId(VlanId.vlanId());
598
599 StatsInfo.Builder sBuilder = new DefaultStatsInfo.DefaultBuilder();
600 sBuilder.withStartupTime(System.currentTimeMillis())
601 .withFstPktArrTime(System.currentTimeMillis())
602 .withLstPktOffset((int) (REFRESH_INTERVAL * MILLISECONDS))
603 .withCurrAccPkts((int) stat.packetsSent())
604 .withCurrAccBytes(stat.bytesSent())
605 .withErrorPkts((short) stat.packetsTxErrors())
606 .withDropPkts((short) stat.packetsTxDropped());
607
608 fBuilder.withStatsInfo(sBuilder.build());
609
610 return mergeFlowInfo(fBuilder.build(), fBuilder, sBuilder);
611 }
612
613 /**
Jian Lif8b8c7f2018-08-27 18:49:04 +0900614 * Obtains the flow info generated from RX port.
Jian Lia4947682018-07-07 14:53:32 +0900615 *
Jian Lif8b8c7f2018-08-27 18:49:04 +0900616 * @param ipAddress IP address
617 * @param macAddress MAC address
618 * @param deviceId Device identifier
Jian Lia4947682018-07-07 14:53:32 +0900619 * @param stat port statistics
620 * @return flow info
621 */
Jian Lif8b8c7f2018-08-27 18:49:04 +0900622 private FlowInfo buildRxFlowInfo(IpAddress ipAddress,
623 MacAddress macAddress,
624 DeviceId deviceId,
625 PortStatistics stat) {
Jian Lia4947682018-07-07 14:53:32 +0900626 FlowInfo.Builder fBuilder = new DefaultFlowInfo.DefaultBuilder();
627
628 fBuilder.withFlowType(FLOW_TYPE_SONA)
629 .withSrcIp(IpPrefix.valueOf(ARBITRARY_IP))
Jian Lif8b8c7f2018-08-27 18:49:04 +0900630 .withDstIp(IpPrefix.valueOf(ipAddress, ARBITRARY_LENGTH))
Boyoung Jeong1cca5e82018-08-01 21:00:08 +0900631 .withSrcMac(NO_HOST_MAC)
Jian Lif8b8c7f2018-08-27 18:49:04 +0900632 .withDstMac(macAddress)
633 .withDeviceId(deviceId)
Jian Lia4947682018-07-07 14:53:32 +0900634 .withInputInterfaceId(ARBITRARY_IN_INTF)
635 .withOutputInterfaceId(ARBITRARY_OUT_INTF)
636 .withVlanId(VlanId.vlanId());
637
638 StatsInfo.Builder sBuilder = new DefaultStatsInfo.DefaultBuilder();
639 sBuilder.withStartupTime(System.currentTimeMillis())
640 .withFstPktArrTime(System.currentTimeMillis())
641 .withLstPktOffset((int) (REFRESH_INTERVAL * MILLISECONDS))
642 .withCurrAccPkts((int) stat.packetsReceived())
643 .withCurrAccBytes(stat.bytesReceived())
644 .withErrorPkts((short) stat.packetsRxErrors())
645 .withDropPkts((short) stat.packetsRxDropped());
646
647 fBuilder.withStatsInfo(sBuilder.build());
648
649 return mergeFlowInfo(fBuilder.build(), fBuilder, sBuilder);
650 }
651
652 /**
653 * Obtains instance port which associated with the given device identifier
654 * and port number.
655 *
656 * @param deviceId device identifier
657 * @param portNumber port number
658 * @return instance port
659 */
660 private InstancePort getInstancePort(DeviceId deviceId, PortNumber portNumber) {
661 return instPortService.instancePorts().stream()
662 .filter(p -> p.deviceId().equals(deviceId))
663 .filter(p -> p.portNumber().equals(portNumber))
664 .findFirst().orElse(null);
665 }
666
Jian Lif8b8c7f2018-08-27 18:49:04 +0900667 /**
668 * Installs a flow rule where the source table is fromTable, while destination
669 * table is toTable.
670 *
671 * @param deviceId device identifier
672 * @param fromTable source table
673 * @param toTable destination table
674 * @param statsFlowRule stats flow rule
675 * @param rulePriority rule priority
676 * @param install installation flag
677 */
Jian Li0bbbb1c2018-06-22 22:01:17 +0900678 private void connectTables(DeviceId deviceId, int fromTable, int toTable,
679 StatsFlowRule statsFlowRule, int rulePriority,
680 boolean install) {
681
Jian Li0bbbb1c2018-06-22 22:01:17 +0900682 int srcPrefixLength = statsFlowRule.srcIpPrefix().prefixLength();
683 int dstPrefixLength = statsFlowRule.dstIpPrefix().prefixLength();
684 int prefixLength = rulePriority + srcPrefixLength + dstPrefixLength;
685 byte protocol = statsFlowRule.ipProtocol();
686
687 TrafficSelector.Builder selectorBuilder =
Jian Li753280e2018-07-03 02:24:34 +0900688 DefaultTrafficSelector.builder()
689 .matchEthType(TYPE_IPV4)
690 .matchIPSrc(statsFlowRule.srcIpPrefix())
691 .matchIPDst(statsFlowRule.dstIpPrefix());
Jian Li0bbbb1c2018-06-22 22:01:17 +0900692
693 if (protocol == PROTOCOL_TCP) {
694 selectorBuilder = selectorBuilder
Jian Li753280e2018-07-03 02:24:34 +0900695 .matchIPProtocol(statsFlowRule.ipProtocol())
696 .matchTcpSrc(statsFlowRule.srcTpPort())
697 .matchTcpDst(statsFlowRule.dstTpPort());
Jian Li0bbbb1c2018-06-22 22:01:17 +0900698
699 } else if (protocol == PROTOCOL_UDP) {
700 selectorBuilder = selectorBuilder
Jian Li753280e2018-07-03 02:24:34 +0900701 .matchIPProtocol(statsFlowRule.ipProtocol())
702 .matchUdpSrc(statsFlowRule.srcTpPort())
703 .matchUdpDst(statsFlowRule.dstTpPort());
Jian Li0bbbb1c2018-06-22 22:01:17 +0900704 } else {
705 log.warn("Unsupported protocol {}", statsFlowRule.ipProtocol());
Boyoung Jeong9e8faec2018-06-17 21:19:23 +0900706 }
Jian Li0bbbb1c2018-06-22 22:01:17 +0900707
708 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
709
710 treatmentBuilder.transition(toTable);
711
712 FlowRule flowRule = DefaultFlowRule.builder()
Jian Li753280e2018-07-03 02:24:34 +0900713 .forDevice(deviceId)
714 .withSelector(selectorBuilder.build())
715 .withTreatment(treatmentBuilder.build())
716 .withPriority(prefixLength)
Jian Lif8b8c7f2018-08-27 18:49:04 +0900717 .fromApp(telemetryAppId)
Jian Li753280e2018-07-03 02:24:34 +0900718 .makePermanent()
719 .forTable(fromTable)
720 .build();
Jian Li0bbbb1c2018-06-22 22:01:17 +0900721
722 applyRule(flowRule, install);
Boyoung Jeong9e8faec2018-06-17 21:19:23 +0900723 }
724
725 /**
Jian Li0bbbb1c2018-06-22 22:01:17 +0900726 * Installs stats related flow rule to switch.
Boyoung Jeong9e8faec2018-06-17 21:19:23 +0900727 *
Jian Li0bbbb1c2018-06-22 22:01:17 +0900728 * @param flowRule flow rule
729 * @param install flag to install or not
Boyoung Jeong9e8faec2018-06-17 21:19:23 +0900730 */
731 private void applyRule(FlowRule flowRule, boolean install) {
Boyoung Jeong9e8faec2018-06-17 21:19:23 +0900732 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
Jian Li0bbbb1c2018-06-22 22:01:17 +0900733 flowOpsBuilder = install ?
734 flowOpsBuilder.add(flowRule) : flowOpsBuilder.remove(flowRule);
Boyoung Jeong9e8faec2018-06-17 21:19:23 +0900735
736 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
737 @Override
738 public void onSuccess(FlowRuleOperations ops) {
Jian Lia4947682018-07-07 14:53:32 +0900739 log.debug("Install rules for telemetry stats: \n {}",
740 ops.toString());
Boyoung Jeong9e8faec2018-06-17 21:19:23 +0900741 }
742
743 @Override
744 public void onError(FlowRuleOperations ops) {
Jian Lia4947682018-07-07 14:53:32 +0900745 log.debug("Failed to install rules for telemetry stats: \n {}",
746 ops.toString());
Boyoung Jeong9e8faec2018-06-17 21:19:23 +0900747 }
748 }));
749 }
750
751 /**
Jian Li0bbbb1c2018-06-22 22:01:17 +0900752 * Merges old FlowInfo.StatsInfo and current FlowInfo.StatsInfo.
Boyoung Jeong9e8faec2018-06-17 21:19:23 +0900753 *
754 * @param flowInfo current FlowInfo object
755 * @param fBuilder Builder for FlowInfo
756 * @param sBuilder Builder for StatsInfo
757 * @return Merged FlowInfo object
758 */
759 private FlowInfo mergeFlowInfo(FlowInfo flowInfo,
760 FlowInfo.Builder fBuilder,
761 StatsInfo.Builder sBuilder) {
Jian Li0bbbb1c2018-06-22 22:01:17 +0900762 for (FlowInfo gFlowInfo : gFlowInfoSet) {
763 log.debug("Old FlowInfo:\n{}", gFlowInfo.toString());
764 if (gFlowInfo.roughEquals(flowInfo)) {
765
766 // Get old StatsInfo object and merge the value to current object.
767 StatsInfo oldStatsInfo = gFlowInfo.statsInfo();
768 sBuilder.withPrevAccPkts(oldStatsInfo.currAccPkts());
769 sBuilder.withPrevAccBytes(oldStatsInfo.currAccBytes());
770 FlowInfo newFlowInfo = fBuilder.withStatsInfo(sBuilder.build())
771 .build();
772
773 gFlowInfoSet.remove(gFlowInfo);
774 gFlowInfoSet.add(newFlowInfo);
Jian Li85573f42018-06-27 22:29:14 +0900775 log.debug("Old FlowInfo found, Merge this {}", newFlowInfo.toString());
Jian Li0bbbb1c2018-06-22 22:01:17 +0900776 return newFlowInfo;
Boyoung Jeong9e8faec2018-06-17 21:19:23 +0900777 }
Boyoung Jeong9e8faec2018-06-17 21:19:23 +0900778 }
Jian Li0bbbb1c2018-06-22 22:01:17 +0900779
780 // No such record, then build the FlowInfo object and return this object.
Jian Li85573f42018-06-27 22:29:14 +0900781 log.debug("No FlowInfo found, add new FlowInfo {}", flowInfo.toString());
Jian Li0bbbb1c2018-06-22 22:01:17 +0900782 FlowInfo newFlowInfo = fBuilder.withStatsInfo(sBuilder.build()).build();
783 gFlowInfoSet.add(newFlowInfo);
784 return newFlowInfo;
785 }
786
Jian Li753280e2018-07-03 02:24:34 +0900787 /**
788 * Installs flow rules for collecting both normal and reverse path flow stats.
789 *
790 * @param statsFlowRule flow rule used for collecting stats
791 * @param install flow rule installation flag
792 */
Jian Li0bbbb1c2018-06-22 22:01:17 +0900793 private void setStatFlowRule(StatsFlowRule statsFlowRule, boolean install) {
Jian Li753280e2018-07-03 02:24:34 +0900794 setStatFlowRuleBase(statsFlowRule, install);
Jian Li0bbbb1c2018-06-22 22:01:17 +0900795
Jian Li753280e2018-07-03 02:24:34 +0900796 // if reverse path stats is enabled, we will install flow rules for
797 // collecting reverse path vFlow stats
798 if (reversePathStats) {
799 StatsFlowRule reverseFlowRule = DefaultStatsFlowRule.builder()
800 .srcIpPrefix(statsFlowRule.dstIpPrefix())
801 .dstIpPrefix(statsFlowRule.srcIpPrefix())
802 .ipProtocol(statsFlowRule.ipProtocol())
803 .srcTpPort(statsFlowRule.dstTpPort())
804 .dstTpPort(statsFlowRule.srcTpPort())
805 .build();
806 setStatFlowRuleBase(reverseFlowRule, install);
807 }
808 }
809
810 /**
811 * A base method which is for installing flow rules for collecting stats.
812 *
813 * @param statsFlowRule flow rule used for collecting stats
814 * @param install flow rule installation flag
815 */
816 private void setStatFlowRuleBase(StatsFlowRule statsFlowRule, boolean install) {
Jian Lie6110b72018-07-06 19:06:36 +0900817
818 IpPrefix srcIp = statsFlowRule.srcIpPrefix();
819 IpPrefix dstIp = statsFlowRule.dstIpPrefix();
820 DeviceId srcDeviceId = getDeviceId(srcIp.address());
821 DeviceId dstDeviceId = getDeviceId(dstIp.address());
Jian Li0bbbb1c2018-06-22 22:01:17 +0900822
Jian Li998ec7b2018-06-29 15:15:49 +0900823 if (srcDeviceId == null && dstDeviceId == null) {
Jian Li85573f42018-06-27 22:29:14 +0900824 return;
825 }
826
Jian Li998ec7b2018-06-29 15:15:49 +0900827 if (srcDeviceId != null) {
Jian Li87ded822018-07-02 18:31:22 +0900828 connectTables(srcDeviceId, STAT_INBOUND_TABLE, VTAP_INBOUND_TABLE,
Jian Li998ec7b2018-06-29 15:15:49 +0900829 statsFlowRule, METRIC_PRIORITY_SOURCE, install);
Jian Li998ec7b2018-06-29 15:15:49 +0900830
Jian Lie6110b72018-07-06 19:06:36 +0900831 if (install) {
832 log.info("Install ingress stat flow rule for SrcIp:{} DstIp:{}",
833 srcIp.toString(), dstIp.toString());
834 } else {
835 log.info("Remove ingress stat flow rule for SrcIp:{} DstIp:{}",
836 srcIp.toString(), dstIp.toString());
Jian Li753280e2018-07-03 02:24:34 +0900837 }
Jian Li998ec7b2018-06-29 15:15:49 +0900838 }
Jian Li85573f42018-06-27 22:29:14 +0900839
Jian Lie6110b72018-07-06 19:06:36 +0900840 Set<IpPrefix> vxlanIps = osNetworkService.getFixedIpsByNetworkType(VXLAN);
841 Set<IpPrefix> vlanIps = osNetworkService.getFixedIpsByNetworkType(VLAN);
842 Set<IpPrefix> flatIps = osNetworkService.getFixedIpsByNetworkType(FLAT);
Jian Li753280e2018-07-03 02:24:34 +0900843
Jian Lie6110b72018-07-06 19:06:36 +0900844 int fromTable, toTable;
Jian Li753280e2018-07-03 02:24:34 +0900845
Jian Lie6110b72018-07-06 19:06:36 +0900846 if (dstDeviceId != null && egressStats) {
847
848 IpPrefix dstIpPrefix = statsFlowRule.dstIpPrefix();
849
850 if (vxlanIps.contains(dstIpPrefix) || vlanIps.contains(dstIpPrefix)) {
851 fromTable = STAT_OUTBOUND_TABLE;
852 toTable = VTAP_OUTBOUND_TABLE;
853 } else if (flatIps.contains(dstIpPrefix)) {
854 fromTable = STAT_FLAT_OUTBOUND_TABLE;
855 toTable = VTAP_FLAT_OUTBOUND_TABLE;
856 } else {
857 return;
858 }
859
860 connectTables(dstDeviceId, fromTable, toTable,
861 statsFlowRule, METRIC_PRIORITY_TARGET, install);
862
863 if (install) {
864 log.info("Install egress stat flow rule for SrcIp:{} DstIp:{}",
865 srcIp.toString(), dstIp.toString());
866 } else {
867 log.info("Remove egress stat flow rule for SrcIp:{} DstIp:{}",
868 srcIp.toString(), dstIp.toString());
869 }
Jian Li753280e2018-07-03 02:24:34 +0900870 }
Jian Li753280e2018-07-03 02:24:34 +0900871 }
872
873 /**
Jian Lif8b8c7f2018-08-27 18:49:04 +0900874 * Gets Device ID which the VM is located.
Jian Li85573f42018-06-27 22:29:14 +0900875 *
876 * @param ipAddress IP Address of host
877 * @return Device ID
878 */
879 private DeviceId getDeviceId(IpAddress ipAddress) {
880 if (!hostService.getHostsByIp(ipAddress).isEmpty()) {
881 Optional<Host> host = hostService.getHostsByIp(ipAddress).stream().findAny();
882 return host.map(host1 -> host1.location().deviceId()).orElse(null);
883 } else {
Jian Lia4947682018-07-07 14:53:32 +0900884 log.debug("No DeviceID is associated to {}", ipAddress.toString());
Jian Li85573f42018-06-27 22:29:14 +0900885 return null;
Jian Li0bbbb1c2018-06-22 22:01:17 +0900886 }
Boyoung Jeong9e8faec2018-06-17 21:19:23 +0900887 }
888
889 /**
Jian Lif8b8c7f2018-08-27 18:49:04 +0900890 * Gets VLAN ID with respect to IP Address.
Boyoung Jeong9e8faec2018-06-17 21:19:23 +0900891 *
892 * @param ipAddress IP Address of host
893 * @return VLAN ID
894 */
Jian Li0bbbb1c2018-06-22 22:01:17 +0900895 private VlanId getVlanId(IpAddress ipAddress) {
896 if (!hostService.getHostsByIp(ipAddress).isEmpty()) {
897 Host host = hostService.getHostsByIp(ipAddress).stream().findAny().get();
898 return host.vlan();
Boyoung Jeong9e8faec2018-06-17 21:19:23 +0900899 }
900 return VlanId.vlanId();
901 }
902
903 /**
Jian Lif8b8c7f2018-08-27 18:49:04 +0900904 * Gets Interface ID of Switch which is connected to a host.
Boyoung Jeong9e8faec2018-06-17 21:19:23 +0900905 *
906 * @param ipAddress IP Address of host
907 * @return Interface ID of Switch
908 */
Jian Li0bbbb1c2018-06-22 22:01:17 +0900909 private int getInterfaceId(IpAddress ipAddress) {
910 if (!hostService.getHostsByIp(ipAddress).isEmpty()) {
911 Host host = hostService.getHostsByIp(ipAddress).stream().findAny().get();
912 return (int) host.location().port().toLong();
Boyoung Jeong9e8faec2018-06-17 21:19:23 +0900913 }
914 return -1;
915 }
916
917 /**
Jian Lif8b8c7f2018-08-27 18:49:04 +0900918 * Gets MAC Address of host.
Boyoung Jeong9e8faec2018-06-17 21:19:23 +0900919 *
920 * @param ipAddress IP Address of host
921 * @return MAC Address of host
922 */
Jian Li0bbbb1c2018-06-22 22:01:17 +0900923 private MacAddress getMacAddress(IpAddress ipAddress) {
924 if (!hostService.getHostsByIp(ipAddress).isEmpty()) {
925 Host host = hostService.getHostsByIp(ipAddress).stream().findAny().get();
926 return host.mac();
Boyoung Jeong9e8faec2018-06-17 21:19:23 +0900927 }
Jian Li0bbbb1c2018-06-22 22:01:17 +0900928
Boyoung Jeong9e8faec2018-06-17 21:19:23 +0900929 return NO_HOST_MAC;
930 }
Jian Li0bbbb1c2018-06-22 22:01:17 +0900931
Jian Lif8b8c7f2018-08-27 18:49:04 +0900932 /**
933 * Gets IP address of the host which is attached to the given device and port.
934 *
935 * @param device device
936 * @param inPort IN port number
937 * @return IP address
938 */
939 private IpAddress getIpAddress(Device device, PortCriterion inPort) {
940
941 Host host = hostService.getConnectedHosts(device.id()).stream()
942 .filter(h -> h.location().port().equals(inPort.port()))
943 .findAny().orElse(null);
944
945 if (host != null) {
946 return host.ipAddresses().stream().findAny().get();
947 }
948
949 return NO_HOST_IP;
950 }
951
Boyoung Jeong1cca5e82018-08-01 21:00:08 +0900952 private void enqFlowInfo(FlowInfo flowInfo) {
953 String key = flowInfo.uniqueFlowInfoKey();
954 Queue<FlowInfo> queue = flowInfoMap.get(key);
955 if (queue == null) {
956 Queue<FlowInfo> newQueue = new LinkedList<FlowInfo>();
957 newQueue.offer(flowInfo);
958 flowInfoMap.put(key, newQueue);
959 return;
960 }
961 queue.offer(flowInfo);
962
963 while (queue.size() > DEFAULT_DATA_POINT_SIZE) {
964 queue.remove(); // Removes a garbage data in the queue.
965 }
966 }
967
Jian Lif8b8c7f2018-08-27 18:49:04 +0900968 /**
969 * Checks whether the given device is edge switch or not.
970 *
971 * @param id device identifier
972 * @return true if the given device is edge switch, false otherwise
973 */
974 private boolean isEdgeSwitch(DeviceId id) {
975
976 return !hostService.getConnectedHosts(id).isEmpty();
Boyoung Jeong1cca5e82018-08-01 21:00:08 +0900977 }
978
Jian Li753280e2018-07-03 02:24:34 +0900979 /**
980 * Extracts properties from the component configuration context.
981 *
982 * @param context the component context
983 */
984 private void readComponentConfiguration(ComponentContext context) {
985 Dictionary<?, ?> properties = context.getProperties();
986
987 Boolean reversePathStatsConfigured =
Ray Milkey8e406512018-10-24 15:56:50 -0700988 getBooleanProperty(properties, PROP_REVERSE_PATH_STATS);
Jian Li753280e2018-07-03 02:24:34 +0900989 if (reversePathStatsConfigured == null) {
Ray Milkey8e406512018-10-24 15:56:50 -0700990 reversePathStats = PROP_REVERSE_PATH_STATS_DEFAULT;
Jian Li753280e2018-07-03 02:24:34 +0900991 log.info("Reversed path stats flag is NOT " +
992 "configured, default value is {}", reversePathStats);
993 } else {
994 reversePathStats = reversePathStatsConfigured;
995 log.info("Configured. Reversed path stats flag is {}", reversePathStats);
996 }
997
Ray Milkey8e406512018-10-24 15:56:50 -0700998 Boolean egressStatsConfigured = getBooleanProperty(properties, PROP_EGRESS_STATS);
Jian Li753280e2018-07-03 02:24:34 +0900999 if (egressStatsConfigured == null) {
Ray Milkey8e406512018-10-24 15:56:50 -07001000 egressStats = PROP_EGRESS_STATS_DEFAULT;
Jian Li753280e2018-07-03 02:24:34 +09001001 log.info("Egress stats flag is NOT " +
1002 "configured, default value is {}", egressStats);
1003 } else {
1004 egressStats = egressStatsConfigured;
1005 log.info("Configured. Egress stats flag is {}", egressStats);
1006 }
Jian Lia4947682018-07-07 14:53:32 +09001007
Ray Milkey8e406512018-10-24 15:56:50 -07001008 Boolean portStatsConfigured = getBooleanProperty(properties, PROP_PORT_STATS);
Jian Lia4947682018-07-07 14:53:32 +09001009 if (portStatsConfigured == null) {
Ray Milkey8e406512018-10-24 15:56:50 -07001010 portStats = PROP_PORT_STATS_DEFAULT;
Jian Lia4947682018-07-07 14:53:32 +09001011 log.info("Port stats flag is NOT " +
1012 "configured, default value is {}", portStats);
1013 } else {
1014 portStats = portStatsConfigured;
1015 log.info("Configured. Port stats flag is {}", portStats);
1016 }
Jian Lif8b8c7f2018-08-27 18:49:04 +09001017
Ray Milkey8e406512018-10-24 15:56:50 -07001018 Boolean monitorOverlayConfigured = getBooleanProperty(properties, PROP_MONITOR_OVERLAY);
Jian Lif8b8c7f2018-08-27 18:49:04 +09001019 if (monitorOverlayConfigured == null) {
Ray Milkey8e406512018-10-24 15:56:50 -07001020 monitorOverlay = PROP_MONITOR_OVERLAY_DEFAULT;
Jian Lif8b8c7f2018-08-27 18:49:04 +09001021 log.info("Monitor overlay flag is NOT " +
1022 "configured, default value is {}", monitorOverlay);
1023 } else {
1024 monitorOverlay = monitorOverlayConfigured;
1025 log.info("Configured. Monitor overlay flag is {}", monitorOverlay);
1026 }
1027
Ray Milkey8e406512018-10-24 15:56:50 -07001028 Boolean monitorUnderlayConfigured = getBooleanProperty(properties, PROP_MONITOR_UNDERLAY);
Jian Lif8b8c7f2018-08-27 18:49:04 +09001029 if (monitorUnderlayConfigured == null) {
Ray Milkey8e406512018-10-24 15:56:50 -07001030 monitorUnderlay = PROP_MONITOR_UNDERLAY_DEFAULT;
Jian Lif8b8c7f2018-08-27 18:49:04 +09001031 log.info("Monitor underlay flag is NOT " +
1032 "configured, default value is {}", monitorUnderlay);
1033 } else {
1034 monitorUnderlay = monitorUnderlayConfigured;
1035 log.info("Configured. Monitor underlay flag is {}", monitorUnderlay);
1036 }
Jian Li753280e2018-07-03 02:24:34 +09001037 }
1038
Jian Lia4947682018-07-07 14:53:32 +09001039 private class TelemetryCollector implements Runnable {
Jian Li0bbbb1c2018-06-22 22:01:17 +09001040 @Override
1041 public void run() {
Jian Lif8b8c7f2018-08-27 18:49:04 +09001042 Set<FlowInfo> filteredOverlayFlowInfos = Sets.newConcurrentHashSet();
1043 Set<FlowInfo> filteredUnderlayFlowInfos = Sets.newConcurrentHashSet();
Jian Li85573f42018-06-27 22:29:14 +09001044
1045 // we only let the master controller of the device where the
Jian Lia4947682018-07-07 14:53:32 +09001046 // stats flow rules are installed send stats message
Jian Lif8b8c7f2018-08-27 18:49:04 +09001047 if (monitorOverlay) {
1048 getOverlayFlowInfos().forEach(f -> {
Jian Lia4947682018-07-07 14:53:32 +09001049 if (checkSrcDstLocalMaster(f)) {
Jian Lif8b8c7f2018-08-27 18:49:04 +09001050 filteredOverlayFlowInfos.add(f);
1051 }
1052 });
1053 }
1054 if (monitorUnderlay) {
1055 getUnderlayFlowInfos().forEach(f -> {
1056 if (checkSrcDstLocalMaster(f)) {
1057 filteredUnderlayFlowInfos.add(f);
Jian Lia4947682018-07-07 14:53:32 +09001058 }
1059 });
Jian Li0bbbb1c2018-06-22 22:01:17 +09001060 }
Jian Lia4947682018-07-07 14:53:32 +09001061
Jian Lif8b8c7f2018-08-27 18:49:04 +09001062 // we only let the master controller of the device where the port
1063 // is located to send stats message
1064 if (portStats) {
1065 if (monitorOverlay) {
1066 getOverlayDstPortBasedFlowInfos().forEach(f -> {
1067 if (checkSrcDstLocalMaster(f)) {
1068 filteredOverlayFlowInfos.add(f);
1069 }
1070 });
1071 }
Boyoung Jeong1cca5e82018-08-01 21:00:08 +09001072
Jian Lif8b8c7f2018-08-27 18:49:04 +09001073 if (monitorUnderlay) {
1074 getUnderlayDstPortBasedFlowInfos().forEach(f -> {
1075 if (checkSrcDstLocalMaster(f)) {
1076 filteredUnderlayFlowInfos.add(f);
1077 }
1078 });
1079 }
1080 }
1081
1082
1083 if (monitorOverlay) {
1084 telemetryService.publish(filteredOverlayFlowInfos);
1085
1086 // TODO: Refactor the following code to "TelemetryService" style.
1087 filteredOverlayFlowInfos.forEach(StatsFlowRuleManager.this::enqFlowInfo);
1088 }
1089
1090 if (monitorUnderlay) {
1091 telemetryService.publish(filteredUnderlayFlowInfos);
1092 }
Jian Lia4947682018-07-07 14:53:32 +09001093 }
1094
1095 private boolean checkSrcDstLocalMaster(FlowInfo info) {
1096 DeviceId srcDeviceId = getDeviceId(info.srcIp().address());
1097 DeviceId dstDeviceId = getDeviceId(info.dstIp().address());
1098
1099 boolean isSrcLocalMaster = srcDeviceId != null &&
1100 mastershipService.isLocalMaster(srcDeviceId);
1101 boolean isDstLocalMaster = dstDeviceId != null &&
1102 mastershipService.isLocalMaster(dstDeviceId);
1103
1104 return isSrcLocalMaster || isDstLocalMaster;
Jian Li0bbbb1c2018-06-22 22:01:17 +09001105 }
1106 }
Boyoung Jeong9e8faec2018-06-17 21:19:23 +09001107}