Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 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.routing.impl; |
| 18 | |
| 19 | import com.google.common.cache.Cache; |
| 20 | import com.google.common.cache.CacheBuilder; |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 21 | import org.onlab.packet.EthType; |
| 22 | import org.onlab.packet.Ethernet; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 23 | import org.onlab.packet.IP; |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 24 | import org.onlab.packet.IPv4; |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 25 | import org.onlab.packet.IPv6; |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 26 | import org.onlab.packet.IpAddress; |
| 27 | import org.onlab.packet.MacAddress; |
| 28 | import org.onlab.packet.VlanId; |
| 29 | import org.onlab.util.Tools; |
| 30 | import org.onosproject.cfg.ComponentConfigService; |
| 31 | import org.onosproject.core.ApplicationId; |
| 32 | import org.onosproject.core.CoreService; |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 33 | import org.onosproject.net.ConnectPoint; |
| 34 | import org.onosproject.net.Host; |
| 35 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 36 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 37 | import org.onosproject.net.flow.TrafficSelector; |
| 38 | import org.onosproject.net.host.HostEvent; |
| 39 | import org.onosproject.net.host.HostListener; |
| 40 | import org.onosproject.net.host.HostService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 41 | import org.onosproject.net.intf.Interface; |
| 42 | import org.onosproject.net.intf.InterfaceService; |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 43 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 44 | import org.onosproject.net.packet.OutboundPacket; |
| 45 | import org.onosproject.net.packet.PacketContext; |
| 46 | import org.onosproject.net.packet.PacketPriority; |
| 47 | import org.onosproject.net.packet.PacketProcessor; |
| 48 | import org.onosproject.net.packet.PacketService; |
| 49 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 50 | import org.osgi.service.component.annotations.Activate; |
| 51 | import org.osgi.service.component.annotations.Component; |
| 52 | import org.osgi.service.component.annotations.Deactivate; |
| 53 | import org.osgi.service.component.annotations.Modified; |
| 54 | import org.osgi.service.component.annotations.Reference; |
| 55 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 56 | import org.slf4j.Logger; |
| 57 | import org.slf4j.LoggerFactory; |
| 58 | |
| 59 | import java.nio.ByteBuffer; |
| 60 | import java.util.Optional; |
| 61 | import java.util.Queue; |
| 62 | import java.util.concurrent.ConcurrentLinkedQueue; |
| 63 | import java.util.concurrent.TimeUnit; |
| 64 | |
| 65 | import static com.google.common.base.Preconditions.checkNotNull; |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 66 | import static org.onlab.packet.IpAddress.Version.INET6; |
Ray Milkey | 4694e06 | 2018-10-31 13:17:18 -0700 | [diff] [blame] | 67 | import static org.onosproject.routing.impl.OsgiPropertyConstants.ENABLED; |
| 68 | import static org.onosproject.routing.impl.OsgiPropertyConstants.ENABLED_DEFAULT; |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * Reactively handles sending packets to hosts that are directly connected to |
| 72 | * router interfaces. |
| 73 | */ |
Ray Milkey | 4694e06 | 2018-10-31 13:17:18 -0700 | [diff] [blame] | 74 | @Component( |
| 75 | immediate = true, |
| 76 | enabled = false, |
| 77 | property = { |
| 78 | ENABLED + ":Boolean=" + ENABLED_DEFAULT |
| 79 | } |
| 80 | ) |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 81 | public class DirectHostManager { |
| 82 | |
| 83 | private Logger log = LoggerFactory.getLogger(getClass()); |
| 84 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 85 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 86 | protected PacketService packetService; |
| 87 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 88 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 89 | protected InterfaceService interfaceService; |
| 90 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 91 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 92 | protected HostService hostService; |
| 93 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 94 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 95 | protected CoreService coreService; |
| 96 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 97 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 98 | protected ComponentConfigService componentConfigService; |
| 99 | |
Ray Milkey | 4694e06 | 2018-10-31 13:17:18 -0700 | [diff] [blame] | 100 | /** Enable reactive directly-connected host processing. */ |
| 101 | private volatile boolean enabled = ENABLED_DEFAULT; |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 102 | |
| 103 | private static final String APP_NAME = "org.onosproject.directhost"; |
| 104 | |
| 105 | private static final long MAX_QUEUED_PACKETS = 10000; |
| 106 | private static final long MAX_QUEUE_DURATION = 2; // seconds |
| 107 | |
| 108 | private ApplicationId appId; |
| 109 | |
| 110 | private InternalPacketProcessor packetProcessor = new InternalPacketProcessor(); |
| 111 | private InternalHostListener hostListener = new InternalHostListener(); |
| 112 | |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 113 | private Cache<IpAddress, Queue<IP>> ipPacketCache = CacheBuilder.newBuilder() |
| 114 | .weigher((IpAddress key, Queue<IP> value) -> value.size()) |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 115 | .maximumWeight(MAX_QUEUED_PACKETS) |
| 116 | .expireAfterAccess(MAX_QUEUE_DURATION, TimeUnit.SECONDS) |
| 117 | .build(); |
| 118 | |
| 119 | @Activate |
| 120 | public void activate(ComponentContext context) { |
| 121 | componentConfigService.registerProperties(getClass()); |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 122 | appId = coreService.registerApplication(APP_NAME); |
Charles Chan | 00d8b5f | 2016-12-04 17:17:39 -0800 | [diff] [blame] | 123 | modified(context); |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | @Modified |
| 127 | private void modified(ComponentContext context) { |
Ray Milkey | 4694e06 | 2018-10-31 13:17:18 -0700 | [diff] [blame] | 128 | Boolean boolEnabled = Tools.isPropertyEnabled(context.getProperties(), ENABLED); |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 129 | if (boolEnabled != null) { |
| 130 | if (enabled && !boolEnabled) { |
| 131 | enabled = false; |
| 132 | disable(); |
| 133 | } else if (!enabled && boolEnabled) { |
| 134 | enabled = true; |
| 135 | enable(); |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | private void enable() { |
| 141 | hostService.addListener(hostListener); |
| 142 | packetService.addProcessor(packetProcessor, PacketProcessor.director(3)); |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 143 | // Requests packets for IPv4 traffic. |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 144 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 145 | .matchEthType(EthType.EtherType.IPV4.ethType().toShort()).build(); |
| 146 | packetService.requestPackets(selector, PacketPriority.REACTIVE, appId, Optional.empty()); |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 147 | // Requests packets for IPv6 traffic. |
| 148 | selector = DefaultTrafficSelector.builder() |
| 149 | .matchEthType(EthType.EtherType.IPV6.ethType().toShort()).build(); |
| 150 | packetService.requestPackets(selector, PacketPriority.REACTIVE, appId, Optional.empty()); |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | private void disable() { |
| 154 | packetService.removeProcessor(packetProcessor); |
| 155 | hostService.removeListener(hostListener); |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 156 | // Withdraws IPv4 request. |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 157 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 158 | .matchEthType(EthType.EtherType.IPV4.ethType().toShort()).build(); |
| 159 | packetService.cancelPackets(selector, PacketPriority.REACTIVE, appId, Optional.empty()); |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 160 | // Withdraws IPv6 request. |
| 161 | selector = DefaultTrafficSelector.builder() |
| 162 | .matchEthType(EthType.EtherType.IPV6.ethType().toShort()).build(); |
| 163 | packetService.cancelPackets(selector, PacketPriority.REACTIVE, appId, Optional.empty()); |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | @Deactivate |
| 167 | public void deactivate() { |
Charles Chan | 00d8b5f | 2016-12-04 17:17:39 -0800 | [diff] [blame] | 168 | if (enabled) { |
| 169 | disable(); |
| 170 | } |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 171 | |
| 172 | componentConfigService.unregisterProperties(getClass(), false); |
| 173 | } |
| 174 | |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 175 | private boolean handle(Ethernet eth) { |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 176 | checkNotNull(eth); |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 177 | // If the DirectHostManager is not enabled and the |
| 178 | // packets are different from what we expect just |
| 179 | // skip them. |
| 180 | if (!enabled || (eth.getEtherType() != Ethernet.TYPE_IPV6 |
| 181 | && eth.getEtherType() != Ethernet.TYPE_IPV4)) { |
| 182 | return false; |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 183 | } |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 184 | // According to the type we set the destIp. |
| 185 | IpAddress dstIp; |
| 186 | if (eth.getEtherType() == Ethernet.TYPE_IPV4) { |
| 187 | IPv4 ip = (IPv4) eth.getPayload(); |
| 188 | dstIp = IpAddress.valueOf(ip.getDestinationAddress()); |
| 189 | } else { |
| 190 | IPv6 ip = (IPv6) eth.getPayload(); |
| 191 | dstIp = IpAddress.valueOf(INET6, ip.getDestinationAddress()); |
| 192 | } |
| 193 | // Looking for a candidate output port. |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 194 | Interface egressInterface = interfaceService.getMatchingInterface(dstIp); |
| 195 | |
| 196 | if (egressInterface == null) { |
| 197 | log.info("No egress interface found for {}", dstIp); |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 198 | return false; |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 199 | } |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 200 | // Looking for the destination mac. |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 201 | Optional<Host> host = hostService.getHostsByIp(dstIp).stream() |
| 202 | .filter(h -> h.location().equals(egressInterface.connectPoint())) |
| 203 | .filter(h -> h.vlan().equals(egressInterface.vlan())) |
| 204 | .findAny(); |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 205 | // If we don't have a destination we start the monitoring |
| 206 | // and we queue the packets waiting for a destination. |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 207 | if (host.isPresent()) { |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 208 | transformAndSend( |
| 209 | (IP) eth.getPayload(), |
| 210 | eth.getEtherType(), |
| 211 | egressInterface, |
| 212 | host.get().mac() |
| 213 | ); |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 214 | } else { |
| 215 | hostService.startMonitoringIp(dstIp); |
| 216 | ipPacketCache.asMap().compute(dstIp, (ip, queue) -> { |
| 217 | if (queue == null) { |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 218 | queue = new ConcurrentLinkedQueue<>(); |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 219 | } |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 220 | queue.add((IP) eth.getPayload()); |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 221 | return queue; |
| 222 | }); |
| 223 | } |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 224 | |
| 225 | return true; |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 228 | private void transformAndSend(IP ip, short ethType, |
| 229 | Interface egressInterface, |
| 230 | MacAddress macAddress) { |
| 231 | // Base processing for IPv4 |
| 232 | if (ethType == Ethernet.TYPE_IPV4) { |
| 233 | IPv4 ipv4 = (IPv4) ip; |
| 234 | ipv4.setTtl((byte) (ipv4.getTtl() - 1)); |
| 235 | ipv4.setChecksum((short) 0); |
| 236 | // Base processing for IPv6. |
| 237 | } else { |
| 238 | IPv6 ipv6 = (IPv6) ip; |
| 239 | ipv6.setHopLimit((byte) (ipv6.getHopLimit() - 1)); |
| 240 | ipv6.resetChecksum(); |
| 241 | } |
| 242 | // Sends and serializes. |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 243 | Ethernet eth = new Ethernet(); |
| 244 | eth.setDestinationMACAddress(macAddress); |
| 245 | eth.setSourceMACAddress(egressInterface.mac()); |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 246 | eth.setEtherType(ethType); |
| 247 | eth.setPayload(ip); |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 248 | if (!egressInterface.vlan().equals(VlanId.NONE)) { |
| 249 | eth.setVlanID(egressInterface.vlan().toShort()); |
| 250 | } |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 251 | send(eth, egressInterface.connectPoint()); |
| 252 | } |
| 253 | |
| 254 | private void send(Ethernet eth, ConnectPoint cp) { |
| 255 | OutboundPacket packet = new DefaultOutboundPacket(cp.deviceId(), |
| 256 | DefaultTrafficTreatment.builder().setOutput(cp.port()).build(), ByteBuffer.wrap(eth.serialize())); |
| 257 | packetService.emit(packet); |
| 258 | } |
| 259 | |
| 260 | private void sendQueued(IpAddress ipAddress, MacAddress macAddress) { |
| 261 | log.debug("Sending queued packets for {} ({})", ipAddress, macAddress); |
| 262 | ipPacketCache.asMap().computeIfPresent(ipAddress, (ip, packets) -> { |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 263 | packets.forEach(ipPackets -> { |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 264 | Interface egressInterface = interfaceService.getMatchingInterface(ipAddress); |
| 265 | |
| 266 | if (egressInterface == null) { |
| 267 | log.info("No egress interface found for {}", ipAddress); |
| 268 | return; |
| 269 | } |
| 270 | |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 271 | // According to the type of the address we set proper |
| 272 | // protocol. |
| 273 | transformAndSend( |
| 274 | ipPackets, |
| 275 | ipAddress.isIp4() ? Ethernet.TYPE_IPV4 : Ethernet.TYPE_IPV6, |
| 276 | egressInterface, |
| 277 | macAddress |
| 278 | ); |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 279 | }); |
| 280 | return null; |
| 281 | }); |
| 282 | } |
| 283 | |
| 284 | private class InternalPacketProcessor implements PacketProcessor { |
| 285 | |
| 286 | @Override |
| 287 | public void process(PacketContext context) { |
| 288 | if (context.isHandled()) { |
| 289 | return; |
| 290 | } |
| 291 | |
| 292 | if (interfaceService.getInterfacesByPort(context.inPacket().receivedFrom()).isEmpty()) { |
| 293 | // Don't handle packets that don't come from one of our configured interfaces |
| 294 | return; |
| 295 | } |
| 296 | |
| 297 | Ethernet eth = context.inPacket().parsed(); |
| 298 | if (eth == null) { |
| 299 | return; |
| 300 | } |
| 301 | |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 302 | if (!handle(eth)) { |
| 303 | return; |
| 304 | } |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 305 | |
| 306 | context.block(); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | private class InternalHostListener implements HostListener { |
| 311 | @Override |
| 312 | public void event(HostEvent event) { |
| 313 | switch (event.type()) { |
| 314 | case HOST_ADDED: |
| 315 | event.subject().ipAddresses().forEach(ip -> |
| 316 | DirectHostManager.this.sendQueued(ip, event.subject().mac())); |
| 317 | break; |
| 318 | case HOST_REMOVED: |
| 319 | case HOST_UPDATED: |
| 320 | case HOST_MOVED: |
| 321 | default: |
| 322 | break; |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | } |