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