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.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Modified; |
| 24 | import org.apache.felix.scr.annotations.Property; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 27 | import org.apache.felix.scr.annotations.Service; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 28 | import org.onlab.packet.IpAddress; |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 29 | import org.onlab.packet.IpPrefix; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 30 | import org.onlab.packet.MacAddress; |
| 31 | import org.onlab.packet.VlanId; |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 32 | import org.onlab.util.SharedScheduledExecutors; |
Jian Li | e6110b7 | 2018-07-06 19:06:36 +0900 | [diff] [blame] | 33 | import org.onosproject.cfg.ComponentConfigService; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 34 | import org.onosproject.core.ApplicationId; |
| 35 | import org.onosproject.core.CoreService; |
Jian Li | 85573f4 | 2018-06-27 22:29:14 +0900 | [diff] [blame] | 36 | import org.onosproject.mastership.MastershipService; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 37 | import org.onosproject.net.DeviceId; |
| 38 | import org.onosproject.net.Host; |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 39 | import org.onosproject.net.PortNumber; |
| 40 | import org.onosproject.net.device.DeviceService; |
| 41 | import org.onosproject.net.device.PortStatistics; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 42 | import org.onosproject.net.flow.DefaultFlowRule; |
| 43 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 44 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 45 | import org.onosproject.net.flow.FlowEntry; |
| 46 | import org.onosproject.net.flow.FlowRule; |
| 47 | import org.onosproject.net.flow.FlowRuleOperations; |
| 48 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
| 49 | import org.onosproject.net.flow.FlowRuleService; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 50 | import org.onosproject.net.flow.TrafficSelector; |
| 51 | import org.onosproject.net.flow.TrafficTreatment; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 52 | import org.onosproject.net.flow.criteria.IPCriterion; |
| 53 | import org.onosproject.net.flow.criteria.IPProtocolCriterion; |
| 54 | import org.onosproject.net.flow.criteria.TcpPortCriterion; |
| 55 | import org.onosproject.net.flow.criteria.UdpPortCriterion; |
| 56 | import org.onosproject.net.host.HostService; |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 57 | import org.onosproject.openstacknetworking.api.InstancePort; |
| 58 | import org.onosproject.openstacknetworking.api.InstancePortService; |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 59 | import org.onosproject.openstacknetworking.api.OpenstackNetworkService; |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 60 | import org.onosproject.openstacknode.api.OpenstackNode; |
| 61 | import org.onosproject.openstacknode.api.OpenstackNodeService; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 62 | import org.onosproject.openstacktelemetry.api.FlowInfo; |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 63 | import org.onosproject.openstacktelemetry.api.OpenstackTelemetryService; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 64 | import org.onosproject.openstacktelemetry.api.StatsFlowRule; |
| 65 | import org.onosproject.openstacktelemetry.api.StatsFlowRuleAdminService; |
| 66 | import org.onosproject.openstacktelemetry.api.StatsInfo; |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 67 | import org.osgi.service.component.ComponentContext; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 68 | import org.slf4j.Logger; |
| 69 | import org.slf4j.LoggerFactory; |
| 70 | |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 71 | import java.util.Dictionary; |
Boyoung Jeong | 1cca5e8 | 2018-08-01 21:00:08 +0900 | [diff] [blame] | 72 | import java.util.LinkedList; |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 73 | import java.util.List; |
Boyoung Jeong | 1cca5e8 | 2018-08-01 21:00:08 +0900 | [diff] [blame] | 74 | import java.util.Map; |
Jian Li | 85573f4 | 2018-06-27 22:29:14 +0900 | [diff] [blame] | 75 | import java.util.Optional; |
Boyoung Jeong | 1cca5e8 | 2018-08-01 21:00:08 +0900 | [diff] [blame] | 76 | import java.util.Queue; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 77 | import java.util.Set; |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 78 | import java.util.concurrent.ScheduledFuture; |
| 79 | import java.util.concurrent.TimeUnit; |
| 80 | import java.util.stream.Collectors; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 81 | |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 82 | import static org.onlab.packet.Ethernet.TYPE_IPV4; |
| 83 | import static org.onlab.packet.IPv4.PROTOCOL_TCP; |
| 84 | import static org.onlab.packet.IPv4.PROTOCOL_UDP; |
| 85 | import static org.onosproject.net.flow.criteria.Criterion.Type.IPV4_DST; |
| 86 | import static org.onosproject.net.flow.criteria.Criterion.Type.IPV4_SRC; |
| 87 | import static org.onosproject.net.flow.criteria.Criterion.Type.IP_PROTO; |
| 88 | import static org.onosproject.net.flow.criteria.Criterion.Type.TCP_DST; |
| 89 | import static org.onosproject.net.flow.criteria.Criterion.Type.TCP_SRC; |
| 90 | import static org.onosproject.net.flow.criteria.Criterion.Type.UDP_DST; |
| 91 | import static org.onosproject.net.flow.criteria.Criterion.Type.UDP_SRC; |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 92 | import static org.onosproject.openstacknetworking.api.Constants.STAT_FLAT_OUTBOUND_TABLE; |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 93 | import static org.onosproject.openstacknetworking.api.Constants.STAT_INBOUND_TABLE; |
| 94 | import static org.onosproject.openstacknetworking.api.Constants.STAT_OUTBOUND_TABLE; |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 95 | import static org.onosproject.openstacknetworking.api.Constants.VTAP_FLAT_OUTBOUND_TABLE; |
Jian Li | 87ded82 | 2018-07-02 18:31:22 +0900 | [diff] [blame] | 96 | import static org.onosproject.openstacknetworking.api.Constants.VTAP_INBOUND_TABLE; |
| 97 | import static org.onosproject.openstacknetworking.api.Constants.VTAP_OUTBOUND_TABLE; |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 98 | import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.COMPUTE; |
Boyoung Jeong | 1cca5e8 | 2018-08-01 21:00:08 +0900 | [diff] [blame] | 99 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_DATA_POINT_SIZE; |
Jian Li | e6110b7 | 2018-07-06 19:06:36 +0900 | [diff] [blame] | 100 | import static org.onosproject.openstacktelemetry.api.Constants.FLAT; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 101 | import static org.onosproject.openstacktelemetry.api.Constants.OPENSTACK_TELEMETRY_APP_ID; |
Jian Li | e6110b7 | 2018-07-06 19:06:36 +0900 | [diff] [blame] | 102 | import static org.onosproject.openstacktelemetry.api.Constants.VLAN; |
| 103 | import static org.onosproject.openstacktelemetry.api.Constants.VXLAN; |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 104 | import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.getBooleanProperty; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 105 | |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 106 | /** |
| 107 | * Flow rule manager for network statistics of a VM. |
| 108 | */ |
| 109 | @Component(immediate = true) |
| 110 | @Service |
| 111 | public class StatsFlowRuleManager implements StatsFlowRuleAdminService { |
| 112 | |
| 113 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 114 | |
| 115 | private static final byte FLOW_TYPE_SONA = 1; // VLAN |
| 116 | |
Ray Milkey | bcc53d3 | 2018-07-02 10:22:57 -0700 | [diff] [blame] | 117 | private static final long MILLISECONDS = 1000L; |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 118 | private static final long INITIAL_DELAY = 5L; |
Ray Milkey | bcc53d3 | 2018-07-02 10:22:57 -0700 | [diff] [blame] | 119 | private static final long REFRESH_INTERVAL = 5L; |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 120 | private static final TimeUnit TIME_UNIT_SECOND = TimeUnit.SECONDS; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 121 | |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 122 | private static final String REVERSE_PATH_STATS = "reversePathStats"; |
| 123 | private static final String EGRESS_STATS = "egressStats"; |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 124 | private static final String PORT_STATS = "portStats"; |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 125 | |
| 126 | private static final boolean DEFAULT_REVERSE_PATH_STATS = false; |
| 127 | private static final boolean DEFAULT_EGRESS_STATS = false; |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 128 | private static final boolean DEFAULT_PORT_STATS = true; |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 129 | |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 130 | private static final String ARBITRARY_IP = "0.0.0.0/32"; |
| 131 | private static final int ARBITRARY_LENGTH = 32; |
| 132 | private static final String ARBITRARY_MAC = "00:00:00:00:00:00"; |
Boyoung Jeong | 1cca5e8 | 2018-08-01 21:00:08 +0900 | [diff] [blame] | 133 | private static final MacAddress NO_HOST_MAC = MacAddress.valueOf(ARBITRARY_MAC); |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 134 | private static final int ARBITRARY_IN_INTF = 0; |
| 135 | private static final int ARBITRARY_OUT_INTF = 0; |
| 136 | |
| 137 | private static final boolean RECOVER_FROM_FAILURE = true; |
| 138 | |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 139 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 140 | protected CoreService coreService; |
| 141 | |
| 142 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 143 | protected FlowRuleService flowRuleService; |
| 144 | |
| 145 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 146 | protected HostService hostService; |
| 147 | |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 148 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 149 | protected DeviceService deviceService; |
| 150 | |
| 151 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jian Li | e6110b7 | 2018-07-06 19:06:36 +0900 | [diff] [blame] | 152 | protected ComponentConfigService componentConfigService; |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 153 | |
| 154 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jian Li | 85573f4 | 2018-06-27 22:29:14 +0900 | [diff] [blame] | 155 | protected MastershipService mastershipService; |
| 156 | |
| 157 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 158 | protected OpenstackNetworkService osNetworkService; |
| 159 | |
| 160 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 161 | protected InstancePortService instPortService; |
| 162 | |
| 163 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 164 | protected OpenstackNodeService osNodeService; |
| 165 | |
| 166 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 167 | protected OpenstackTelemetryService telemetryService; |
| 168 | |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 169 | @Property(name = REVERSE_PATH_STATS, boolValue = DEFAULT_REVERSE_PATH_STATS, |
| 170 | label = "A flag which indicates whether to install the rules for " + |
| 171 | "collecting the flow-based stats for reversed path.") |
| 172 | private boolean reversePathStats = DEFAULT_REVERSE_PATH_STATS; |
| 173 | |
| 174 | @Property(name = EGRESS_STATS, boolValue = DEFAULT_EGRESS_STATS, |
| 175 | label = "A flag which indicates whether to install the rules for " + |
| 176 | "collecting the flow-based stats for egress port.") |
| 177 | private boolean egressStats = DEFAULT_EGRESS_STATS; |
| 178 | |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 179 | @Property(name = PORT_STATS, boolValue = DEFAULT_PORT_STATS, |
| 180 | label = "A flag which indicates whether to collect port TX & RX stats.") |
| 181 | private boolean portStats = DEFAULT_PORT_STATS; |
| 182 | |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 183 | private ApplicationId appId; |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 184 | private TelemetryCollector collector; |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 185 | private ScheduledFuture result; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 186 | |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 187 | private final Set<FlowInfo> gFlowInfoSet = Sets.newHashSet(); |
Boyoung Jeong | 1cca5e8 | 2018-08-01 21:00:08 +0900 | [diff] [blame] | 188 | private final Map<String, Queue<FlowInfo>> flowInfoMap = Maps.newConcurrentMap(); |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 189 | |
| 190 | private static final int SOURCE_ID = 1; |
| 191 | private static final int TARGET_ID = 2; |
| 192 | private static final int PRIORITY_BASE = 10000; |
| 193 | private static final int METRIC_PRIORITY_SOURCE = SOURCE_ID * PRIORITY_BASE; |
| 194 | private static final int METRIC_PRIORITY_TARGET = TARGET_ID * PRIORITY_BASE; |
| 195 | |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 196 | @Activate |
| 197 | protected void activate() { |
| 198 | appId = coreService.registerApplication(OPENSTACK_TELEMETRY_APP_ID); |
Jian Li | e6110b7 | 2018-07-06 19:06:36 +0900 | [diff] [blame] | 199 | componentConfigService.registerProperties(getClass()); |
Jian Li | bd295cd | 2018-07-22 11:53:57 +0900 | [diff] [blame] | 200 | start(); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 201 | |
| 202 | log.info("Started"); |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | @Deactivate |
| 206 | protected void deactivate() { |
Jian Li | e6110b7 | 2018-07-06 19:06:36 +0900 | [diff] [blame] | 207 | componentConfigService.unregisterProperties(getClass(), false); |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 208 | flowRuleService.removeFlowRulesById(appId); |
Jian Li | bd295cd | 2018-07-22 11:53:57 +0900 | [diff] [blame] | 209 | stop(); |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 210 | |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 211 | log.info("Stopped"); |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 212 | } |
| 213 | |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 214 | @Modified |
| 215 | protected void modified(ComponentContext context) { |
| 216 | readComponentConfiguration(context); |
| 217 | |
| 218 | log.info("Modified"); |
| 219 | } |
| 220 | |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 221 | @Override |
| 222 | public void start() { |
| 223 | log.info("Start publishing thread"); |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 224 | collector = new TelemetryCollector(); |
| 225 | |
Jian Li | bd295cd | 2018-07-22 11:53:57 +0900 | [diff] [blame] | 226 | result = SharedScheduledExecutors.getSingleThreadExecutor() |
| 227 | .scheduleAtFixedRate(collector, INITIAL_DELAY, |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 228 | REFRESH_INTERVAL, TIME_UNIT_SECOND, RECOVER_FROM_FAILURE); |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | @Override |
| 232 | public void stop() { |
| 233 | log.info("Stop data publishing thread"); |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 234 | result.cancel(true); |
| 235 | collector = null; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 236 | } |
| 237 | |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 238 | @Override |
| 239 | public void createStatFlowRule(StatsFlowRule statsFlowRule) { |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 240 | |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 241 | setStatFlowRule(statsFlowRule, true); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | @Override |
| 245 | public void deleteStatFlowRule(StatsFlowRule statsFlowRule) { |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 246 | |
| 247 | setStatFlowRule(statsFlowRule, false); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 248 | } |
| 249 | |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 250 | /** |
| 251 | * Gets a set of the flow infos. |
| 252 | * |
| 253 | * @return a set of flow infos |
| 254 | */ |
| 255 | @Override |
| 256 | public Set<FlowInfo> getFlowInfos() { |
| 257 | Set<FlowInfo> flowInfos = Sets.newConcurrentHashSet(); |
| 258 | |
| 259 | // obtain all flow rule entries installed by telemetry app |
| 260 | for (FlowEntry entry : flowRuleService.getFlowEntriesById(appId)) { |
| 261 | FlowInfo.Builder fBuilder = new DefaultFlowInfo.DefaultBuilder(); |
| 262 | TrafficSelector selector = entry.selector(); |
| 263 | |
| 264 | IPCriterion srcIp = (IPCriterion) selector.getCriterion(IPV4_SRC); |
| 265 | IPCriterion dstIp = (IPCriterion) selector.getCriterion(IPV4_DST); |
| 266 | IPProtocolCriterion ipProtocol = |
| 267 | (IPProtocolCriterion) selector.getCriterion(IP_PROTO); |
| 268 | |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 269 | fBuilder.withFlowType(FLOW_TYPE_SONA) |
| 270 | .withSrcIp(srcIp.ip()) |
| 271 | .withDstIp(dstIp.ip()); |
| 272 | |
| 273 | if (ipProtocol != null) { |
| 274 | fBuilder.withProtocol((byte) ipProtocol.protocol()); |
| 275 | |
| 276 | if (ipProtocol.protocol() == PROTOCOL_TCP) { |
| 277 | TcpPortCriterion tcpSrc = |
| 278 | (TcpPortCriterion) selector.getCriterion(TCP_SRC); |
| 279 | TcpPortCriterion tcpDst = |
| 280 | (TcpPortCriterion) selector.getCriterion(TCP_DST); |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 281 | fBuilder.withSrcPort(tcpSrc.tcpPort()); |
| 282 | fBuilder.withDstPort(tcpDst.tcpPort()); |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 283 | } else if (ipProtocol.protocol() == PROTOCOL_UDP) { |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 284 | UdpPortCriterion udpSrc = |
| 285 | (UdpPortCriterion) selector.getCriterion(UDP_SRC); |
| 286 | UdpPortCriterion udpDst = |
| 287 | (UdpPortCriterion) selector.getCriterion(UDP_DST); |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 288 | fBuilder.withSrcPort(udpSrc.udpPort()); |
| 289 | fBuilder.withDstPort(udpDst.udpPort()); |
| 290 | } else { |
| 291 | log.debug("Other protocol: {}", ipProtocol.protocol()); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | fBuilder.withSrcMac(getMacAddress(srcIp.ip().address())) |
| 296 | .withDstMac(getMacAddress(dstIp.ip().address())) |
| 297 | .withInputInterfaceId(getInterfaceId(srcIp.ip().address())) |
| 298 | .withOutputInterfaceId(getInterfaceId(dstIp.ip().address())) |
| 299 | .withVlanId(getVlanId(srcIp.ip().address())) |
| 300 | .withDeviceId(entry.deviceId()); |
| 301 | |
| 302 | StatsInfo.Builder sBuilder = new DefaultStatsInfo.DefaultBuilder(); |
| 303 | |
| 304 | // TODO: need to collect error and drop packets stats |
| 305 | // TODO: need to make the refresh interval configurable |
| 306 | sBuilder.withStartupTime(System.currentTimeMillis()) |
| 307 | .withFstPktArrTime(System.currentTimeMillis()) |
| 308 | .withLstPktOffset((int) (REFRESH_INTERVAL * MILLISECONDS)) |
| 309 | .withCurrAccPkts((int) entry.packets()) |
| 310 | .withCurrAccBytes(entry.bytes()) |
| 311 | .withErrorPkts((short) 0) |
| 312 | .withDropPkts((short) 0); |
| 313 | |
| 314 | fBuilder.withStatsInfo(sBuilder.build()); |
| 315 | |
| 316 | FlowInfo flowInfo = mergeFlowInfo(fBuilder.build(), fBuilder, sBuilder); |
| 317 | |
| 318 | flowInfos.add(flowInfo); |
| 319 | |
| 320 | log.debug("FlowInfo: \n{}", flowInfo.toString()); |
| 321 | } |
| 322 | |
| 323 | return flowInfos; |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Gets a set of flow infos by referring to destination VM port. |
| 328 | * |
| 329 | * @return flow infos |
| 330 | */ |
| 331 | private Set<FlowInfo> getDstPortBasedFlowInfos() { |
| 332 | Set<FlowInfo> flowInfos = Sets.newConcurrentHashSet(); |
| 333 | Set<PortNumber> instPortNums = instPortService.instancePorts() |
| 334 | .stream() |
| 335 | .map(InstancePort::portNumber) |
| 336 | .collect(Collectors.toSet()); |
| 337 | Set<DeviceId> deviceIds = osNodeService.completeNodes(COMPUTE) |
| 338 | .stream() |
| 339 | .map(OpenstackNode::intgBridge) |
| 340 | .collect(Collectors.toSet()); |
| 341 | |
| 342 | deviceIds.forEach(d -> { |
| 343 | List<PortStatistics> stats = |
| 344 | deviceService.getPortStatistics(d) |
| 345 | .stream() |
| 346 | .filter(s -> instPortNums.contains(s.portNumber())) |
| 347 | .collect(Collectors.toList()); |
| 348 | |
| 349 | stats.forEach(s -> { |
| 350 | InstancePort instPort = getInstancePort(d, s.portNumber()); |
| 351 | flowInfos.add(buildTxPortInfo(instPort, s)); |
| 352 | flowInfos.add(buildRxPortInfo(instPort, s)); |
| 353 | }); |
| 354 | }); |
| 355 | |
| 356 | return flowInfos; |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Obtains the flow info generated by TX port. |
| 361 | * |
| 362 | * @param instPort instance port |
| 363 | * @param stat port statistics |
| 364 | * @return flow info |
| 365 | */ |
| 366 | private FlowInfo buildTxPortInfo(InstancePort instPort, PortStatistics stat) { |
| 367 | FlowInfo.Builder fBuilder = new DefaultFlowInfo.DefaultBuilder(); |
| 368 | |
| 369 | fBuilder.withFlowType(FLOW_TYPE_SONA) |
| 370 | .withSrcIp(IpPrefix.valueOf(instPort.ipAddress(), ARBITRARY_LENGTH)) |
| 371 | .withDstIp(IpPrefix.valueOf(ARBITRARY_IP)) |
| 372 | .withSrcMac(instPort.macAddress()) |
Boyoung Jeong | 1cca5e8 | 2018-08-01 21:00:08 +0900 | [diff] [blame] | 373 | .withDstMac(NO_HOST_MAC) |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 374 | .withDeviceId(instPort.deviceId()) |
| 375 | .withInputInterfaceId(ARBITRARY_IN_INTF) |
| 376 | .withOutputInterfaceId(ARBITRARY_OUT_INTF) |
| 377 | .withVlanId(VlanId.vlanId()); |
| 378 | |
| 379 | StatsInfo.Builder sBuilder = new DefaultStatsInfo.DefaultBuilder(); |
| 380 | sBuilder.withStartupTime(System.currentTimeMillis()) |
| 381 | .withFstPktArrTime(System.currentTimeMillis()) |
| 382 | .withLstPktOffset((int) (REFRESH_INTERVAL * MILLISECONDS)) |
| 383 | .withCurrAccPkts((int) stat.packetsSent()) |
| 384 | .withCurrAccBytes(stat.bytesSent()) |
| 385 | .withErrorPkts((short) stat.packetsTxErrors()) |
| 386 | .withDropPkts((short) stat.packetsTxDropped()); |
| 387 | |
| 388 | fBuilder.withStatsInfo(sBuilder.build()); |
| 389 | |
| 390 | return mergeFlowInfo(fBuilder.build(), fBuilder, sBuilder); |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * Obtains the flow info generated by RX port. |
| 395 | * |
| 396 | * @param instPort instance port |
| 397 | * @param stat port statistics |
| 398 | * @return flow info |
| 399 | */ |
| 400 | private FlowInfo buildRxPortInfo(InstancePort instPort, PortStatistics stat) { |
| 401 | FlowInfo.Builder fBuilder = new DefaultFlowInfo.DefaultBuilder(); |
| 402 | |
| 403 | fBuilder.withFlowType(FLOW_TYPE_SONA) |
| 404 | .withSrcIp(IpPrefix.valueOf(ARBITRARY_IP)) |
| 405 | .withDstIp(IpPrefix.valueOf(instPort.ipAddress(), ARBITRARY_LENGTH)) |
Boyoung Jeong | 1cca5e8 | 2018-08-01 21:00:08 +0900 | [diff] [blame] | 406 | .withSrcMac(NO_HOST_MAC) |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 407 | .withDstMac(instPort.macAddress()) |
| 408 | .withDeviceId(instPort.deviceId()) |
| 409 | .withInputInterfaceId(ARBITRARY_IN_INTF) |
| 410 | .withOutputInterfaceId(ARBITRARY_OUT_INTF) |
| 411 | .withVlanId(VlanId.vlanId()); |
| 412 | |
| 413 | StatsInfo.Builder sBuilder = new DefaultStatsInfo.DefaultBuilder(); |
| 414 | sBuilder.withStartupTime(System.currentTimeMillis()) |
| 415 | .withFstPktArrTime(System.currentTimeMillis()) |
| 416 | .withLstPktOffset((int) (REFRESH_INTERVAL * MILLISECONDS)) |
| 417 | .withCurrAccPkts((int) stat.packetsReceived()) |
| 418 | .withCurrAccBytes(stat.bytesReceived()) |
| 419 | .withErrorPkts((short) stat.packetsRxErrors()) |
| 420 | .withDropPkts((short) stat.packetsRxDropped()); |
| 421 | |
| 422 | fBuilder.withStatsInfo(sBuilder.build()); |
| 423 | |
| 424 | return mergeFlowInfo(fBuilder.build(), fBuilder, sBuilder); |
| 425 | } |
| 426 | |
| 427 | /** |
| 428 | * Obtains instance port which associated with the given device identifier |
| 429 | * and port number. |
| 430 | * |
| 431 | * @param deviceId device identifier |
| 432 | * @param portNumber port number |
| 433 | * @return instance port |
| 434 | */ |
| 435 | private InstancePort getInstancePort(DeviceId deviceId, PortNumber portNumber) { |
| 436 | return instPortService.instancePorts().stream() |
| 437 | .filter(p -> p.deviceId().equals(deviceId)) |
| 438 | .filter(p -> p.portNumber().equals(portNumber)) |
| 439 | .findFirst().orElse(null); |
| 440 | } |
| 441 | |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 442 | private void connectTables(DeviceId deviceId, int fromTable, int toTable, |
| 443 | StatsFlowRule statsFlowRule, int rulePriority, |
| 444 | boolean install) { |
| 445 | |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 446 | int srcPrefixLength = statsFlowRule.srcIpPrefix().prefixLength(); |
| 447 | int dstPrefixLength = statsFlowRule.dstIpPrefix().prefixLength(); |
| 448 | int prefixLength = rulePriority + srcPrefixLength + dstPrefixLength; |
| 449 | byte protocol = statsFlowRule.ipProtocol(); |
| 450 | |
| 451 | TrafficSelector.Builder selectorBuilder = |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 452 | DefaultTrafficSelector.builder() |
| 453 | .matchEthType(TYPE_IPV4) |
| 454 | .matchIPSrc(statsFlowRule.srcIpPrefix()) |
| 455 | .matchIPDst(statsFlowRule.dstIpPrefix()); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 456 | |
| 457 | if (protocol == PROTOCOL_TCP) { |
| 458 | selectorBuilder = selectorBuilder |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 459 | .matchIPProtocol(statsFlowRule.ipProtocol()) |
| 460 | .matchTcpSrc(statsFlowRule.srcTpPort()) |
| 461 | .matchTcpDst(statsFlowRule.dstTpPort()); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 462 | |
| 463 | } else if (protocol == PROTOCOL_UDP) { |
| 464 | selectorBuilder = selectorBuilder |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 465 | .matchIPProtocol(statsFlowRule.ipProtocol()) |
| 466 | .matchUdpSrc(statsFlowRule.srcTpPort()) |
| 467 | .matchUdpDst(statsFlowRule.dstTpPort()); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 468 | } else { |
| 469 | log.warn("Unsupported protocol {}", statsFlowRule.ipProtocol()); |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 470 | } |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 471 | |
| 472 | TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder(); |
| 473 | |
| 474 | treatmentBuilder.transition(toTable); |
| 475 | |
| 476 | FlowRule flowRule = DefaultFlowRule.builder() |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 477 | .forDevice(deviceId) |
| 478 | .withSelector(selectorBuilder.build()) |
| 479 | .withTreatment(treatmentBuilder.build()) |
| 480 | .withPriority(prefixLength) |
| 481 | .fromApp(appId) |
| 482 | .makePermanent() |
| 483 | .forTable(fromTable) |
| 484 | .build(); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 485 | |
| 486 | applyRule(flowRule, install); |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | /** |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 490 | * Installs stats related flow rule to switch. |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 491 | * |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 492 | * @param flowRule flow rule |
| 493 | * @param install flag to install or not |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 494 | */ |
| 495 | private void applyRule(FlowRule flowRule, boolean install) { |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 496 | FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder(); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 497 | flowOpsBuilder = install ? |
| 498 | flowOpsBuilder.add(flowRule) : flowOpsBuilder.remove(flowRule); |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 499 | |
| 500 | flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() { |
| 501 | @Override |
| 502 | public void onSuccess(FlowRuleOperations ops) { |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 503 | log.debug("Install rules for telemetry stats: \n {}", |
| 504 | ops.toString()); |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | @Override |
| 508 | public void onError(FlowRuleOperations ops) { |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 509 | log.debug("Failed to install rules for telemetry stats: \n {}", |
| 510 | ops.toString()); |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 511 | } |
| 512 | })); |
| 513 | } |
| 514 | |
| 515 | /** |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 516 | * Merges old FlowInfo.StatsInfo and current FlowInfo.StatsInfo. |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 517 | * |
| 518 | * @param flowInfo current FlowInfo object |
| 519 | * @param fBuilder Builder for FlowInfo |
| 520 | * @param sBuilder Builder for StatsInfo |
| 521 | * @return Merged FlowInfo object |
| 522 | */ |
| 523 | private FlowInfo mergeFlowInfo(FlowInfo flowInfo, |
| 524 | FlowInfo.Builder fBuilder, |
| 525 | StatsInfo.Builder sBuilder) { |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 526 | for (FlowInfo gFlowInfo : gFlowInfoSet) { |
| 527 | log.debug("Old FlowInfo:\n{}", gFlowInfo.toString()); |
| 528 | if (gFlowInfo.roughEquals(flowInfo)) { |
| 529 | |
| 530 | // Get old StatsInfo object and merge the value to current object. |
| 531 | StatsInfo oldStatsInfo = gFlowInfo.statsInfo(); |
| 532 | sBuilder.withPrevAccPkts(oldStatsInfo.currAccPkts()); |
| 533 | sBuilder.withPrevAccBytes(oldStatsInfo.currAccBytes()); |
| 534 | FlowInfo newFlowInfo = fBuilder.withStatsInfo(sBuilder.build()) |
| 535 | .build(); |
| 536 | |
| 537 | gFlowInfoSet.remove(gFlowInfo); |
| 538 | gFlowInfoSet.add(newFlowInfo); |
Jian Li | 85573f4 | 2018-06-27 22:29:14 +0900 | [diff] [blame] | 539 | log.debug("Old FlowInfo found, Merge this {}", newFlowInfo.toString()); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 540 | return newFlowInfo; |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 541 | } |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 542 | } |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 543 | |
| 544 | // No such record, then build the FlowInfo object and return this object. |
Jian Li | 85573f4 | 2018-06-27 22:29:14 +0900 | [diff] [blame] | 545 | log.debug("No FlowInfo found, add new FlowInfo {}", flowInfo.toString()); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 546 | FlowInfo newFlowInfo = fBuilder.withStatsInfo(sBuilder.build()).build(); |
| 547 | gFlowInfoSet.add(newFlowInfo); |
| 548 | return newFlowInfo; |
| 549 | } |
| 550 | |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 551 | /** |
| 552 | * Installs flow rules for collecting both normal and reverse path flow stats. |
| 553 | * |
| 554 | * @param statsFlowRule flow rule used for collecting stats |
| 555 | * @param install flow rule installation flag |
| 556 | */ |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 557 | private void setStatFlowRule(StatsFlowRule statsFlowRule, boolean install) { |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 558 | setStatFlowRuleBase(statsFlowRule, install); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 559 | |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 560 | // if reverse path stats is enabled, we will install flow rules for |
| 561 | // collecting reverse path vFlow stats |
| 562 | if (reversePathStats) { |
| 563 | StatsFlowRule reverseFlowRule = DefaultStatsFlowRule.builder() |
| 564 | .srcIpPrefix(statsFlowRule.dstIpPrefix()) |
| 565 | .dstIpPrefix(statsFlowRule.srcIpPrefix()) |
| 566 | .ipProtocol(statsFlowRule.ipProtocol()) |
| 567 | .srcTpPort(statsFlowRule.dstTpPort()) |
| 568 | .dstTpPort(statsFlowRule.srcTpPort()) |
| 569 | .build(); |
| 570 | setStatFlowRuleBase(reverseFlowRule, install); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | /** |
| 575 | * A base method which is for installing flow rules for collecting stats. |
| 576 | * |
| 577 | * @param statsFlowRule flow rule used for collecting stats |
| 578 | * @param install flow rule installation flag |
| 579 | */ |
| 580 | private void setStatFlowRuleBase(StatsFlowRule statsFlowRule, boolean install) { |
Jian Li | e6110b7 | 2018-07-06 19:06:36 +0900 | [diff] [blame] | 581 | |
| 582 | IpPrefix srcIp = statsFlowRule.srcIpPrefix(); |
| 583 | IpPrefix dstIp = statsFlowRule.dstIpPrefix(); |
| 584 | DeviceId srcDeviceId = getDeviceId(srcIp.address()); |
| 585 | DeviceId dstDeviceId = getDeviceId(dstIp.address()); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 586 | |
Jian Li | 998ec7b | 2018-06-29 15:15:49 +0900 | [diff] [blame] | 587 | if (srcDeviceId == null && dstDeviceId == null) { |
Jian Li | 85573f4 | 2018-06-27 22:29:14 +0900 | [diff] [blame] | 588 | return; |
| 589 | } |
| 590 | |
Jian Li | 998ec7b | 2018-06-29 15:15:49 +0900 | [diff] [blame] | 591 | if (srcDeviceId != null) { |
Jian Li | 87ded82 | 2018-07-02 18:31:22 +0900 | [diff] [blame] | 592 | connectTables(srcDeviceId, STAT_INBOUND_TABLE, VTAP_INBOUND_TABLE, |
Jian Li | 998ec7b | 2018-06-29 15:15:49 +0900 | [diff] [blame] | 593 | statsFlowRule, METRIC_PRIORITY_SOURCE, install); |
Jian Li | 998ec7b | 2018-06-29 15:15:49 +0900 | [diff] [blame] | 594 | |
Jian Li | e6110b7 | 2018-07-06 19:06:36 +0900 | [diff] [blame] | 595 | if (install) { |
| 596 | log.info("Install ingress stat flow rule for SrcIp:{} DstIp:{}", |
| 597 | srcIp.toString(), dstIp.toString()); |
| 598 | } else { |
| 599 | log.info("Remove ingress stat flow rule for SrcIp:{} DstIp:{}", |
| 600 | srcIp.toString(), dstIp.toString()); |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 601 | } |
Jian Li | 998ec7b | 2018-06-29 15:15:49 +0900 | [diff] [blame] | 602 | } |
Jian Li | 85573f4 | 2018-06-27 22:29:14 +0900 | [diff] [blame] | 603 | |
Jian Li | e6110b7 | 2018-07-06 19:06:36 +0900 | [diff] [blame] | 604 | Set<IpPrefix> vxlanIps = osNetworkService.getFixedIpsByNetworkType(VXLAN); |
| 605 | Set<IpPrefix> vlanIps = osNetworkService.getFixedIpsByNetworkType(VLAN); |
| 606 | Set<IpPrefix> flatIps = osNetworkService.getFixedIpsByNetworkType(FLAT); |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 607 | |
Jian Li | e6110b7 | 2018-07-06 19:06:36 +0900 | [diff] [blame] | 608 | int fromTable, toTable; |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 609 | |
Jian Li | e6110b7 | 2018-07-06 19:06:36 +0900 | [diff] [blame] | 610 | if (dstDeviceId != null && egressStats) { |
| 611 | |
| 612 | IpPrefix dstIpPrefix = statsFlowRule.dstIpPrefix(); |
| 613 | |
| 614 | if (vxlanIps.contains(dstIpPrefix) || vlanIps.contains(dstIpPrefix)) { |
| 615 | fromTable = STAT_OUTBOUND_TABLE; |
| 616 | toTable = VTAP_OUTBOUND_TABLE; |
| 617 | } else if (flatIps.contains(dstIpPrefix)) { |
| 618 | fromTable = STAT_FLAT_OUTBOUND_TABLE; |
| 619 | toTable = VTAP_FLAT_OUTBOUND_TABLE; |
| 620 | } else { |
| 621 | return; |
| 622 | } |
| 623 | |
| 624 | connectTables(dstDeviceId, fromTable, toTable, |
| 625 | statsFlowRule, METRIC_PRIORITY_TARGET, install); |
| 626 | |
| 627 | if (install) { |
| 628 | log.info("Install egress stat flow rule for SrcIp:{} DstIp:{}", |
| 629 | srcIp.toString(), dstIp.toString()); |
| 630 | } else { |
| 631 | log.info("Remove egress stat flow rule for SrcIp:{} DstIp:{}", |
| 632 | srcIp.toString(), dstIp.toString()); |
| 633 | } |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 634 | } |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | /** |
Jian Li | 85573f4 | 2018-06-27 22:29:14 +0900 | [diff] [blame] | 638 | * Get Device ID which the VM is located. |
| 639 | * |
| 640 | * @param ipAddress IP Address of host |
| 641 | * @return Device ID |
| 642 | */ |
| 643 | private DeviceId getDeviceId(IpAddress ipAddress) { |
| 644 | if (!hostService.getHostsByIp(ipAddress).isEmpty()) { |
| 645 | Optional<Host> host = hostService.getHostsByIp(ipAddress).stream().findAny(); |
| 646 | return host.map(host1 -> host1.location().deviceId()).orElse(null); |
| 647 | } else { |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 648 | log.debug("No DeviceID is associated to {}", ipAddress.toString()); |
Jian Li | 85573f4 | 2018-06-27 22:29:14 +0900 | [diff] [blame] | 649 | return null; |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 650 | } |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | /** |
| 654 | * Get VLAN ID with respect to IP Address. |
| 655 | * |
| 656 | * @param ipAddress IP Address of host |
| 657 | * @return VLAN ID |
| 658 | */ |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 659 | private VlanId getVlanId(IpAddress ipAddress) { |
| 660 | if (!hostService.getHostsByIp(ipAddress).isEmpty()) { |
| 661 | Host host = hostService.getHostsByIp(ipAddress).stream().findAny().get(); |
| 662 | return host.vlan(); |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 663 | } |
| 664 | return VlanId.vlanId(); |
| 665 | } |
| 666 | |
| 667 | /** |
| 668 | * Get Interface ID of Switch which is connected to a host. |
| 669 | * |
| 670 | * @param ipAddress IP Address of host |
| 671 | * @return Interface ID of Switch |
| 672 | */ |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 673 | private int getInterfaceId(IpAddress ipAddress) { |
| 674 | if (!hostService.getHostsByIp(ipAddress).isEmpty()) { |
| 675 | Host host = hostService.getHostsByIp(ipAddress).stream().findAny().get(); |
| 676 | return (int) host.location().port().toLong(); |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 677 | } |
| 678 | return -1; |
| 679 | } |
| 680 | |
| 681 | /** |
| 682 | * Get MAC Address of host. |
| 683 | * |
| 684 | * @param ipAddress IP Address of host |
| 685 | * @return MAC Address of host |
| 686 | */ |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 687 | private MacAddress getMacAddress(IpAddress ipAddress) { |
| 688 | if (!hostService.getHostsByIp(ipAddress).isEmpty()) { |
| 689 | Host host = hostService.getHostsByIp(ipAddress).stream().findAny().get(); |
| 690 | return host.mac(); |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 691 | } |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 692 | |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 693 | return NO_HOST_MAC; |
| 694 | } |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 695 | |
Boyoung Jeong | 1cca5e8 | 2018-08-01 21:00:08 +0900 | [diff] [blame] | 696 | private void enqFlowInfo(FlowInfo flowInfo) { |
| 697 | String key = flowInfo.uniqueFlowInfoKey(); |
| 698 | Queue<FlowInfo> queue = flowInfoMap.get(key); |
| 699 | if (queue == null) { |
| 700 | Queue<FlowInfo> newQueue = new LinkedList<FlowInfo>(); |
| 701 | newQueue.offer(flowInfo); |
| 702 | flowInfoMap.put(key, newQueue); |
| 703 | return; |
| 704 | } |
| 705 | queue.offer(flowInfo); |
| 706 | |
| 707 | while (queue.size() > DEFAULT_DATA_POINT_SIZE) { |
| 708 | queue.remove(); // Removes a garbage data in the queue. |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | public Map<String, Queue<FlowInfo>> getFlowInfoMap() { |
| 713 | return flowInfoMap; |
| 714 | } |
| 715 | |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 716 | /** |
| 717 | * Extracts properties from the component configuration context. |
| 718 | * |
| 719 | * @param context the component context |
| 720 | */ |
| 721 | private void readComponentConfiguration(ComponentContext context) { |
| 722 | Dictionary<?, ?> properties = context.getProperties(); |
| 723 | |
| 724 | Boolean reversePathStatsConfigured = |
| 725 | getBooleanProperty(properties, REVERSE_PATH_STATS); |
| 726 | if (reversePathStatsConfigured == null) { |
| 727 | reversePathStats = DEFAULT_REVERSE_PATH_STATS; |
| 728 | log.info("Reversed path stats flag is NOT " + |
| 729 | "configured, default value is {}", reversePathStats); |
| 730 | } else { |
| 731 | reversePathStats = reversePathStatsConfigured; |
| 732 | log.info("Configured. Reversed path stats flag is {}", reversePathStats); |
| 733 | } |
| 734 | |
| 735 | Boolean egressStatsConfigured = getBooleanProperty(properties, EGRESS_STATS); |
| 736 | if (egressStatsConfigured == null) { |
| 737 | egressStats = DEFAULT_EGRESS_STATS; |
| 738 | log.info("Egress stats flag is NOT " + |
| 739 | "configured, default value is {}", egressStats); |
| 740 | } else { |
| 741 | egressStats = egressStatsConfigured; |
| 742 | log.info("Configured. Egress stats flag is {}", egressStats); |
| 743 | } |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 744 | |
| 745 | Boolean portStatsConfigured = getBooleanProperty(properties, PORT_STATS); |
| 746 | if (portStatsConfigured == null) { |
| 747 | portStats = DEFAULT_PORT_STATS; |
| 748 | log.info("Port stats flag is NOT " + |
| 749 | "configured, default value is {}", portStats); |
| 750 | } else { |
| 751 | portStats = portStatsConfigured; |
| 752 | log.info("Configured. Port stats flag is {}", portStats); |
| 753 | } |
Jian Li | 753280e | 2018-07-03 02:24:34 +0900 | [diff] [blame] | 754 | } |
| 755 | |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 756 | private class TelemetryCollector implements Runnable { |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 757 | @Override |
| 758 | public void run() { |
Jian Li | 85573f4 | 2018-06-27 22:29:14 +0900 | [diff] [blame] | 759 | Set<FlowInfo> filteredFlowInfos = Sets.newConcurrentHashSet(); |
| 760 | |
| 761 | // we only let the master controller of the device where the |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 762 | // stats flow rules are installed send stats message |
| 763 | getFlowInfos().forEach(f -> { |
| 764 | if (checkSrcDstLocalMaster(f)) { |
Jian Li | 85573f4 | 2018-06-27 22:29:14 +0900 | [diff] [blame] | 765 | filteredFlowInfos.add(f); |
| 766 | } |
| 767 | }); |
| 768 | |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 769 | // we only let the master controller of the device where the port |
| 770 | // is located to send stats message |
| 771 | if (portStats) { |
| 772 | getDstPortBasedFlowInfos().forEach(f -> { |
| 773 | if (checkSrcDstLocalMaster(f)) { |
| 774 | filteredFlowInfos.add(f); |
| 775 | } |
| 776 | }); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 777 | } |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 778 | |
| 779 | telemetryService.publish(filteredFlowInfos); |
Boyoung Jeong | 1cca5e8 | 2018-08-01 21:00:08 +0900 | [diff] [blame] | 780 | |
| 781 | // TODO: Refactor the following code to "TelemetryService" style. |
| 782 | filteredFlowInfos.forEach(flowInfo -> { |
| 783 | enqFlowInfo(flowInfo); |
| 784 | }); |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | private boolean checkSrcDstLocalMaster(FlowInfo info) { |
| 788 | DeviceId srcDeviceId = getDeviceId(info.srcIp().address()); |
| 789 | DeviceId dstDeviceId = getDeviceId(info.dstIp().address()); |
| 790 | |
| 791 | boolean isSrcLocalMaster = srcDeviceId != null && |
| 792 | mastershipService.isLocalMaster(srcDeviceId); |
| 793 | boolean isDstLocalMaster = dstDeviceId != null && |
| 794 | mastershipService.isLocalMaster(dstDeviceId); |
| 795 | |
| 796 | return isSrcLocalMaster || isDstLocalMaster; |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 797 | } |
| 798 | } |
Boyoung Jeong | 9e8faec | 2018-06-17 21:19:23 +0900 | [diff] [blame] | 799 | } |