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 | |
boyoung2 | a8549d2 | 2018-11-23 20:42:37 +0900 | [diff] [blame^] | 141 | private static final byte FLOW_TYPE_SONA = 1; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 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"; |
| 151 | private static final int ARBITRARY_LENGTH = 32; |
| 152 | private static final String ARBITRARY_MAC = "00:00:00:00:00:00"; |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 153 | 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] | 154 | private static final MacAddress NO_HOST_MAC = MacAddress.valueOf(ARBITRARY_MAC); |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 155 | private static final int ARBITRARY_IN_INTF = 0; |
| 156 | private static final int ARBITRARY_OUT_INTF = 0; |
| 157 | |
| 158 | private static final boolean RECOVER_FROM_FAILURE = true; |
| 159 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 160 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 161 | protected CoreService coreService; |
| 162 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 163 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 164 | protected FlowRuleService flowRuleService; |
| 165 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 166 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 167 | protected HostService hostService; |
| 168 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 169 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 170 | protected DeviceService deviceService; |
| 171 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 172 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 173 | protected DriverService driverService; |
| 174 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 175 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | e6110b7 | 2018-07-06 19:06:36 +0900 | [diff] [blame] | 176 | protected ComponentConfigService componentConfigService; |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 177 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 178 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | 85573f4 | 2018-06-27 22:29:14 +0900 | [diff] [blame] | 179 | protected MastershipService mastershipService; |
| 180 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 181 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 182 | protected OpenstackNetworkService osNetworkService; |
| 183 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 184 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 185 | protected InstancePortService instPortService; |
| 186 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 187 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 188 | protected OpenstackNodeService osNodeService; |
| 189 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 190 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 191 | protected OpenstackTelemetryService telemetryService; |
| 192 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 193 | /** A flag which indicates whether to install the rules for collecting the flow-based stats for reversed path. */ |
| 194 | private boolean reversePathStats = PROP_REVERSE_PATH_STATS_DEFAULT; |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 195 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 196 | /** A flag which indicates whether to install the rules for collecting the flow-based stats for egress port. */ |
| 197 | private boolean egressStats = PROP_EGRESS_STATS_DEFAULT; |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 198 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 199 | /** A flag which indicates whether to collect port TX & RX stats. */ |
| 200 | private boolean portStats = PROP_PORT_STATS_DEFAULT; |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 201 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 202 | /** A flag which indicates whether to monitor overlay network port stats. */ |
| 203 | private boolean monitorOverlay = PROP_MONITOR_OVERLAY_DEFAULT; |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 204 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 205 | /** A flag which indicates whether to monitor underlay network port stats. */ |
| 206 | private boolean monitorUnderlay = PROP_MONITOR_UNDERLAY_DEFAULT; |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 207 | |
| 208 | private ApplicationId telemetryAppId; |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 209 | private TelemetryCollector collector; |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 210 | private ScheduledFuture result; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 211 | |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 212 | private final Set<FlowInfo> gFlowInfoSet = Sets.newHashSet(); |
Boyoung Jeong | 1cca5e8 | 2018-08-01 21:00:08 +0900 | [diff] [blame] | 213 | private final Map<String, Queue<FlowInfo>> flowInfoMap = Maps.newConcurrentMap(); |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 214 | |
| 215 | private static final int SOURCE_ID = 1; |
| 216 | private static final int TARGET_ID = 2; |
| 217 | private static final int PRIORITY_BASE = 10000; |
| 218 | private static final int METRIC_PRIORITY_SOURCE = SOURCE_ID * PRIORITY_BASE; |
| 219 | private static final int METRIC_PRIORITY_TARGET = TARGET_ID * PRIORITY_BASE; |
| 220 | |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 221 | @Activate |
| 222 | protected void activate() { |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 223 | telemetryAppId = coreService.registerApplication(OPENSTACK_TELEMETRY_APP_ID); |
| 224 | |
Jian Li | e6110b7 | 2018-07-06 19:06:36 +0900 | [diff] [blame] | 225 | componentConfigService.registerProperties(getClass()); |
Jian Li | bd295cd | 2018-07-22 11:53:57 +0900 | [diff] [blame] | 226 | start(); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 227 | |
| 228 | log.info("Started"); |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | @Deactivate |
| 232 | protected void deactivate() { |
Jian Li | e6110b7 | 2018-07-06 19:06:36 +0900 | [diff] [blame] | 233 | componentConfigService.unregisterProperties(getClass(), false); |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 234 | flowRuleService.removeFlowRulesById(telemetryAppId); |
Jian Li | bd295cd | 2018-07-22 11:53:57 +0900 | [diff] [blame] | 235 | stop(); |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 236 | |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 237 | log.info("Stopped"); |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 238 | } |
| 239 | |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 240 | @Modified |
| 241 | protected void modified(ComponentContext context) { |
| 242 | readComponentConfiguration(context); |
| 243 | |
| 244 | log.info("Modified"); |
| 245 | } |
| 246 | |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 247 | @Override |
| 248 | public void start() { |
| 249 | log.info("Start publishing thread"); |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 250 | collector = new TelemetryCollector(); |
| 251 | |
Jian Li | bd295cd | 2018-07-22 11:53:57 +0900 | [diff] [blame] | 252 | result = SharedScheduledExecutors.getSingleThreadExecutor() |
| 253 | .scheduleAtFixedRate(collector, INITIAL_DELAY, |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 254 | REFRESH_INTERVAL, TIME_UNIT_SECOND, RECOVER_FROM_FAILURE); |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | @Override |
| 258 | public void stop() { |
| 259 | log.info("Stop data publishing thread"); |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 260 | result.cancel(true); |
| 261 | collector = null; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 262 | } |
| 263 | |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 264 | @Override |
boyoung2 | 7b44412 | 2018-09-01 17:28:13 +0900 | [diff] [blame] | 265 | public void createStatFlowRule(StatsFlowRule statsFlowRule) { |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 266 | setStatFlowRule(statsFlowRule, true); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | @Override |
| 270 | public void deleteStatFlowRule(StatsFlowRule statsFlowRule) { |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 271 | setStatFlowRule(statsFlowRule, false); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 272 | } |
| 273 | |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 274 | |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 275 | @Override |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 276 | public Map<String, Queue<FlowInfo>> getFlowInfoMap() { |
| 277 | return flowInfoMap; |
| 278 | } |
| 279 | |
| 280 | |
| 281 | @Override |
| 282 | public Set<FlowInfo> getUnderlayFlowInfos() { |
| 283 | |
| 284 | Set<FlowInfo> flowInfos = Sets.newConcurrentHashSet(); |
| 285 | |
| 286 | for (Device device : getUnderlayDevices()) { |
| 287 | |
| 288 | if (!isEdgeSwitch(device.id())) { |
| 289 | continue; |
| 290 | } |
| 291 | |
| 292 | for (FlowEntry entry : flowRuleService.getFlowEntries(device.id())) { |
| 293 | FlowInfo.Builder fBuilder = new DefaultFlowInfo.DefaultBuilder(); |
| 294 | TrafficSelector selector = entry.selector(); |
| 295 | Criterion inPort = selector.getCriterion(Criterion.Type.IN_PORT); |
| 296 | Criterion dstIpCriterion = selector.getCriterion(Criterion.Type.IPV4_DST); |
| 297 | if (inPort != null && dstIpCriterion != null) { |
| 298 | IpAddress srcIp = getIpAddress(device, (PortCriterion) inPort); |
| 299 | IpAddress dstIp = ((IPCriterion) dstIpCriterion).ip().address(); |
| 300 | |
| 301 | if (srcIp == null) { |
| 302 | continue; |
| 303 | } |
| 304 | |
| 305 | fBuilder.withFlowType(FLOW_TYPE_SONA) |
| 306 | .withSrcIp(IpPrefix.valueOf(srcIp, ARBITRARY_LENGTH)) |
| 307 | .withDstIp(IpPrefix.valueOf(dstIp, ARBITRARY_LENGTH)) |
| 308 | .withSrcMac(getMacAddress(srcIp)) |
| 309 | .withDstMac(getMacAddress(dstIp)) |
| 310 | .withInputInterfaceId(getInterfaceId(srcIp)) |
| 311 | .withOutputInterfaceId(getInterfaceId(dstIp)) |
| 312 | .withDeviceId(entry.deviceId()); |
| 313 | |
| 314 | StatsInfo.Builder sBuilder = new DefaultStatsInfo.DefaultBuilder(); |
| 315 | |
| 316 | sBuilder.withStartupTime(System.currentTimeMillis()) |
| 317 | .withFstPktArrTime(System.currentTimeMillis()) |
| 318 | .withLstPktOffset((int) (REFRESH_INTERVAL * MILLISECONDS)) |
| 319 | .withCurrAccPkts((int) entry.packets()) |
| 320 | .withCurrAccBytes(entry.bytes()) |
| 321 | .withErrorPkts((short) 0) |
| 322 | .withDropPkts((short) 0); |
| 323 | |
| 324 | fBuilder.withStatsInfo(sBuilder.build()); |
| 325 | |
| 326 | FlowInfo flowInfo = mergeFlowInfo(fBuilder.build(), fBuilder, sBuilder); |
| 327 | |
| 328 | flowInfos.add(flowInfo); |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | return flowInfos; |
| 334 | } |
| 335 | |
| 336 | @Override |
| 337 | public Set<FlowInfo> getOverlayFlowInfos() { |
| 338 | |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 339 | Set<FlowInfo> flowInfos = Sets.newConcurrentHashSet(); |
| 340 | |
| 341 | // obtain all flow rule entries installed by telemetry app |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 342 | for (FlowEntry entry : flowRuleService.getFlowEntriesById(telemetryAppId)) { |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 343 | FlowInfo.Builder fBuilder = new DefaultFlowInfo.DefaultBuilder(); |
| 344 | TrafficSelector selector = entry.selector(); |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 345 | IPCriterion srcIp = (IPCriterion) selector.getCriterion(IPV4_SRC); |
| 346 | IPCriterion dstIp = (IPCriterion) selector.getCriterion(IPV4_DST); |
| 347 | IPProtocolCriterion ipProtocol = |
| 348 | (IPProtocolCriterion) selector.getCriterion(IP_PROTO); |
| 349 | |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 350 | fBuilder.withFlowType(FLOW_TYPE_SONA) |
| 351 | .withSrcIp(srcIp.ip()) |
| 352 | .withDstIp(dstIp.ip()); |
| 353 | |
| 354 | if (ipProtocol != null) { |
| 355 | fBuilder.withProtocol((byte) ipProtocol.protocol()); |
| 356 | |
| 357 | if (ipProtocol.protocol() == PROTOCOL_TCP) { |
| 358 | TcpPortCriterion tcpSrc = |
| 359 | (TcpPortCriterion) selector.getCriterion(TCP_SRC); |
| 360 | TcpPortCriterion tcpDst = |
| 361 | (TcpPortCriterion) selector.getCriterion(TCP_DST); |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 362 | fBuilder.withSrcPort(tcpSrc.tcpPort()); |
| 363 | fBuilder.withDstPort(tcpDst.tcpPort()); |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 364 | } else if (ipProtocol.protocol() == PROTOCOL_UDP) { |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 365 | UdpPortCriterion udpSrc = |
| 366 | (UdpPortCriterion) selector.getCriterion(UDP_SRC); |
| 367 | UdpPortCriterion udpDst = |
| 368 | (UdpPortCriterion) selector.getCriterion(UDP_DST); |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 369 | fBuilder.withSrcPort(udpSrc.udpPort()); |
| 370 | fBuilder.withDstPort(udpDst.udpPort()); |
| 371 | } else { |
| 372 | log.debug("Other protocol: {}", ipProtocol.protocol()); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | fBuilder.withSrcMac(getMacAddress(srcIp.ip().address())) |
| 377 | .withDstMac(getMacAddress(dstIp.ip().address())) |
| 378 | .withInputInterfaceId(getInterfaceId(srcIp.ip().address())) |
| 379 | .withOutputInterfaceId(getInterfaceId(dstIp.ip().address())) |
| 380 | .withVlanId(getVlanId(srcIp.ip().address())) |
| 381 | .withDeviceId(entry.deviceId()); |
| 382 | |
| 383 | StatsInfo.Builder sBuilder = new DefaultStatsInfo.DefaultBuilder(); |
| 384 | |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 385 | sBuilder.withStartupTime(System.currentTimeMillis()) |
| 386 | .withFstPktArrTime(System.currentTimeMillis()) |
| 387 | .withLstPktOffset((int) (REFRESH_INTERVAL * MILLISECONDS)) |
| 388 | .withCurrAccPkts((int) entry.packets()) |
| 389 | .withCurrAccBytes(entry.bytes()) |
| 390 | .withErrorPkts((short) 0) |
| 391 | .withDropPkts((short) 0); |
| 392 | |
| 393 | fBuilder.withStatsInfo(sBuilder.build()); |
| 394 | |
| 395 | FlowInfo flowInfo = mergeFlowInfo(fBuilder.build(), fBuilder, sBuilder); |
| 396 | |
| 397 | flowInfos.add(flowInfo); |
| 398 | |
| 399 | log.debug("FlowInfo: \n{}", flowInfo.toString()); |
| 400 | } |
| 401 | |
| 402 | return flowInfos; |
| 403 | } |
| 404 | |
| 405 | /** |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 406 | * 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] | 407 | * |
| 408 | * @return flow infos |
| 409 | */ |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 410 | private Set<FlowInfo> getOverlayDstPortBasedFlowInfos() { |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 411 | Set<FlowInfo> flowInfos = Sets.newConcurrentHashSet(); |
| 412 | Set<PortNumber> instPortNums = instPortService.instancePorts() |
| 413 | .stream() |
| 414 | .map(InstancePort::portNumber) |
| 415 | .collect(Collectors.toSet()); |
| 416 | Set<DeviceId> deviceIds = osNodeService.completeNodes(COMPUTE) |
| 417 | .stream() |
| 418 | .map(OpenstackNode::intgBridge) |
| 419 | .collect(Collectors.toSet()); |
| 420 | |
| 421 | deviceIds.forEach(d -> { |
| 422 | List<PortStatistics> stats = |
| 423 | deviceService.getPortStatistics(d) |
| 424 | .stream() |
| 425 | .filter(s -> instPortNums.contains(s.portNumber())) |
| 426 | .collect(Collectors.toList()); |
| 427 | |
| 428 | stats.forEach(s -> { |
| 429 | InstancePort instPort = getInstancePort(d, s.portNumber()); |
boyoung2 | a8549d2 | 2018-11-23 20:42:37 +0900 | [diff] [blame^] | 430 | if (instPort != null) { |
| 431 | flowInfos.add(buildTxFlowInfoFromInstancePort(instPort, s)); |
| 432 | flowInfos.add(buildRxFlowInfoFromInstancePort(instPort, s)); |
| 433 | } |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 434 | }); |
| 435 | }); |
| 436 | |
| 437 | return flowInfos; |
| 438 | } |
| 439 | |
| 440 | /** |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 441 | * Gets a set of flow infos by referring to underlay destination port. |
| 442 | * |
| 443 | * @return flow infos |
| 444 | */ |
| 445 | private Set<FlowInfo> getUnderlayDstPortBasedFlowInfos() { |
| 446 | Set<FlowInfo> flowInfos = Sets.newConcurrentHashSet(); |
| 447 | |
| 448 | for (Device d : getUnderlayDevices()) { |
| 449 | List<PortStatistics> stats = |
| 450 | new ArrayList<>(deviceService.getPortStatistics(d.id())); |
| 451 | stats.forEach(s -> { |
| 452 | Host host = hostService.getConnectedHosts(new ConnectPoint(d.id(), s.portNumber())) |
| 453 | .stream().findFirst().orElse(null); |
| 454 | if (host != null) { |
| 455 | flowInfos.add(buildTxFlowInfoFromHost(host, s)); |
| 456 | flowInfos.add(buildRxFlowInfoFromHost(host, s)); |
| 457 | } |
| 458 | }); |
| 459 | } |
| 460 | |
| 461 | return flowInfos; |
| 462 | } |
| 463 | |
| 464 | /** |
| 465 | * Obtains a set of device instances which construct underlay network. |
| 466 | * |
| 467 | * @return a set of device instances |
| 468 | */ |
| 469 | private Set<Device> getUnderlayDevices() { |
| 470 | |
| 471 | Set<Device> underlayDevices = Sets.newConcurrentHashSet(); |
| 472 | |
| 473 | Set<DeviceId> overlayDeviceIds = osNodeService.completeNodes() |
| 474 | .stream() |
| 475 | .filter(n -> n.type() != CONTROLLER) |
| 476 | .map(OpenstackNode::intgBridge) |
| 477 | .collect(Collectors.toSet()); |
| 478 | |
| 479 | for (Device d : deviceService.getAvailableDevices(SWITCH)) { |
| 480 | if (overlayDeviceIds.contains(d.id())) { |
| 481 | continue; |
| 482 | } |
| 483 | |
| 484 | underlayDevices.add(d); |
| 485 | } |
| 486 | |
| 487 | return underlayDevices; |
| 488 | } |
| 489 | |
| 490 | /** |
| 491 | * Checks whether the given drivers contains OVS driver. |
| 492 | * |
| 493 | * @param drivers a set of drivers |
| 494 | * @return true if the given drivers contain any OVS driver, false otherwise |
| 495 | */ |
| 496 | private boolean hasOvsDriver(List<Driver> drivers) { |
| 497 | |
| 498 | for (Driver driver : drivers) { |
| 499 | if (OVS_DRIVER_NAME.equals(driver.name())) { |
| 500 | return true; |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | return false; |
| 505 | } |
| 506 | |
| 507 | /** |
| 508 | * Obtains the flow info generated by TX port from instance port. |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 509 | * |
| 510 | * @param instPort instance port |
| 511 | * @param stat port statistics |
| 512 | * @return flow info |
| 513 | */ |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 514 | private FlowInfo buildTxFlowInfoFromInstancePort(InstancePort instPort, |
| 515 | PortStatistics stat) { |
| 516 | return buildTxFlowInfo(instPort.ipAddress(), instPort.macAddress(), |
| 517 | instPort.deviceId(), stat); |
| 518 | } |
| 519 | |
| 520 | /** |
| 521 | * Obtains the flow info generated from RX port from instance port. |
| 522 | * |
| 523 | * @param instPort instance port |
| 524 | * @param stat port statistics |
| 525 | * @return flow info |
| 526 | */ |
| 527 | private FlowInfo buildRxFlowInfoFromInstancePort(InstancePort instPort, |
| 528 | PortStatistics stat) { |
| 529 | return buildRxFlowInfo(instPort.ipAddress(), instPort.macAddress(), |
| 530 | instPort.deviceId(), stat); |
| 531 | } |
| 532 | |
| 533 | /** |
| 534 | * Obtains the flow info generated by TX port from host. |
| 535 | * |
| 536 | * @param host host |
| 537 | * @param stat port statistics |
| 538 | * @return flow info |
| 539 | */ |
| 540 | private FlowInfo buildTxFlowInfoFromHost(Host host, PortStatistics stat) { |
| 541 | IpAddress ip = host.ipAddresses().stream().findFirst().orElse(null); |
| 542 | |
| 543 | if (ip != null) { |
| 544 | return buildTxFlowInfo(ip, host.mac(), host.location().deviceId(), stat); |
| 545 | } |
| 546 | return null; |
| 547 | } |
| 548 | |
| 549 | /** |
| 550 | * Obtains the flow info generated by RX @param host host. |
| 551 | * |
| 552 | * @param host host |
| 553 | * @param stat port statistics |
| 554 | * @return flow info |
| 555 | */ |
| 556 | private FlowInfo buildRxFlowInfoFromHost(Host host, PortStatistics stat) { |
| 557 | IpAddress ip = host.ipAddresses().stream().findFirst().orElse(null); |
| 558 | |
| 559 | if (ip != null) { |
| 560 | return buildRxFlowInfo(ip, host.mac(), host.location().deviceId(), stat); |
| 561 | } |
| 562 | return null; |
| 563 | } |
| 564 | |
| 565 | /** |
| 566 | * Obtains the flow info generated from TX port. |
| 567 | * |
| 568 | * @param ipAddress IP address |
| 569 | * @param macAddress MAC address |
| 570 | * @param deviceId device identifier |
| 571 | * @param stat port statistics |
| 572 | * @return flow info |
| 573 | */ |
| 574 | private FlowInfo buildTxFlowInfo(IpAddress ipAddress, |
| 575 | MacAddress macAddress, |
| 576 | DeviceId deviceId, |
| 577 | PortStatistics stat) { |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 578 | FlowInfo.Builder fBuilder = new DefaultFlowInfo.DefaultBuilder(); |
| 579 | |
| 580 | fBuilder.withFlowType(FLOW_TYPE_SONA) |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 581 | .withSrcIp(IpPrefix.valueOf(ipAddress, ARBITRARY_LENGTH)) |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 582 | .withDstIp(IpPrefix.valueOf(ARBITRARY_IP)) |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 583 | .withSrcMac(macAddress) |
Boyoung Jeong | 1cca5e8 | 2018-08-01 21:00:08 +0900 | [diff] [blame] | 584 | .withDstMac(NO_HOST_MAC) |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 585 | .withDeviceId(deviceId) |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 586 | .withInputInterfaceId(ARBITRARY_IN_INTF) |
| 587 | .withOutputInterfaceId(ARBITRARY_OUT_INTF) |
| 588 | .withVlanId(VlanId.vlanId()); |
| 589 | |
| 590 | StatsInfo.Builder sBuilder = new DefaultStatsInfo.DefaultBuilder(); |
| 591 | sBuilder.withStartupTime(System.currentTimeMillis()) |
| 592 | .withFstPktArrTime(System.currentTimeMillis()) |
| 593 | .withLstPktOffset((int) (REFRESH_INTERVAL * MILLISECONDS)) |
| 594 | .withCurrAccPkts((int) stat.packetsSent()) |
| 595 | .withCurrAccBytes(stat.bytesSent()) |
| 596 | .withErrorPkts((short) stat.packetsTxErrors()) |
| 597 | .withDropPkts((short) stat.packetsTxDropped()); |
| 598 | |
| 599 | fBuilder.withStatsInfo(sBuilder.build()); |
| 600 | |
| 601 | return mergeFlowInfo(fBuilder.build(), fBuilder, sBuilder); |
| 602 | } |
| 603 | |
| 604 | /** |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 605 | * Obtains the flow info generated from RX port. |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 606 | * |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 607 | * @param ipAddress IP address |
| 608 | * @param macAddress MAC address |
| 609 | * @param deviceId Device identifier |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 610 | * @param stat port statistics |
| 611 | * @return flow info |
| 612 | */ |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 613 | private FlowInfo buildRxFlowInfo(IpAddress ipAddress, |
| 614 | MacAddress macAddress, |
| 615 | DeviceId deviceId, |
| 616 | PortStatistics stat) { |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 617 | FlowInfo.Builder fBuilder = new DefaultFlowInfo.DefaultBuilder(); |
| 618 | |
| 619 | fBuilder.withFlowType(FLOW_TYPE_SONA) |
| 620 | .withSrcIp(IpPrefix.valueOf(ARBITRARY_IP)) |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 621 | .withDstIp(IpPrefix.valueOf(ipAddress, ARBITRARY_LENGTH)) |
Boyoung Jeong | 1cca5e8 | 2018-08-01 21:00:08 +0900 | [diff] [blame] | 622 | .withSrcMac(NO_HOST_MAC) |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 623 | .withDstMac(macAddress) |
| 624 | .withDeviceId(deviceId) |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 625 | .withInputInterfaceId(ARBITRARY_IN_INTF) |
| 626 | .withOutputInterfaceId(ARBITRARY_OUT_INTF) |
| 627 | .withVlanId(VlanId.vlanId()); |
| 628 | |
| 629 | StatsInfo.Builder sBuilder = new DefaultStatsInfo.DefaultBuilder(); |
| 630 | sBuilder.withStartupTime(System.currentTimeMillis()) |
| 631 | .withFstPktArrTime(System.currentTimeMillis()) |
| 632 | .withLstPktOffset((int) (REFRESH_INTERVAL * MILLISECONDS)) |
| 633 | .withCurrAccPkts((int) stat.packetsReceived()) |
| 634 | .withCurrAccBytes(stat.bytesReceived()) |
| 635 | .withErrorPkts((short) stat.packetsRxErrors()) |
| 636 | .withDropPkts((short) stat.packetsRxDropped()); |
| 637 | |
| 638 | fBuilder.withStatsInfo(sBuilder.build()); |
| 639 | |
| 640 | return mergeFlowInfo(fBuilder.build(), fBuilder, sBuilder); |
| 641 | } |
| 642 | |
| 643 | /** |
| 644 | * Obtains instance port which associated with the given device identifier |
| 645 | * and port number. |
| 646 | * |
| 647 | * @param deviceId device identifier |
| 648 | * @param portNumber port number |
| 649 | * @return instance port |
| 650 | */ |
| 651 | private InstancePort getInstancePort(DeviceId deviceId, PortNumber portNumber) { |
| 652 | return instPortService.instancePorts().stream() |
| 653 | .filter(p -> p.deviceId().equals(deviceId)) |
| 654 | .filter(p -> p.portNumber().equals(portNumber)) |
| 655 | .findFirst().orElse(null); |
| 656 | } |
| 657 | |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 658 | /** |
| 659 | * Installs a flow rule where the source table is fromTable, while destination |
| 660 | * table is toTable. |
| 661 | * |
| 662 | * @param deviceId device identifier |
| 663 | * @param fromTable source table |
| 664 | * @param toTable destination table |
| 665 | * @param statsFlowRule stats flow rule |
| 666 | * @param rulePriority rule priority |
| 667 | * @param install installation flag |
| 668 | */ |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 669 | private void connectTables(DeviceId deviceId, int fromTable, int toTable, |
| 670 | StatsFlowRule statsFlowRule, int rulePriority, |
| 671 | boolean install) { |
| 672 | |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 673 | int srcPrefixLength = statsFlowRule.srcIpPrefix().prefixLength(); |
| 674 | int dstPrefixLength = statsFlowRule.dstIpPrefix().prefixLength(); |
| 675 | int prefixLength = rulePriority + srcPrefixLength + dstPrefixLength; |
| 676 | byte protocol = statsFlowRule.ipProtocol(); |
| 677 | |
| 678 | TrafficSelector.Builder selectorBuilder = |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 679 | DefaultTrafficSelector.builder() |
| 680 | .matchEthType(TYPE_IPV4) |
| 681 | .matchIPSrc(statsFlowRule.srcIpPrefix()) |
| 682 | .matchIPDst(statsFlowRule.dstIpPrefix()); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 683 | |
| 684 | if (protocol == PROTOCOL_TCP) { |
| 685 | selectorBuilder = selectorBuilder |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 686 | .matchIPProtocol(statsFlowRule.ipProtocol()) |
| 687 | .matchTcpSrc(statsFlowRule.srcTpPort()) |
| 688 | .matchTcpDst(statsFlowRule.dstTpPort()); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 689 | |
| 690 | } else if (protocol == PROTOCOL_UDP) { |
| 691 | selectorBuilder = selectorBuilder |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 692 | .matchIPProtocol(statsFlowRule.ipProtocol()) |
| 693 | .matchUdpSrc(statsFlowRule.srcTpPort()) |
| 694 | .matchUdpDst(statsFlowRule.dstTpPort()); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 695 | } else { |
| 696 | log.warn("Unsupported protocol {}", statsFlowRule.ipProtocol()); |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 697 | } |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 698 | |
| 699 | TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder(); |
| 700 | |
| 701 | treatmentBuilder.transition(toTable); |
| 702 | |
| 703 | FlowRule flowRule = DefaultFlowRule.builder() |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 704 | .forDevice(deviceId) |
| 705 | .withSelector(selectorBuilder.build()) |
| 706 | .withTreatment(treatmentBuilder.build()) |
| 707 | .withPriority(prefixLength) |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 708 | .fromApp(telemetryAppId) |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 709 | .makePermanent() |
| 710 | .forTable(fromTable) |
| 711 | .build(); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 712 | |
| 713 | applyRule(flowRule, install); |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | /** |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 717 | * Installs stats related flow rule to switch. |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 718 | * |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 719 | * @param flowRule flow rule |
| 720 | * @param install flag to install or not |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 721 | */ |
| 722 | private void applyRule(FlowRule flowRule, boolean install) { |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 723 | FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder(); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 724 | flowOpsBuilder = install ? |
| 725 | flowOpsBuilder.add(flowRule) : flowOpsBuilder.remove(flowRule); |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 726 | |
| 727 | flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() { |
| 728 | @Override |
| 729 | public void onSuccess(FlowRuleOperations ops) { |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 730 | log.debug("Install rules for telemetry stats: \n {}", |
| 731 | ops.toString()); |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | @Override |
| 735 | public void onError(FlowRuleOperations ops) { |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 736 | log.debug("Failed to install rules for telemetry stats: \n {}", |
| 737 | ops.toString()); |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 738 | } |
| 739 | })); |
| 740 | } |
| 741 | |
| 742 | /** |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 743 | * Merges old FlowInfo.StatsInfo and current FlowInfo.StatsInfo. |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 744 | * |
| 745 | * @param flowInfo current FlowInfo object |
| 746 | * @param fBuilder Builder for FlowInfo |
| 747 | * @param sBuilder Builder for StatsInfo |
| 748 | * @return Merged FlowInfo object |
| 749 | */ |
| 750 | private FlowInfo mergeFlowInfo(FlowInfo flowInfo, |
| 751 | FlowInfo.Builder fBuilder, |
| 752 | StatsInfo.Builder sBuilder) { |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 753 | for (FlowInfo gFlowInfo : gFlowInfoSet) { |
| 754 | log.debug("Old FlowInfo:\n{}", gFlowInfo.toString()); |
| 755 | if (gFlowInfo.roughEquals(flowInfo)) { |
| 756 | |
| 757 | // Get old StatsInfo object and merge the value to current object. |
| 758 | StatsInfo oldStatsInfo = gFlowInfo.statsInfo(); |
| 759 | sBuilder.withPrevAccPkts(oldStatsInfo.currAccPkts()); |
| 760 | sBuilder.withPrevAccBytes(oldStatsInfo.currAccBytes()); |
| 761 | FlowInfo newFlowInfo = fBuilder.withStatsInfo(sBuilder.build()) |
| 762 | .build(); |
| 763 | |
| 764 | gFlowInfoSet.remove(gFlowInfo); |
| 765 | gFlowInfoSet.add(newFlowInfo); |
Jian Li | 85573f4 | 2018-06-27 22:29:14 +0900 | [diff] [blame] | 766 | log.debug("Old FlowInfo found, Merge this {}", newFlowInfo.toString()); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 767 | return newFlowInfo; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 768 | } |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 769 | } |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 770 | |
| 771 | // No such record, then build the FlowInfo object and return this object. |
Jian Li | 85573f4 | 2018-06-27 22:29:14 +0900 | [diff] [blame] | 772 | log.debug("No FlowInfo found, add new FlowInfo {}", flowInfo.toString()); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 773 | FlowInfo newFlowInfo = fBuilder.withStatsInfo(sBuilder.build()).build(); |
| 774 | gFlowInfoSet.add(newFlowInfo); |
| 775 | return newFlowInfo; |
| 776 | } |
| 777 | |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 778 | /** |
| 779 | * Installs flow rules for collecting both normal and reverse path flow stats. |
| 780 | * |
| 781 | * @param statsFlowRule flow rule used for collecting stats |
| 782 | * @param install flow rule installation flag |
| 783 | */ |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 784 | private void setStatFlowRule(StatsFlowRule statsFlowRule, boolean install) { |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 785 | setStatFlowRuleBase(statsFlowRule, install); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 786 | |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 787 | // if reverse path stats is enabled, we will install flow rules for |
| 788 | // collecting reverse path vFlow stats |
| 789 | if (reversePathStats) { |
| 790 | StatsFlowRule reverseFlowRule = DefaultStatsFlowRule.builder() |
| 791 | .srcIpPrefix(statsFlowRule.dstIpPrefix()) |
| 792 | .dstIpPrefix(statsFlowRule.srcIpPrefix()) |
| 793 | .ipProtocol(statsFlowRule.ipProtocol()) |
| 794 | .srcTpPort(statsFlowRule.dstTpPort()) |
| 795 | .dstTpPort(statsFlowRule.srcTpPort()) |
| 796 | .build(); |
| 797 | setStatFlowRuleBase(reverseFlowRule, install); |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | /** |
| 802 | * A base method which is for installing flow rules for collecting stats. |
| 803 | * |
| 804 | * @param statsFlowRule flow rule used for collecting stats |
| 805 | * @param install flow rule installation flag |
| 806 | */ |
| 807 | private void setStatFlowRuleBase(StatsFlowRule statsFlowRule, boolean install) { |
Jian Li | e6110b7 | 2018-07-06 19:06:36 +0900 | [diff] [blame] | 808 | |
| 809 | IpPrefix srcIp = statsFlowRule.srcIpPrefix(); |
| 810 | IpPrefix dstIp = statsFlowRule.dstIpPrefix(); |
| 811 | DeviceId srcDeviceId = getDeviceId(srcIp.address()); |
| 812 | DeviceId dstDeviceId = getDeviceId(dstIp.address()); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 813 | |
Jian Li | 998ec7b | 2018-06-29 15:15:49 +0900 | [diff] [blame] | 814 | if (srcDeviceId == null && dstDeviceId == null) { |
Jian Li | 85573f4 | 2018-06-27 22:29:14 +0900 | [diff] [blame] | 815 | return; |
| 816 | } |
| 817 | |
Jian Li | 998ec7b | 2018-06-29 15:15:49 +0900 | [diff] [blame] | 818 | if (srcDeviceId != null) { |
Jian Li | 87ded82 | 2018-07-02 18:31:22 +0900 | [diff] [blame] | 819 | connectTables(srcDeviceId, STAT_INBOUND_TABLE, VTAP_INBOUND_TABLE, |
Jian Li | 998ec7b | 2018-06-29 15:15:49 +0900 | [diff] [blame] | 820 | statsFlowRule, METRIC_PRIORITY_SOURCE, install); |
Jian Li | 998ec7b | 2018-06-29 15:15:49 +0900 | [diff] [blame] | 821 | |
Jian Li | e6110b7 | 2018-07-06 19:06:36 +0900 | [diff] [blame] | 822 | if (install) { |
| 823 | log.info("Install ingress stat flow rule for SrcIp:{} DstIp:{}", |
| 824 | srcIp.toString(), dstIp.toString()); |
| 825 | } else { |
| 826 | log.info("Remove ingress stat flow rule for SrcIp:{} DstIp:{}", |
| 827 | srcIp.toString(), dstIp.toString()); |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 828 | } |
Jian Li | 998ec7b | 2018-06-29 15:15:49 +0900 | [diff] [blame] | 829 | } |
Jian Li | 85573f4 | 2018-06-27 22:29:14 +0900 | [diff] [blame] | 830 | |
Jian Li | e6110b7 | 2018-07-06 19:06:36 +0900 | [diff] [blame] | 831 | Set<IpPrefix> vxlanIps = osNetworkService.getFixedIpsByNetworkType(VXLAN); |
| 832 | Set<IpPrefix> vlanIps = osNetworkService.getFixedIpsByNetworkType(VLAN); |
| 833 | Set<IpPrefix> flatIps = osNetworkService.getFixedIpsByNetworkType(FLAT); |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 834 | |
Jian Li | e6110b7 | 2018-07-06 19:06:36 +0900 | [diff] [blame] | 835 | int fromTable, toTable; |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 836 | |
Jian Li | e6110b7 | 2018-07-06 19:06:36 +0900 | [diff] [blame] | 837 | if (dstDeviceId != null && egressStats) { |
| 838 | |
| 839 | IpPrefix dstIpPrefix = statsFlowRule.dstIpPrefix(); |
| 840 | |
| 841 | if (vxlanIps.contains(dstIpPrefix) || vlanIps.contains(dstIpPrefix)) { |
| 842 | fromTable = STAT_OUTBOUND_TABLE; |
| 843 | toTable = VTAP_OUTBOUND_TABLE; |
| 844 | } else if (flatIps.contains(dstIpPrefix)) { |
| 845 | fromTable = STAT_FLAT_OUTBOUND_TABLE; |
| 846 | toTable = VTAP_FLAT_OUTBOUND_TABLE; |
| 847 | } else { |
| 848 | return; |
| 849 | } |
| 850 | |
| 851 | connectTables(dstDeviceId, fromTable, toTable, |
| 852 | statsFlowRule, METRIC_PRIORITY_TARGET, install); |
| 853 | |
| 854 | if (install) { |
| 855 | log.info("Install egress stat flow rule for SrcIp:{} DstIp:{}", |
| 856 | srcIp.toString(), dstIp.toString()); |
| 857 | } else { |
| 858 | log.info("Remove egress stat flow rule for SrcIp:{} DstIp:{}", |
| 859 | srcIp.toString(), dstIp.toString()); |
| 860 | } |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 861 | } |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | /** |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 865 | * Gets Device ID which the VM is located. |
Jian Li | 85573f4 | 2018-06-27 22:29:14 +0900 | [diff] [blame] | 866 | * |
| 867 | * @param ipAddress IP Address of host |
| 868 | * @return Device ID |
| 869 | */ |
| 870 | private DeviceId getDeviceId(IpAddress ipAddress) { |
| 871 | if (!hostService.getHostsByIp(ipAddress).isEmpty()) { |
| 872 | Optional<Host> host = hostService.getHostsByIp(ipAddress).stream().findAny(); |
| 873 | return host.map(host1 -> host1.location().deviceId()).orElse(null); |
| 874 | } else { |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 875 | log.debug("No DeviceID is associated to {}", ipAddress.toString()); |
Jian Li | 85573f4 | 2018-06-27 22:29:14 +0900 | [diff] [blame] | 876 | return null; |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 877 | } |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | /** |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 881 | * Gets VLAN ID with respect to IP Address. |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 882 | * |
| 883 | * @param ipAddress IP Address of host |
| 884 | * @return VLAN ID |
| 885 | */ |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 886 | private VlanId getVlanId(IpAddress ipAddress) { |
| 887 | if (!hostService.getHostsByIp(ipAddress).isEmpty()) { |
| 888 | Host host = hostService.getHostsByIp(ipAddress).stream().findAny().get(); |
| 889 | return host.vlan(); |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 890 | } |
| 891 | return VlanId.vlanId(); |
| 892 | } |
| 893 | |
| 894 | /** |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 895 | * Gets Interface ID of Switch which is connected to a host. |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 896 | * |
| 897 | * @param ipAddress IP Address of host |
| 898 | * @return Interface ID of Switch |
| 899 | */ |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 900 | private int getInterfaceId(IpAddress ipAddress) { |
| 901 | if (!hostService.getHostsByIp(ipAddress).isEmpty()) { |
| 902 | Host host = hostService.getHostsByIp(ipAddress).stream().findAny().get(); |
| 903 | return (int) host.location().port().toLong(); |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 904 | } |
| 905 | return -1; |
| 906 | } |
| 907 | |
| 908 | /** |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 909 | * Gets MAC Address of host. |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 910 | * |
| 911 | * @param ipAddress IP Address of host |
| 912 | * @return MAC Address of host |
| 913 | */ |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 914 | private MacAddress getMacAddress(IpAddress ipAddress) { |
| 915 | if (!hostService.getHostsByIp(ipAddress).isEmpty()) { |
| 916 | Host host = hostService.getHostsByIp(ipAddress).stream().findAny().get(); |
| 917 | return host.mac(); |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 918 | } |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 919 | |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 920 | return NO_HOST_MAC; |
| 921 | } |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 922 | |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 923 | /** |
| 924 | * Gets IP address of the host which is attached to the given device and port. |
| 925 | * |
| 926 | * @param device device |
| 927 | * @param inPort IN port number |
| 928 | * @return IP address |
| 929 | */ |
| 930 | private IpAddress getIpAddress(Device device, PortCriterion inPort) { |
| 931 | |
| 932 | Host host = hostService.getConnectedHosts(device.id()).stream() |
| 933 | .filter(h -> h.location().port().equals(inPort.port())) |
| 934 | .findAny().orElse(null); |
| 935 | |
| 936 | if (host != null) { |
| 937 | return host.ipAddresses().stream().findAny().get(); |
| 938 | } |
| 939 | |
| 940 | return NO_HOST_IP; |
| 941 | } |
| 942 | |
Boyoung Jeong | 1cca5e8 | 2018-08-01 21:00:08 +0900 | [diff] [blame] | 943 | private void enqFlowInfo(FlowInfo flowInfo) { |
| 944 | String key = flowInfo.uniqueFlowInfoKey(); |
| 945 | Queue<FlowInfo> queue = flowInfoMap.get(key); |
| 946 | if (queue == null) { |
| 947 | Queue<FlowInfo> newQueue = new LinkedList<FlowInfo>(); |
| 948 | newQueue.offer(flowInfo); |
| 949 | flowInfoMap.put(key, newQueue); |
| 950 | return; |
| 951 | } |
| 952 | queue.offer(flowInfo); |
| 953 | |
| 954 | while (queue.size() > DEFAULT_DATA_POINT_SIZE) { |
| 955 | queue.remove(); // Removes a garbage data in the queue. |
| 956 | } |
| 957 | } |
| 958 | |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 959 | /** |
| 960 | * Checks whether the given device is edge switch or not. |
| 961 | * |
| 962 | * @param id device identifier |
| 963 | * @return true if the given device is edge switch, false otherwise |
| 964 | */ |
| 965 | private boolean isEdgeSwitch(DeviceId id) { |
| 966 | |
| 967 | return !hostService.getConnectedHosts(id).isEmpty(); |
Boyoung Jeong | 1cca5e8 | 2018-08-01 21:00:08 +0900 | [diff] [blame] | 968 | } |
| 969 | |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 970 | /** |
| 971 | * Extracts properties from the component configuration context. |
| 972 | * |
| 973 | * @param context the component context |
| 974 | */ |
| 975 | private void readComponentConfiguration(ComponentContext context) { |
| 976 | Dictionary<?, ?> properties = context.getProperties(); |
| 977 | |
| 978 | Boolean reversePathStatsConfigured = |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 979 | getBooleanProperty(properties, PROP_REVERSE_PATH_STATS); |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 980 | if (reversePathStatsConfigured == null) { |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 981 | reversePathStats = PROP_REVERSE_PATH_STATS_DEFAULT; |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 982 | log.info("Reversed path stats flag is NOT " + |
| 983 | "configured, default value is {}", reversePathStats); |
| 984 | } else { |
| 985 | reversePathStats = reversePathStatsConfigured; |
| 986 | log.info("Configured. Reversed path stats flag is {}", reversePathStats); |
| 987 | } |
| 988 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 989 | Boolean egressStatsConfigured = getBooleanProperty(properties, PROP_EGRESS_STATS); |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 990 | if (egressStatsConfigured == null) { |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 991 | egressStats = PROP_EGRESS_STATS_DEFAULT; |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 992 | log.info("Egress stats flag is NOT " + |
| 993 | "configured, default value is {}", egressStats); |
| 994 | } else { |
| 995 | egressStats = egressStatsConfigured; |
| 996 | log.info("Configured. Egress stats flag is {}", egressStats); |
| 997 | } |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 998 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 999 | Boolean portStatsConfigured = getBooleanProperty(properties, PROP_PORT_STATS); |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 1000 | if (portStatsConfigured == null) { |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 1001 | portStats = PROP_PORT_STATS_DEFAULT; |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 1002 | log.info("Port stats flag is NOT " + |
| 1003 | "configured, default value is {}", portStats); |
| 1004 | } else { |
| 1005 | portStats = portStatsConfigured; |
| 1006 | log.info("Configured. Port stats flag is {}", portStats); |
| 1007 | } |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 1008 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 1009 | Boolean monitorOverlayConfigured = getBooleanProperty(properties, PROP_MONITOR_OVERLAY); |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 1010 | if (monitorOverlayConfigured == null) { |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 1011 | monitorOverlay = PROP_MONITOR_OVERLAY_DEFAULT; |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 1012 | log.info("Monitor overlay flag is NOT " + |
| 1013 | "configured, default value is {}", monitorOverlay); |
| 1014 | } else { |
| 1015 | monitorOverlay = monitorOverlayConfigured; |
| 1016 | log.info("Configured. Monitor overlay flag is {}", monitorOverlay); |
| 1017 | } |
| 1018 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 1019 | Boolean monitorUnderlayConfigured = getBooleanProperty(properties, PROP_MONITOR_UNDERLAY); |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 1020 | if (monitorUnderlayConfigured == null) { |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 1021 | monitorUnderlay = PROP_MONITOR_UNDERLAY_DEFAULT; |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 1022 | log.info("Monitor underlay flag is NOT " + |
| 1023 | "configured, default value is {}", monitorUnderlay); |
| 1024 | } else { |
| 1025 | monitorUnderlay = monitorUnderlayConfigured; |
| 1026 | log.info("Configured. Monitor underlay flag is {}", monitorUnderlay); |
| 1027 | } |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 1028 | } |
| 1029 | |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 1030 | private class TelemetryCollector implements Runnable { |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 1031 | @Override |
| 1032 | public void run() { |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 1033 | Set<FlowInfo> filteredOverlayFlowInfos = Sets.newConcurrentHashSet(); |
| 1034 | Set<FlowInfo> filteredUnderlayFlowInfos = Sets.newConcurrentHashSet(); |
Jian Li | 85573f4 | 2018-06-27 22:29:14 +0900 | [diff] [blame] | 1035 | |
| 1036 | // we only let the master controller of the device where the |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 1037 | // stats flow rules are installed send stats message |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 1038 | if (monitorOverlay) { |
| 1039 | getOverlayFlowInfos().forEach(f -> { |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 1040 | if (checkSrcDstLocalMaster(f)) { |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 1041 | filteredOverlayFlowInfos.add(f); |
| 1042 | } |
| 1043 | }); |
| 1044 | } |
| 1045 | if (monitorUnderlay) { |
| 1046 | getUnderlayFlowInfos().forEach(f -> { |
| 1047 | if (checkSrcDstLocalMaster(f)) { |
| 1048 | filteredUnderlayFlowInfos.add(f); |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 1049 | } |
| 1050 | }); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 1051 | } |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 1052 | |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 1053 | // we only let the master controller of the device where the port |
| 1054 | // is located to send stats message |
| 1055 | if (portStats) { |
| 1056 | if (monitorOverlay) { |
| 1057 | getOverlayDstPortBasedFlowInfos().forEach(f -> { |
| 1058 | if (checkSrcDstLocalMaster(f)) { |
| 1059 | filteredOverlayFlowInfos.add(f); |
| 1060 | } |
| 1061 | }); |
| 1062 | } |
Boyoung Jeong | 1cca5e8 | 2018-08-01 21:00:08 +0900 | [diff] [blame] | 1063 | |
Jian Li | f8b8c7f | 2018-08-27 18:49:04 +0900 | [diff] [blame] | 1064 | if (monitorUnderlay) { |
| 1065 | getUnderlayDstPortBasedFlowInfos().forEach(f -> { |
| 1066 | if (checkSrcDstLocalMaster(f)) { |
| 1067 | filteredUnderlayFlowInfos.add(f); |
| 1068 | } |
| 1069 | }); |
| 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | |
| 1074 | if (monitorOverlay) { |
| 1075 | telemetryService.publish(filteredOverlayFlowInfos); |
| 1076 | |
| 1077 | // TODO: Refactor the following code to "TelemetryService" style. |
| 1078 | filteredOverlayFlowInfos.forEach(StatsFlowRuleManager.this::enqFlowInfo); |
| 1079 | } |
| 1080 | |
| 1081 | if (monitorUnderlay) { |
| 1082 | telemetryService.publish(filteredUnderlayFlowInfos); |
| 1083 | } |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | private boolean checkSrcDstLocalMaster(FlowInfo info) { |
| 1087 | DeviceId srcDeviceId = getDeviceId(info.srcIp().address()); |
| 1088 | DeviceId dstDeviceId = getDeviceId(info.dstIp().address()); |
| 1089 | |
| 1090 | boolean isSrcLocalMaster = srcDeviceId != null && |
| 1091 | mastershipService.isLocalMaster(srcDeviceId); |
| 1092 | boolean isDstLocalMaster = dstDeviceId != null && |
| 1093 | mastershipService.isLocalMaster(dstDeviceId); |
| 1094 | |
| 1095 | return isSrcLocalMaster || isDstLocalMaster; |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 1096 | } |
| 1097 | } |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 1098 | } |