Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [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 | |
| 17 | package org.onosproject.routescale; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.ImmutableSet; |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 21 | import org.onlab.packet.Ethernet; |
| 22 | import org.onlab.packet.IpAddress; |
| 23 | import org.onlab.packet.IpPrefix; |
| 24 | import org.onlab.packet.MacAddress; |
| 25 | import org.onosproject.app.ApplicationService; |
Thomas Vachuska | fa61549 | 2018-03-19 09:11:51 -0700 | [diff] [blame] | 26 | import org.onosproject.cfg.ComponentConfigService; |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.net.Device; |
| 29 | import org.onosproject.net.DeviceId; |
| 30 | import org.onosproject.net.Host; |
Thomas Vachuska | fa61549 | 2018-03-19 09:11:51 -0700 | [diff] [blame] | 31 | import org.onosproject.net.MastershipRole; |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 32 | import org.onosproject.net.PortNumber; |
| 33 | import org.onosproject.net.device.DeviceService; |
| 34 | import org.onosproject.net.flow.DefaultFlowRule; |
| 35 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 36 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
Thomas Vachuska | fa61549 | 2018-03-19 09:11:51 -0700 | [diff] [blame] | 37 | import org.onosproject.net.flow.FlowEntry; |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 38 | import org.onosproject.net.flow.FlowRule; |
| 39 | import org.onosproject.net.flow.FlowRuleOperations; |
| 40 | import org.onosproject.net.flow.FlowRuleService; |
| 41 | import org.onosproject.net.flow.TrafficSelector; |
| 42 | import org.onosproject.net.flow.TrafficTreatment; |
| 43 | import org.onosproject.net.host.HostAdminService; |
Thomas Vachuska | fa61549 | 2018-03-19 09:11:51 -0700 | [diff] [blame] | 44 | import org.onosproject.net.link.LinkService; |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 45 | import org.onosproject.routeservice.Route; |
| 46 | import org.onosproject.routeservice.RouteAdminService; |
Thomas Vachuska | fa61549 | 2018-03-19 09:11:51 -0700 | [diff] [blame] | 47 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 48 | import org.osgi.service.component.annotations.Activate; |
| 49 | import org.osgi.service.component.annotations.Component; |
| 50 | import org.osgi.service.component.annotations.Deactivate; |
| 51 | import org.osgi.service.component.annotations.Modified; |
| 52 | import org.osgi.service.component.annotations.Reference; |
| 53 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 54 | import org.slf4j.Logger; |
| 55 | import org.slf4j.LoggerFactory; |
| 56 | |
Thomas Vachuska | fa61549 | 2018-03-19 09:11:51 -0700 | [diff] [blame] | 57 | import java.util.Dictionary; |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 58 | import java.util.List; |
Thomas Vachuska | fa61549 | 2018-03-19 09:11:51 -0700 | [diff] [blame] | 59 | import java.util.Objects; |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 60 | import java.util.Random; |
| 61 | |
Thomas Vachuska | fa61549 | 2018-03-19 09:11:51 -0700 | [diff] [blame] | 62 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 63 | import static org.onlab.util.Tools.get; |
Ray Milkey | 88dd7e2 | 2018-10-24 10:04:03 -0700 | [diff] [blame] | 64 | import static org.onosproject.routescale.OsgiPropertyConstants.FLOW_COUNT; |
| 65 | import static org.onosproject.routescale.OsgiPropertyConstants.FLOW_COUNT_DEFAULT; |
| 66 | import static org.onosproject.routescale.OsgiPropertyConstants.ROUTE_COUNT; |
| 67 | import static org.onosproject.routescale.OsgiPropertyConstants.ROUTE_COUNT_DEFAULT; |
Thomas Vachuska | fa61549 | 2018-03-19 09:11:51 -0700 | [diff] [blame] | 68 | |
Ray Milkey | 88dd7e2 | 2018-10-24 10:04:03 -0700 | [diff] [blame] | 69 | @Component( |
| 70 | immediate = true, |
| 71 | service = ScaleTestManager.class, |
| 72 | property = { |
| 73 | FLOW_COUNT + ":Integer=" + FLOW_COUNT_DEFAULT, |
| 74 | ROUTE_COUNT + ":Integer=" + ROUTE_COUNT_DEFAULT, |
| 75 | } |
| 76 | ) |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 77 | public class ScaleTestManager { |
| 78 | |
| 79 | private Logger log = LoggerFactory.getLogger(getClass()); |
| 80 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 81 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 82 | protected ApplicationService applicationService; |
| 83 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 84 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 85 | protected DeviceService deviceService; |
| 86 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 87 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 88 | protected HostAdminService hostAdminService; |
| 89 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 90 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | fa61549 | 2018-03-19 09:11:51 -0700 | [diff] [blame] | 91 | protected LinkService linkService; |
| 92 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 93 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 94 | protected FlowRuleService flowRuleService; |
| 95 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 96 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 97 | protected RouteAdminService routeAdminService; |
| 98 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 99 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | fa61549 | 2018-03-19 09:11:51 -0700 | [diff] [blame] | 100 | protected ComponentConfigService componentConfigService; |
| 101 | |
Ray Milkey | 88dd7e2 | 2018-10-24 10:04:03 -0700 | [diff] [blame] | 102 | /** Number of flows to be maintained in the system. */ |
| 103 | private int flowCount = FLOW_COUNT_DEFAULT; |
Thomas Vachuska | fa61549 | 2018-03-19 09:11:51 -0700 | [diff] [blame] | 104 | |
Ray Milkey | 88dd7e2 | 2018-10-24 10:04:03 -0700 | [diff] [blame] | 105 | /** Number of routes to be maintained in the system. */ |
| 106 | private int routeCount = ROUTE_COUNT_DEFAULT; |
Thomas Vachuska | fa61549 | 2018-03-19 09:11:51 -0700 | [diff] [blame] | 107 | |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 108 | private final Random random = new Random(System.currentTimeMillis()); |
| 109 | |
| 110 | private ApplicationId appId; |
| 111 | |
Thomas Vachuska | 6205f69 | 2018-03-21 12:22:04 -0700 | [diff] [blame] | 112 | private long macBase = System.currentTimeMillis(); |
| 113 | |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 114 | @Activate |
| 115 | protected void activate() { |
| 116 | appId = applicationService.getId("org.onosproject.routescale"); |
Thomas Vachuska | fa61549 | 2018-03-19 09:11:51 -0700 | [diff] [blame] | 117 | componentConfigService.registerProperties(getClass()); |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 118 | log.info("Started"); |
| 119 | } |
| 120 | |
| 121 | @Deactivate |
| 122 | protected void deactivate() { |
Thomas Vachuska | fa61549 | 2018-03-19 09:11:51 -0700 | [diff] [blame] | 123 | componentConfigService.unregisterProperties(getClass(), false); |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 124 | log.info("Stopped"); |
| 125 | } |
| 126 | |
Thomas Vachuska | fa61549 | 2018-03-19 09:11:51 -0700 | [diff] [blame] | 127 | @Modified |
| 128 | public void modified(ComponentContext context) { |
| 129 | if (context == null) { |
| 130 | return; |
| 131 | } |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 132 | |
Thomas Vachuska | fa61549 | 2018-03-19 09:11:51 -0700 | [diff] [blame] | 133 | Dictionary<?, ?> properties = context.getProperties(); |
| 134 | try { |
Ray Milkey | 88dd7e2 | 2018-10-24 10:04:03 -0700 | [diff] [blame] | 135 | String s = get(properties, FLOW_COUNT); |
Thomas Vachuska | fa61549 | 2018-03-19 09:11:51 -0700 | [diff] [blame] | 136 | flowCount = isNullOrEmpty(s) ? flowCount : Integer.parseInt(s.trim()); |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 137 | |
Ray Milkey | 88dd7e2 | 2018-10-24 10:04:03 -0700 | [diff] [blame] | 138 | s = get(properties, ROUTE_COUNT); |
Thomas Vachuska | fa61549 | 2018-03-19 09:11:51 -0700 | [diff] [blame] | 139 | routeCount = isNullOrEmpty(s) ? routeCount : Integer.parseInt(s.trim()); |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 140 | |
Thomas Vachuska | fa61549 | 2018-03-19 09:11:51 -0700 | [diff] [blame] | 141 | log.info("Reconfigured; flowCount={}; routeCount={}", flowCount, routeCount); |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 142 | |
Thomas Vachuska | fa61549 | 2018-03-19 09:11:51 -0700 | [diff] [blame] | 143 | adjustFlows(); |
| 144 | adjustRoutes(); |
| 145 | |
| 146 | } catch (NumberFormatException | ClassCastException e) { |
| 147 | log.warn("Misconfigured", e); |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
Thomas Vachuska | fa61549 | 2018-03-19 09:11:51 -0700 | [diff] [blame] | 151 | private void adjustFlows() { |
| 152 | int deviceCount = deviceService.getAvailableDeviceCount(); |
| 153 | if (deviceCount == 0) { |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | int flowsPerDevice = flowCount / deviceCount; |
| 158 | for (Device device : deviceService.getAvailableDevices()) { |
| 159 | DeviceId id = device.id(); |
| 160 | if (deviceService.getRole(id) != MastershipRole.MASTER || |
| 161 | flowsPerDevice == 0) { |
| 162 | continue; |
| 163 | } |
| 164 | |
| 165 | int currentFlowCount = flowRuleService.getFlowRuleCount(id); |
| 166 | if (flowsPerDevice > currentFlowCount) { |
| 167 | addMoreFlows(flowsPerDevice, device, id, currentFlowCount); |
| 168 | |
| 169 | } else if (flowsPerDevice < currentFlowCount) { |
| 170 | removeExcessFlows(flowsPerDevice, id, currentFlowCount); |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | private void addMoreFlows(int flowsPerDevice, Device device, DeviceId id, |
| 176 | int currentFlowCount) { |
| 177 | int c = flowsPerDevice - currentFlowCount; |
| 178 | log.info("Adding {} flows for device {}", c, id); |
| 179 | List<PortNumber> ports = devicePorts(device); |
| 180 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 181 | for (int i = 0; i < c; i++) { |
| 182 | FlowRule.Builder frb = DefaultFlowRule.builder(); |
Thomas Vachuska | 6205f69 | 2018-03-21 12:22:04 -0700 | [diff] [blame] | 183 | frb.fromApp(appId).makePermanent().withPriority((currentFlowCount + i) % FlowRule.MAX_PRIORITY); |
Thomas Vachuska | fa61549 | 2018-03-19 09:11:51 -0700 | [diff] [blame] | 184 | TrafficSelector.Builder tsb = DefaultTrafficSelector.builder(); |
| 185 | TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder(); |
| 186 | |
| 187 | tsb.matchEthType(Ethernet.TYPE_IPV4); |
Thomas Vachuska | 6205f69 | 2018-03-21 12:22:04 -0700 | [diff] [blame] | 188 | tsb.matchEthDst(randomMac()); |
Thomas Vachuska | fa61549 | 2018-03-19 09:11:51 -0700 | [diff] [blame] | 189 | ttb.setEthDst(randomMac()).setEthSrc(randomMac()); |
| 190 | ttb.setOutput(randomPort(ports)); |
| 191 | frb.withSelector(tsb.build()).withTreatment(ttb.build()); |
| 192 | ops.add(frb.forDevice(id).build()); |
| 193 | } |
| 194 | flowRuleService.apply(ops.build()); |
| 195 | } |
| 196 | |
| 197 | private void removeExcessFlows(int flowsPerDevice, DeviceId id, |
| 198 | int currentFlowCount) { |
| 199 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
Thomas Vachuska | 6205f69 | 2018-03-21 12:22:04 -0700 | [diff] [blame] | 200 | int c = currentFlowCount - flowsPerDevice; |
Thomas Vachuska | fa61549 | 2018-03-19 09:11:51 -0700 | [diff] [blame] | 201 | log.info("Removing {} flows from device {}", c, id); |
| 202 | for (FlowEntry e : flowRuleService.getFlowEntries(id)) { |
| 203 | if (Objects.equals(e.appId(), appId.id()) && c > 0) { |
| 204 | ops.remove(e); |
| 205 | c--; |
Thomas Vachuska | 6205f69 | 2018-03-21 12:22:04 -0700 | [diff] [blame] | 206 | } else if (c == 0) { |
| 207 | break; |
Thomas Vachuska | fa61549 | 2018-03-19 09:11:51 -0700 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | flowRuleService.apply(ops.build()); |
| 211 | } |
| 212 | |
| 213 | private void adjustRoutes() { |
| 214 | int currentRouteCount = |
| 215 | routeAdminService.getRouteTables().parallelStream() |
| 216 | .mapToInt(t -> routeAdminService.getRoutes(t).size()).sum(); |
| 217 | if (currentRouteCount < routeCount) { |
| 218 | createRoutes(routeCount - currentRouteCount); |
| 219 | } else if (currentRouteCount > routeCount) { |
| 220 | removeRoutes(currentRouteCount - routeCount); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | // Returns a list of ports on the given device that have either links or |
| 225 | // hosts connected to them. |
| 226 | private List<PortNumber> devicePorts(Device device) { |
| 227 | DeviceId id = device.id(); |
| 228 | ImmutableList.Builder<PortNumber> ports = ImmutableList.builder(); |
| 229 | linkService.getDeviceEgressLinks(id).forEach(l -> ports.add(l.src().port())); |
| 230 | hostAdminService.getConnectedHosts(id) |
| 231 | .forEach(h -> h.locations().stream() |
| 232 | .filter(l -> Objects.equals(id, l.elementId())) |
| 233 | .findFirst() |
| 234 | .ifPresent(l -> ports.add(l.port()))); |
| 235 | return ports.build(); |
| 236 | } |
| 237 | |
| 238 | // Creates the specified number of random routes. Such routes are generated |
| 239 | // using random IP prefices with next hop being an IP address of a randomly |
| 240 | // chosen hosts. |
| 241 | private void createRoutes(int routeCount) { |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 242 | List<Host> hosts = ImmutableList.copyOf(hostAdminService.getHosts()); |
| 243 | ImmutableSet.Builder<Route> routes = ImmutableSet.builder(); |
| 244 | for (int i = 0; i < routeCount; i++) { |
| 245 | IpPrefix prefix = randomIp().toIpPrefix(); |
| 246 | IpAddress nextHop = randomIp(hosts); |
| 247 | routes.add(new Route(Route.Source.STATIC, prefix, nextHop)); |
| 248 | } |
| 249 | routeAdminService.update(routes.build()); |
| 250 | } |
| 251 | |
Thomas Vachuska | fa61549 | 2018-03-19 09:11:51 -0700 | [diff] [blame] | 252 | // Removes the specified number of routes chosen at random. |
| 253 | private void removeRoutes(int routeCount) { |
| 254 | log.warn("Not implemented yet"); |
| 255 | } |
| 256 | |
| 257 | // Generates a random IP address. |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 258 | private IpAddress randomIp() { |
| 259 | byte[] bytes = new byte[4]; |
| 260 | random.nextBytes(bytes); |
| 261 | return IpAddress.valueOf(IpAddress.Version.INET, bytes, 0); |
| 262 | } |
| 263 | |
Thomas Vachuska | fa61549 | 2018-03-19 09:11:51 -0700 | [diff] [blame] | 264 | // Generates a random MAC address. |
| 265 | private MacAddress randomMac() { |
Thomas Vachuska | 6205f69 | 2018-03-21 12:22:04 -0700 | [diff] [blame] | 266 | return MacAddress.valueOf(macBase++); |
Thomas Vachuska | fa61549 | 2018-03-19 09:11:51 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | // Returns IP address of a host randomly chosen from the specified list. |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 270 | private IpAddress randomIp(List<Host> hosts) { |
| 271 | Host host = hosts.get(random.nextInt(hosts.size())); |
| 272 | return host.ipAddresses().iterator().next(); |
| 273 | } |
| 274 | |
Thomas Vachuska | fa61549 | 2018-03-19 09:11:51 -0700 | [diff] [blame] | 275 | // Returns port number randomly chosen from the given list of port numbers. |
| 276 | private PortNumber randomPort(List<PortNumber> ports) { |
| 277 | return ports.get(random.nextInt(ports.size())); |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | } |