Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.openstacktelemetry.impl; |
| 17 | |
Boyoung Jeong | 1cca5e8 | 2018-08-01 21:00:08 +0900 | [diff] [blame] | 18 | import com.google.common.collect.Maps; |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 19 | import com.google.common.collect.Sets; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 20 | import org.onlab.packet.IpAddress; |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 21 | import org.onlab.packet.IpPrefix; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 22 | import org.onlab.packet.MacAddress; |
| 23 | import org.onlab.packet.VlanId; |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 24 | import org.onlab.util.SharedScheduledExecutors; |
Jian Li | e6110b7 | 2018-07-06 19:06:36 +0900 | [diff] [blame] | 25 | import org.onosproject.cfg.ComponentConfigService; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 26 | import org.onosproject.core.ApplicationId; |
| 27 | import org.onosproject.core.CoreService; |
Jian Li | 85573f4 | 2018-06-27 22:29:14 +0900 | [diff] [blame] | 28 | import org.onosproject.mastership.MastershipService; |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 29 | import org.onosproject.net.ConnectPoint; |
| 30 | import org.onosproject.net.Device; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 31 | import org.onosproject.net.DeviceId; |
| 32 | import org.onosproject.net.Host; |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 33 | import org.onosproject.net.PortNumber; |
| 34 | import org.onosproject.net.device.DeviceService; |
| 35 | import org.onosproject.net.device.PortStatistics; |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 36 | import org.onosproject.net.driver.Driver; |
| 37 | import org.onosproject.net.driver.DriverService; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 38 | import org.onosproject.net.flow.DefaultFlowRule; |
| 39 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 40 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 41 | import org.onosproject.net.flow.FlowEntry; |
| 42 | import org.onosproject.net.flow.FlowRule; |
| 43 | import org.onosproject.net.flow.FlowRuleOperations; |
| 44 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
| 45 | import org.onosproject.net.flow.FlowRuleService; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 46 | import org.onosproject.net.flow.TrafficSelector; |
| 47 | import org.onosproject.net.flow.TrafficTreatment; |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 48 | import org.onosproject.net.flow.criteria.Criterion; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 49 | import org.onosproject.net.flow.criteria.IPCriterion; |
| 50 | import org.onosproject.net.flow.criteria.IPProtocolCriterion; |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 51 | import org.onosproject.net.flow.criteria.PortCriterion; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 52 | import org.onosproject.net.flow.criteria.TcpPortCriterion; |
| 53 | import org.onosproject.net.flow.criteria.UdpPortCriterion; |
| 54 | import org.onosproject.net.host.HostService; |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 55 | import org.onosproject.openstacknetworking.api.InstancePort; |
| 56 | import org.onosproject.openstacknetworking.api.InstancePortService; |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 57 | import org.onosproject.openstacknetworking.api.OpenstackNetworkService; |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 58 | import org.onosproject.openstacknode.api.OpenstackNode; |
| 59 | import org.onosproject.openstacknode.api.OpenstackNodeService; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 60 | import org.onosproject.openstacktelemetry.api.FlowInfo; |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 61 | import org.onosproject.openstacktelemetry.api.OpenstackTelemetryService; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 62 | import org.onosproject.openstacktelemetry.api.StatsFlowRule; |
| 63 | import org.onosproject.openstacktelemetry.api.StatsFlowRuleAdminService; |
| 64 | import org.onosproject.openstacktelemetry.api.StatsInfo; |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 65 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 66 | import org.osgi.service.component.annotations.Activate; |
| 67 | import org.osgi.service.component.annotations.Component; |
| 68 | import org.osgi.service.component.annotations.Deactivate; |
| 69 | import org.osgi.service.component.annotations.Modified; |
| 70 | import org.osgi.service.component.annotations.Reference; |
| 71 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 72 | import org.slf4j.Logger; |
| 73 | import org.slf4j.LoggerFactory; |
| 74 | |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 75 | import java.util.ArrayList; |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 76 | import java.util.Dictionary; |
Boyoung Jeong | 1cca5e8 | 2018-08-01 21:00:08 +0900 | [diff] [blame] | 77 | import java.util.LinkedList; |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 78 | import java.util.List; |
Boyoung Jeong | 1cca5e8 | 2018-08-01 21:00:08 +0900 | [diff] [blame] | 79 | import java.util.Map; |
Jian Li | 85573f4 | 2018-06-27 22:29:14 +0900 | [diff] [blame] | 80 | import java.util.Optional; |
Boyoung Jeong | 1cca5e8 | 2018-08-01 21:00:08 +0900 | [diff] [blame] | 81 | import java.util.Queue; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 82 | import java.util.Set; |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 83 | import java.util.concurrent.ScheduledFuture; |
| 84 | import java.util.concurrent.TimeUnit; |
| 85 | import java.util.stream.Collectors; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 86 | |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 87 | import static org.onlab.packet.Ethernet.TYPE_IPV4; |
| 88 | import static org.onlab.packet.IPv4.PROTOCOL_TCP; |
| 89 | import static org.onlab.packet.IPv4.PROTOCOL_UDP; |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 90 | import static org.onosproject.net.Device.Type.SWITCH; |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 91 | import static org.onosproject.net.flow.criteria.Criterion.Type.IPV4_DST; |
| 92 | import static org.onosproject.net.flow.criteria.Criterion.Type.IPV4_SRC; |
| 93 | import static org.onosproject.net.flow.criteria.Criterion.Type.IP_PROTO; |
| 94 | import static org.onosproject.net.flow.criteria.Criterion.Type.TCP_DST; |
| 95 | import static org.onosproject.net.flow.criteria.Criterion.Type.TCP_SRC; |
| 96 | import static org.onosproject.net.flow.criteria.Criterion.Type.UDP_DST; |
| 97 | import static org.onosproject.net.flow.criteria.Criterion.Type.UDP_SRC; |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 98 | import static org.onosproject.openstacknetworking.api.Constants.STAT_FLAT_OUTBOUND_TABLE; |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 99 | import static org.onosproject.openstacknetworking.api.Constants.STAT_INBOUND_TABLE; |
| 100 | import static org.onosproject.openstacknetworking.api.Constants.STAT_OUTBOUND_TABLE; |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 101 | import static org.onosproject.openstacknetworking.api.Constants.VTAP_FLAT_OUTBOUND_TABLE; |
Jian Li | 87ded82 | 2018-07-02 18:31:22 +0900 | [diff] [blame] | 102 | import static org.onosproject.openstacknetworking.api.Constants.VTAP_INBOUND_TABLE; |
| 103 | import static org.onosproject.openstacknetworking.api.Constants.VTAP_OUTBOUND_TABLE; |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 104 | import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.COMPUTE; |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 105 | import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.CONTROLLER; |
Boyoung Jeong | 1cca5e8 | 2018-08-01 21:00:08 +0900 | [diff] [blame] | 106 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_DATA_POINT_SIZE; |
Jian Li | e6110b7 | 2018-07-06 19:06:36 +0900 | [diff] [blame] | 107 | import static org.onosproject.openstacktelemetry.api.Constants.FLAT; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 108 | import static org.onosproject.openstacktelemetry.api.Constants.OPENSTACK_TELEMETRY_APP_ID; |
Jian Li | e6110b7 | 2018-07-06 19:06:36 +0900 | [diff] [blame] | 109 | import static org.onosproject.openstacktelemetry.api.Constants.VLAN; |
| 110 | import static org.onosproject.openstacktelemetry.api.Constants.VXLAN; |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 111 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_EGRESS_STATS; |
| 112 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_EGRESS_STATS_DEFAULT; |
| 113 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_MONITOR_OVERLAY; |
| 114 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_MONITOR_OVERLAY_DEFAULT; |
| 115 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_MONITOR_UNDERLAY; |
| 116 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_MONITOR_UNDERLAY_DEFAULT; |
| 117 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_PORT_STATS; |
| 118 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_PORT_STATS_DEFAULT; |
| 119 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_REVERSE_PATH_STATS; |
| 120 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_REVERSE_PATH_STATS_DEFAULT; |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 121 | import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.getBooleanProperty; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 122 | |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 123 | /** |
| 124 | * Flow rule manager for network statistics of a VM. |
| 125 | */ |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 126 | @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 Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 137 | public 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 Milkey | bcc53d3 | 2018-07-02 10:22:57 -0700 | [diff] [blame] | 143 | private static final long MILLISECONDS = 1000L; |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 144 | private static final long INITIAL_DELAY = 5L; |
Ray Milkey | bcc53d3 | 2018-07-02 10:22:57 -0700 | [diff] [blame] | 145 | private static final long REFRESH_INTERVAL = 5L; |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 146 | private static final TimeUnit TIME_UNIT_SECOND = TimeUnit.SECONDS; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 147 | |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 148 | private static final String OVS_DRIVER_NAME = "ovs"; |
| 149 | |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 150 | private static final String ARBITRARY_IP = "0.0.0.0/32"; |
boyoung2 | 7b44412 | 2018-09-01 17:28:13 +0900 | [diff] [blame] | 151 | private static final int ARBITRARY_PROTOCOL = 0x0; |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 152 | private static final int ARBITRARY_LENGTH = 32; |
| 153 | private static final String ARBITRARY_MAC = "00:00:00:00:00:00"; |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 154 | private static final IpAddress NO_HOST_IP = IpAddress.valueOf("255.255.255.255"); |
Boyoung Jeong | 1cca5e8 | 2018-08-01 21:00:08 +0900 | [diff] [blame] | 155 | private static final MacAddress NO_HOST_MAC = MacAddress.valueOf(ARBITRARY_MAC); |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 156 | 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 Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 161 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 162 | protected CoreService coreService; |
| 163 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 164 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 165 | protected FlowRuleService flowRuleService; |
| 166 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 167 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 168 | protected HostService hostService; |
| 169 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 170 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 171 | protected DeviceService deviceService; |
| 172 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 173 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 174 | protected DriverService driverService; |
| 175 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 176 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | e6110b7 | 2018-07-06 19:06:36 +0900 | [diff] [blame] | 177 | protected ComponentConfigService componentConfigService; |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 178 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 179 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | 85573f4 | 2018-06-27 22:29:14 +0900 | [diff] [blame] | 180 | protected MastershipService mastershipService; |
| 181 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 182 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 183 | protected OpenstackNetworkService osNetworkService; |
| 184 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 185 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 186 | protected InstancePortService instPortService; |
| 187 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 188 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 189 | protected OpenstackNodeService osNodeService; |
| 190 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 191 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 192 | protected OpenstackTelemetryService telemetryService; |
| 193 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 194 | /** 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 Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 196 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 197 | /** 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 Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 199 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 200 | /** A flag which indicates whether to collect port TX & RX stats. */ |
| 201 | private boolean portStats = PROP_PORT_STATS_DEFAULT; |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 202 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 203 | /** A flag which indicates whether to monitor overlay network port stats. */ |
| 204 | private boolean monitorOverlay = PROP_MONITOR_OVERLAY_DEFAULT; |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 205 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 206 | /** A flag which indicates whether to monitor underlay network port stats. */ |
| 207 | private boolean monitorUnderlay = PROP_MONITOR_UNDERLAY_DEFAULT; |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 208 | |
| 209 | private ApplicationId telemetryAppId; |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 210 | private TelemetryCollector collector; |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 211 | private ScheduledFuture result; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 212 | |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 213 | private final Set<FlowInfo> gFlowInfoSet = Sets.newHashSet(); |
Boyoung Jeong | 1cca5e8 | 2018-08-01 21:00:08 +0900 | [diff] [blame] | 214 | private final Map<String, Queue<FlowInfo>> flowInfoMap = Maps.newConcurrentMap(); |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 215 | |
| 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 Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 222 | @Activate |
| 223 | protected void activate() { |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 224 | telemetryAppId = coreService.registerApplication(OPENSTACK_TELEMETRY_APP_ID); |
| 225 | |
Jian Li | e6110b7 | 2018-07-06 19:06:36 +0900 | [diff] [blame] | 226 | componentConfigService.registerProperties(getClass()); |
Jian Li | bd295cd | 2018-07-22 11:53:57 +0900 | [diff] [blame] | 227 | start(); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 228 | |
| 229 | log.info("Started"); |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | @Deactivate |
| 233 | protected void deactivate() { |
Jian Li | e6110b7 | 2018-07-06 19:06:36 +0900 | [diff] [blame] | 234 | componentConfigService.unregisterProperties(getClass(), false); |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 235 | flowRuleService.removeFlowRulesById(telemetryAppId); |
Jian Li | bd295cd | 2018-07-22 11:53:57 +0900 | [diff] [blame] | 236 | stop(); |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 237 | |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 238 | log.info("Stopped"); |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 239 | } |
| 240 | |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 241 | @Modified |
| 242 | protected void modified(ComponentContext context) { |
| 243 | readComponentConfiguration(context); |
| 244 | |
| 245 | log.info("Modified"); |
| 246 | } |
| 247 | |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 248 | @Override |
| 249 | public void start() { |
| 250 | log.info("Start publishing thread"); |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 251 | collector = new TelemetryCollector(); |
| 252 | |
Jian Li | bd295cd | 2018-07-22 11:53:57 +0900 | [diff] [blame] | 253 | result = SharedScheduledExecutors.getSingleThreadExecutor() |
| 254 | .scheduleAtFixedRate(collector, INITIAL_DELAY, |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 255 | REFRESH_INTERVAL, TIME_UNIT_SECOND, RECOVER_FROM_FAILURE); |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | @Override |
| 259 | public void stop() { |
| 260 | log.info("Stop data publishing thread"); |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 261 | result.cancel(true); |
| 262 | collector = null; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 263 | } |
| 264 | |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 265 | @Override |
boyoung2 | 7b44412 | 2018-09-01 17:28:13 +0900 | [diff] [blame] | 266 | 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 Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 274 | |
boyoung2 | 7b44412 | 2018-09-01 17:28:13 +0900 | [diff] [blame] | 275 | @Override |
| 276 | public void createStatFlowRule(StatsFlowRule statsFlowRule) { |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 277 | setStatFlowRule(statsFlowRule, true); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | @Override |
| 281 | public void deleteStatFlowRule(StatsFlowRule statsFlowRule) { |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 282 | setStatFlowRule(statsFlowRule, false); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 283 | } |
| 284 | |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 285 | |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 286 | @Override |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 287 | 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 Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 350 | Set<FlowInfo> flowInfos = Sets.newConcurrentHashSet(); |
| 351 | |
| 352 | // obtain all flow rule entries installed by telemetry app |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 353 | for (FlowEntry entry : flowRuleService.getFlowEntriesById(telemetryAppId)) { |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 354 | FlowInfo.Builder fBuilder = new DefaultFlowInfo.DefaultBuilder(); |
| 355 | TrafficSelector selector = entry.selector(); |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 356 | 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 Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 361 | 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 Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 373 | fBuilder.withSrcPort(tcpSrc.tcpPort()); |
| 374 | fBuilder.withDstPort(tcpDst.tcpPort()); |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 375 | } else if (ipProtocol.protocol() == PROTOCOL_UDP) { |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 376 | UdpPortCriterion udpSrc = |
| 377 | (UdpPortCriterion) selector.getCriterion(UDP_SRC); |
| 378 | UdpPortCriterion udpDst = |
| 379 | (UdpPortCriterion) selector.getCriterion(UDP_DST); |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 380 | 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 Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 396 | 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 Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 417 | * Gets a set of flow infos by referring to overlay destination VM port. |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 418 | * |
| 419 | * @return flow infos |
| 420 | */ |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 421 | private Set<FlowInfo> getOverlayDstPortBasedFlowInfos() { |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 422 | 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 Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 441 | flowInfos.add(buildTxFlowInfoFromInstancePort(instPort, s)); |
| 442 | flowInfos.add(buildRxFlowInfoFromInstancePort(instPort, s)); |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 443 | }); |
| 444 | }); |
| 445 | |
| 446 | return flowInfos; |
| 447 | } |
| 448 | |
| 449 | /** |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 450 | * 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 Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 518 | * |
| 519 | * @param instPort instance port |
| 520 | * @param stat port statistics |
| 521 | * @return flow info |
| 522 | */ |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 523 | 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 Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 587 | FlowInfo.Builder fBuilder = new DefaultFlowInfo.DefaultBuilder(); |
| 588 | |
| 589 | fBuilder.withFlowType(FLOW_TYPE_SONA) |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 590 | .withSrcIp(IpPrefix.valueOf(ipAddress, ARBITRARY_LENGTH)) |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 591 | .withDstIp(IpPrefix.valueOf(ARBITRARY_IP)) |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 592 | .withSrcMac(macAddress) |
Boyoung Jeong | 1cca5e8 | 2018-08-01 21:00:08 +0900 | [diff] [blame] | 593 | .withDstMac(NO_HOST_MAC) |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 594 | .withDeviceId(deviceId) |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 595 | .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 Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 614 | * Obtains the flow info generated from RX port. |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 615 | * |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 616 | * @param ipAddress IP address |
| 617 | * @param macAddress MAC address |
| 618 | * @param deviceId Device identifier |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 619 | * @param stat port statistics |
| 620 | * @return flow info |
| 621 | */ |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 622 | private FlowInfo buildRxFlowInfo(IpAddress ipAddress, |
| 623 | MacAddress macAddress, |
| 624 | DeviceId deviceId, |
| 625 | PortStatistics stat) { |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 626 | FlowInfo.Builder fBuilder = new DefaultFlowInfo.DefaultBuilder(); |
| 627 | |
| 628 | fBuilder.withFlowType(FLOW_TYPE_SONA) |
| 629 | .withSrcIp(IpPrefix.valueOf(ARBITRARY_IP)) |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 630 | .withDstIp(IpPrefix.valueOf(ipAddress, ARBITRARY_LENGTH)) |
Boyoung Jeong | 1cca5e8 | 2018-08-01 21:00:08 +0900 | [diff] [blame] | 631 | .withSrcMac(NO_HOST_MAC) |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 632 | .withDstMac(macAddress) |
| 633 | .withDeviceId(deviceId) |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 634 | .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 Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 667 | /** |
| 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 Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 678 | private void connectTables(DeviceId deviceId, int fromTable, int toTable, |
| 679 | StatsFlowRule statsFlowRule, int rulePriority, |
| 680 | boolean install) { |
| 681 | |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 682 | 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 Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 688 | DefaultTrafficSelector.builder() |
| 689 | .matchEthType(TYPE_IPV4) |
| 690 | .matchIPSrc(statsFlowRule.srcIpPrefix()) |
| 691 | .matchIPDst(statsFlowRule.dstIpPrefix()); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 692 | |
| 693 | if (protocol == PROTOCOL_TCP) { |
| 694 | selectorBuilder = selectorBuilder |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 695 | .matchIPProtocol(statsFlowRule.ipProtocol()) |
| 696 | .matchTcpSrc(statsFlowRule.srcTpPort()) |
| 697 | .matchTcpDst(statsFlowRule.dstTpPort()); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 698 | |
| 699 | } else if (protocol == PROTOCOL_UDP) { |
| 700 | selectorBuilder = selectorBuilder |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 701 | .matchIPProtocol(statsFlowRule.ipProtocol()) |
| 702 | .matchUdpSrc(statsFlowRule.srcTpPort()) |
| 703 | .matchUdpDst(statsFlowRule.dstTpPort()); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 704 | } else { |
| 705 | log.warn("Unsupported protocol {}", statsFlowRule.ipProtocol()); |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 706 | } |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 707 | |
| 708 | TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder(); |
| 709 | |
| 710 | treatmentBuilder.transition(toTable); |
| 711 | |
| 712 | FlowRule flowRule = DefaultFlowRule.builder() |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 713 | .forDevice(deviceId) |
| 714 | .withSelector(selectorBuilder.build()) |
| 715 | .withTreatment(treatmentBuilder.build()) |
| 716 | .withPriority(prefixLength) |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 717 | .fromApp(telemetryAppId) |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 718 | .makePermanent() |
| 719 | .forTable(fromTable) |
| 720 | .build(); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 721 | |
| 722 | applyRule(flowRule, install); |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | /** |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 726 | * Installs stats related flow rule to switch. |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 727 | * |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 728 | * @param flowRule flow rule |
| 729 | * @param install flag to install or not |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 730 | */ |
| 731 | private void applyRule(FlowRule flowRule, boolean install) { |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 732 | FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder(); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 733 | flowOpsBuilder = install ? |
| 734 | flowOpsBuilder.add(flowRule) : flowOpsBuilder.remove(flowRule); |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 735 | |
| 736 | flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() { |
| 737 | @Override |
| 738 | public void onSuccess(FlowRuleOperations ops) { |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 739 | log.debug("Install rules for telemetry stats: \n {}", |
| 740 | ops.toString()); |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | @Override |
| 744 | public void onError(FlowRuleOperations ops) { |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 745 | log.debug("Failed to install rules for telemetry stats: \n {}", |
| 746 | ops.toString()); |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 747 | } |
| 748 | })); |
| 749 | } |
| 750 | |
| 751 | /** |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 752 | * Merges old FlowInfo.StatsInfo and current FlowInfo.StatsInfo. |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 753 | * |
| 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 Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 762 | 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 Li | 85573f4 | 2018-06-27 22:29:14 +0900 | [diff] [blame] | 775 | log.debug("Old FlowInfo found, Merge this {}", newFlowInfo.toString()); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 776 | return newFlowInfo; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 777 | } |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 778 | } |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 779 | |
| 780 | // No such record, then build the FlowInfo object and return this object. |
Jian Li | 85573f4 | 2018-06-27 22:29:14 +0900 | [diff] [blame] | 781 | log.debug("No FlowInfo found, add new FlowInfo {}", flowInfo.toString()); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 782 | FlowInfo newFlowInfo = fBuilder.withStatsInfo(sBuilder.build()).build(); |
| 783 | gFlowInfoSet.add(newFlowInfo); |
| 784 | return newFlowInfo; |
| 785 | } |
| 786 | |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 787 | /** |
| 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 Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 793 | private void setStatFlowRule(StatsFlowRule statsFlowRule, boolean install) { |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 794 | setStatFlowRuleBase(statsFlowRule, install); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 795 | |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 796 | // 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 Li | e6110b7 | 2018-07-06 19:06:36 +0900 | [diff] [blame] | 817 | |
| 818 | IpPrefix srcIp = statsFlowRule.srcIpPrefix(); |
| 819 | IpPrefix dstIp = statsFlowRule.dstIpPrefix(); |
| 820 | DeviceId srcDeviceId = getDeviceId(srcIp.address()); |
| 821 | DeviceId dstDeviceId = getDeviceId(dstIp.address()); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 822 | |
Jian Li | 998ec7b | 2018-06-29 15:15:49 +0900 | [diff] [blame] | 823 | if (srcDeviceId == null && dstDeviceId == null) { |
Jian Li | 85573f4 | 2018-06-27 22:29:14 +0900 | [diff] [blame] | 824 | return; |
| 825 | } |
| 826 | |
Jian Li | 998ec7b | 2018-06-29 15:15:49 +0900 | [diff] [blame] | 827 | if (srcDeviceId != null) { |
Jian Li | 87ded82 | 2018-07-02 18:31:22 +0900 | [diff] [blame] | 828 | connectTables(srcDeviceId, STAT_INBOUND_TABLE, VTAP_INBOUND_TABLE, |
Jian Li | 998ec7b | 2018-06-29 15:15:49 +0900 | [diff] [blame] | 829 | statsFlowRule, METRIC_PRIORITY_SOURCE, install); |
Jian Li | 998ec7b | 2018-06-29 15:15:49 +0900 | [diff] [blame] | 830 | |
Jian Li | e6110b7 | 2018-07-06 19:06:36 +0900 | [diff] [blame] | 831 | 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 Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 837 | } |
Jian Li | 998ec7b | 2018-06-29 15:15:49 +0900 | [diff] [blame] | 838 | } |
Jian Li | 85573f4 | 2018-06-27 22:29:14 +0900 | [diff] [blame] | 839 | |
Jian Li | e6110b7 | 2018-07-06 19:06:36 +0900 | [diff] [blame] | 840 | Set<IpPrefix> vxlanIps = osNetworkService.getFixedIpsByNetworkType(VXLAN); |
| 841 | Set<IpPrefix> vlanIps = osNetworkService.getFixedIpsByNetworkType(VLAN); |
| 842 | Set<IpPrefix> flatIps = osNetworkService.getFixedIpsByNetworkType(FLAT); |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 843 | |
Jian Li | e6110b7 | 2018-07-06 19:06:36 +0900 | [diff] [blame] | 844 | int fromTable, toTable; |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 845 | |
Jian Li | e6110b7 | 2018-07-06 19:06:36 +0900 | [diff] [blame] | 846 | 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 Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 870 | } |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | /** |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 874 | * Gets Device ID which the VM is located. |
Jian Li | 85573f4 | 2018-06-27 22:29:14 +0900 | [diff] [blame] | 875 | * |
| 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 Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 884 | log.debug("No DeviceID is associated to {}", ipAddress.toString()); |
Jian Li | 85573f4 | 2018-06-27 22:29:14 +0900 | [diff] [blame] | 885 | return null; |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 886 | } |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | /** |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 890 | * Gets VLAN ID with respect to IP Address. |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 891 | * |
| 892 | * @param ipAddress IP Address of host |
| 893 | * @return VLAN ID |
| 894 | */ |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 895 | 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 Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 899 | } |
| 900 | return VlanId.vlanId(); |
| 901 | } |
| 902 | |
| 903 | /** |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 904 | * Gets Interface ID of Switch which is connected to a host. |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 905 | * |
| 906 | * @param ipAddress IP Address of host |
| 907 | * @return Interface ID of Switch |
| 908 | */ |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 909 | 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 Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 913 | } |
| 914 | return -1; |
| 915 | } |
| 916 | |
| 917 | /** |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 918 | * Gets MAC Address of host. |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 919 | * |
| 920 | * @param ipAddress IP Address of host |
| 921 | * @return MAC Address of host |
| 922 | */ |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 923 | 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 Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 927 | } |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 928 | |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 929 | return NO_HOST_MAC; |
| 930 | } |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 931 | |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 932 | /** |
| 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 Jeong | 1cca5e8 | 2018-08-01 21:00:08 +0900 | [diff] [blame] | 952 | 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 Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 968 | /** |
| 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 Jeong | 1cca5e8 | 2018-08-01 21:00:08 +0900 | [diff] [blame] | 977 | } |
| 978 | |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 979 | /** |
| 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 Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 988 | getBooleanProperty(properties, PROP_REVERSE_PATH_STATS); |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 989 | if (reversePathStatsConfigured == null) { |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 990 | reversePathStats = PROP_REVERSE_PATH_STATS_DEFAULT; |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 991 | 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 Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 998 | Boolean egressStatsConfigured = getBooleanProperty(properties, PROP_EGRESS_STATS); |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 999 | if (egressStatsConfigured == null) { |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 1000 | egressStats = PROP_EGRESS_STATS_DEFAULT; |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 1001 | 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 Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 1007 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 1008 | Boolean portStatsConfigured = getBooleanProperty(properties, PROP_PORT_STATS); |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 1009 | if (portStatsConfigured == null) { |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 1010 | portStats = PROP_PORT_STATS_DEFAULT; |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 1011 | 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 Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 1017 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 1018 | Boolean monitorOverlayConfigured = getBooleanProperty(properties, PROP_MONITOR_OVERLAY); |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 1019 | if (monitorOverlayConfigured == null) { |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 1020 | monitorOverlay = PROP_MONITOR_OVERLAY_DEFAULT; |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 1021 | 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 Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 1028 | Boolean monitorUnderlayConfigured = getBooleanProperty(properties, PROP_MONITOR_UNDERLAY); |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 1029 | if (monitorUnderlayConfigured == null) { |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 1030 | monitorUnderlay = PROP_MONITOR_UNDERLAY_DEFAULT; |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 1031 | 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 Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 1037 | } |
| 1038 | |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 1039 | private class TelemetryCollector implements Runnable { |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 1040 | @Override |
| 1041 | public void run() { |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 1042 | Set<FlowInfo> filteredOverlayFlowInfos = Sets.newConcurrentHashSet(); |
| 1043 | Set<FlowInfo> filteredUnderlayFlowInfos = Sets.newConcurrentHashSet(); |
Jian Li | 85573f4 | 2018-06-27 22:29:14 +0900 | [diff] [blame] | 1044 | |
| 1045 | // we only let the master controller of the device where the |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 1046 | // stats flow rules are installed send stats message |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 1047 | if (monitorOverlay) { |
| 1048 | getOverlayFlowInfos().forEach(f -> { |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 1049 | if (checkSrcDstLocalMaster(f)) { |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 1050 | filteredOverlayFlowInfos.add(f); |
| 1051 | } |
| 1052 | }); |
| 1053 | } |
| 1054 | if (monitorUnderlay) { |
| 1055 | getUnderlayFlowInfos().forEach(f -> { |
| 1056 | if (checkSrcDstLocalMaster(f)) { |
| 1057 | filteredUnderlayFlowInfos.add(f); |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 1058 | } |
| 1059 | }); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 1060 | } |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 1061 | |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 1062 | // 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 Jeong | 1cca5e8 | 2018-08-01 21:00:08 +0900 | [diff] [blame] | 1072 | |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 1073 | 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 Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 1093 | } |
| 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 Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 1105 | } |
| 1106 | } |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 1107 | } |