sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 43b5354 | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
sangho | 80f11cb | 2015-04-01 13:05:26 -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 | package org.onosproject.segmentrouting; |
| 17 | |
| 18 | import org.onlab.packet.Ethernet; |
| 19 | import org.onlab.packet.IPv4; |
| 20 | import org.onlab.packet.Ip4Address; |
| 21 | import org.onosproject.net.ConnectPoint; |
| 22 | import org.onosproject.net.DeviceId; |
| 23 | import org.onosproject.net.Host; |
| 24 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 25 | import org.onosproject.net.flow.TrafficTreatment; |
| 26 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 27 | import org.onosproject.net.packet.InboundPacket; |
| 28 | import org.onosproject.net.packet.OutboundPacket; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 29 | import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException; |
| 30 | import org.onosproject.segmentrouting.config.DeviceConfiguration; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 31 | import org.slf4j.Logger; |
| 32 | import org.slf4j.LoggerFactory; |
| 33 | |
| 34 | import java.nio.ByteBuffer; |
| 35 | import java.util.concurrent.ConcurrentHashMap; |
| 36 | import java.util.concurrent.ConcurrentLinkedQueue; |
| 37 | |
| 38 | import static com.google.common.base.Preconditions.checkNotNull; |
| 39 | |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 40 | /** |
| 41 | * Handler of IP packets that forwards IP packets that are sent to the controller, |
| 42 | * except the ICMP packets which are processed by @link{IcmpHandler}. |
| 43 | */ |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 44 | public class IpHandler { |
| 45 | |
| 46 | private static Logger log = LoggerFactory.getLogger(IpHandler.class); |
| 47 | private SegmentRoutingManager srManager; |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 48 | private DeviceConfiguration config; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 49 | private ConcurrentHashMap<Ip4Address, ConcurrentLinkedQueue<IPv4>> ipPacketQueue; |
| 50 | |
| 51 | /** |
| 52 | * Creates an IpHandler object. |
| 53 | * |
| 54 | * @param srManager SegmentRoutingManager object |
| 55 | */ |
| 56 | public IpHandler(SegmentRoutingManager srManager) { |
| 57 | this.srManager = srManager; |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 58 | this.config = checkNotNull(srManager.deviceConfiguration); |
Sho SHIMIZU | 47b8aa2 | 2015-09-11 11:19:11 -0700 | [diff] [blame] | 59 | ipPacketQueue = new ConcurrentHashMap<>(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Processes incoming IP packets. |
| 64 | * |
| 65 | * If it is an IP packet for known host, then forward it to the host. |
| 66 | * If it is an IP packet for unknown host in subnet, then send an ARP request |
| 67 | * to the subnet. |
| 68 | * |
| 69 | * @param pkt incoming packet |
| 70 | */ |
| 71 | public void processPacketIn(InboundPacket pkt) { |
| 72 | Ethernet ethernet = pkt.parsed(); |
| 73 | IPv4 ipv4 = (IPv4) ethernet.getPayload(); |
| 74 | |
| 75 | ConnectPoint connectPoint = pkt.receivedFrom(); |
| 76 | DeviceId deviceId = connectPoint.deviceId(); |
| 77 | Ip4Address destinationAddress = |
| 78 | Ip4Address.valueOf(ipv4.getDestinationAddress()); |
| 79 | |
| 80 | // IP packet for know hosts |
| 81 | if (!srManager.hostService.getHostsByIp(destinationAddress).isEmpty()) { |
| 82 | forwardPackets(deviceId, destinationAddress); |
| 83 | |
| 84 | // IP packet for unknown host in the subnet of the router |
| 85 | } else if (config.inSameSubnet(deviceId, destinationAddress)) { |
| 86 | srManager.arpHandler.sendArpRequest(deviceId, destinationAddress, connectPoint); |
| 87 | |
| 88 | // IP packets for unknown host |
| 89 | } else { |
| 90 | log.debug("ICMP request for unknown host {} which is not in the subnet", |
| 91 | destinationAddress); |
| 92 | // Do nothing |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Adds the IP packet to a buffer. |
| 98 | * The packets are forwarded to corresponding destination when the destination |
| 99 | * MAC address is known via ARP response. |
| 100 | * |
| 101 | * @param ipPacket IP packet to add to the buffer |
| 102 | */ |
| 103 | public void addToPacketBuffer(IPv4 ipPacket) { |
| 104 | |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 105 | // Better not buffer TCP packets due to out-of-order packet transfer |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 106 | if (ipPacket.getProtocol() == IPv4.PROTOCOL_TCP) { |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | Ip4Address destIpAddress = Ip4Address.valueOf(ipPacket.getDestinationAddress()); |
| 111 | |
Yuta HIGUCHI | 4491403 | 2016-12-23 13:39:08 -0800 | [diff] [blame] | 112 | ipPacketQueue |
| 113 | .computeIfAbsent(destIpAddress, a -> new ConcurrentLinkedQueue<>()) |
| 114 | .add(ipPacket); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Forwards IP packets in the buffer to the destination IP address. |
| 119 | * It is called when the controller finds the destination MAC address |
Saurav Das | 8897918 | 2015-10-19 14:37:36 -0700 | [diff] [blame] | 120 | * via ARP responses. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 121 | * |
| 122 | * @param deviceId switch device ID |
| 123 | * @param destIpAddress destination IP address |
| 124 | */ |
| 125 | public void forwardPackets(DeviceId deviceId, Ip4Address destIpAddress) { |
Srikanth Vavilapalli | c56a2f3 | 2015-06-05 11:40:14 -0700 | [diff] [blame] | 126 | if (ipPacketQueue.get(destIpAddress) == null) { |
| 127 | return; |
| 128 | } |
| 129 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 130 | for (IPv4 ipPacket : ipPacketQueue.get(destIpAddress)) { |
| 131 | Ip4Address destAddress = Ip4Address.valueOf(ipPacket.getDestinationAddress()); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 132 | if (config.inSameSubnet(deviceId, destAddress)) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 133 | ipPacket.setTtl((byte) (ipPacket.getTtl() - 1)); |
| 134 | ipPacket.setChecksum((short) 0); |
| 135 | for (Host dest: srManager.hostService.getHostsByIp(destIpAddress)) { |
| 136 | Ethernet eth = new Ethernet(); |
| 137 | eth.setDestinationMACAddress(dest.mac()); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 138 | try { |
| 139 | eth.setSourceMACAddress(config.getDeviceMac(deviceId)); |
| 140 | } catch (DeviceConfigNotFoundException e) { |
| 141 | log.warn(e.getMessage() |
| 142 | + " Skipping forwardPackets for this destination."); |
| 143 | continue; |
| 144 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 145 | eth.setEtherType(Ethernet.TYPE_IPV4); |
| 146 | eth.setPayload(ipPacket); |
| 147 | |
| 148 | TrafficTreatment treatment = DefaultTrafficTreatment.builder(). |
| 149 | setOutput(dest.location().port()).build(); |
| 150 | OutboundPacket packet = new DefaultOutboundPacket(deviceId, |
| 151 | treatment, ByteBuffer.wrap(eth.serialize())); |
| 152 | srManager.packetService.emit(packet); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 153 | ipPacketQueue.get(destIpAddress).remove(ipPacket); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 154 | } |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 155 | ipPacketQueue.get(destIpAddress).remove(ipPacket); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | } |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 159 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 160 | } |