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