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; |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 19 | import org.onlab.packet.IP; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 20 | import org.onlab.packet.IPv4; |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 21 | import org.onlab.packet.IPv6; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 22 | import org.onlab.packet.Ip4Address; |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 23 | import org.onlab.packet.Ip6Address; |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 24 | import org.onlab.packet.IpAddress; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 25 | import org.onosproject.net.ConnectPoint; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.net.Host; |
| 28 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 29 | import org.onosproject.net.flow.TrafficTreatment; |
| 30 | import org.onosproject.net.packet.DefaultOutboundPacket; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 31 | import org.onosproject.net.packet.OutboundPacket; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 32 | import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException; |
| 33 | import org.onosproject.segmentrouting.config.DeviceConfiguration; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 34 | import org.slf4j.Logger; |
| 35 | import org.slf4j.LoggerFactory; |
| 36 | |
| 37 | import java.nio.ByteBuffer; |
| 38 | import java.util.concurrent.ConcurrentHashMap; |
| 39 | import java.util.concurrent.ConcurrentLinkedQueue; |
| 40 | |
| 41 | import static com.google.common.base.Preconditions.checkNotNull; |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 42 | import static org.onlab.packet.IpAddress.Version.INET6; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 43 | |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 44 | /** |
| 45 | * Handler of IP packets that forwards IP packets that are sent to the controller, |
| 46 | * except the ICMP packets which are processed by @link{IcmpHandler}. |
| 47 | */ |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 48 | public class IpHandler { |
| 49 | |
| 50 | private static Logger log = LoggerFactory.getLogger(IpHandler.class); |
| 51 | private SegmentRoutingManager srManager; |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 52 | private DeviceConfiguration config; |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 53 | private ConcurrentHashMap<IpAddress, ConcurrentLinkedQueue<IP>> ipPacketQueue; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * Creates an IpHandler object. |
| 57 | * |
| 58 | * @param srManager SegmentRoutingManager object |
| 59 | */ |
| 60 | public IpHandler(SegmentRoutingManager srManager) { |
| 61 | this.srManager = srManager; |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 62 | this.config = checkNotNull(srManager.deviceConfiguration); |
Sho SHIMIZU | 47b8aa2 | 2015-09-11 11:19:11 -0700 | [diff] [blame] | 63 | ipPacketQueue = new ConcurrentHashMap<>(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /** |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 67 | * Enqueues the packet using the destination address as key. |
| 68 | * |
| 69 | * @param ipPacket the ip packet to store |
| 70 | * @param destinationAddress the destination address |
| 71 | */ |
| 72 | private void enqueuePacket(IP ipPacket, IpAddress destinationAddress) { |
| 73 | |
| 74 | ipPacketQueue |
| 75 | .computeIfAbsent(destinationAddress, a -> new ConcurrentLinkedQueue<>()) |
| 76 | .add(ipPacket); |
| 77 | |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Dequeues the packet using the destination address as key. |
| 82 | * |
| 83 | * @param ipPacket the ip packet to remove |
| 84 | * @param destinationAddress the destination address |
| 85 | */ |
| 86 | public void dequeuePacket(IP ipPacket, IpAddress destinationAddress) { |
| 87 | |
| 88 | if (ipPacketQueue.get(destinationAddress) == null) { |
| 89 | return; |
| 90 | } |
| 91 | ipPacketQueue.get(destinationAddress).remove(ipPacket); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Forwards the packet to a given host and deque the packet. |
| 96 | * |
| 97 | * @param deviceId the target device |
| 98 | * @param eth the packet to send |
| 99 | * @param dest the target host |
| 100 | */ |
| 101 | private void forwardToHost(DeviceId deviceId, Ethernet eth, Host dest) { |
| 102 | TrafficTreatment treatment = DefaultTrafficTreatment.builder(). |
| 103 | setOutput(dest.location().port()).build(); |
| 104 | OutboundPacket packet = new DefaultOutboundPacket(deviceId, |
| 105 | treatment, ByteBuffer.wrap(eth.serialize())); |
| 106 | srManager.packetService.emit(packet); |
| 107 | } |
| 108 | |
| 109 | ////////////////////// |
| 110 | // IPv4 Handling // |
| 111 | //////////////////// |
| 112 | |
| 113 | /** |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 114 | * Processes incoming IP packets. |
| 115 | * |
| 116 | * If it is an IP packet for known host, then forward it to the host. |
| 117 | * If it is an IP packet for unknown host in subnet, then send an ARP request |
| 118 | * to the subnet. |
| 119 | * |
| 120 | * @param pkt incoming packet |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 121 | * @param connectPoint the target device |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 122 | */ |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 123 | public void processPacketIn(IPv4 pkt, ConnectPoint connectPoint) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 124 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 125 | DeviceId deviceId = connectPoint.deviceId(); |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 126 | Ip4Address destinationAddress = Ip4Address.valueOf(pkt.getDestinationAddress()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 127 | |
| 128 | // IP packet for know hosts |
| 129 | if (!srManager.hostService.getHostsByIp(destinationAddress).isEmpty()) { |
| 130 | forwardPackets(deviceId, destinationAddress); |
| 131 | |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 132 | // IP packet for unknown host in one of the configured subnets of the router |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 133 | } else if (config.inSameSubnet(deviceId, destinationAddress)) { |
| 134 | srManager.arpHandler.sendArpRequest(deviceId, destinationAddress, connectPoint); |
| 135 | |
| 136 | // IP packets for unknown host |
| 137 | } else { |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 138 | log.debug("IPv4 packet for unknown host {} which is not in the subnet", |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 139 | destinationAddress); |
| 140 | // Do nothing |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Adds the IP packet to a buffer. |
| 146 | * The packets are forwarded to corresponding destination when the destination |
| 147 | * MAC address is known via ARP response. |
| 148 | * |
| 149 | * @param ipPacket IP packet to add to the buffer |
| 150 | */ |
| 151 | public void addToPacketBuffer(IPv4 ipPacket) { |
| 152 | |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 153 | // Better not buffer TCP packets due to out-of-order packet transfer |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 154 | if (ipPacket.getProtocol() == IPv4.PROTOCOL_TCP) { |
| 155 | return; |
| 156 | } |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 157 | IpAddress destIpAddress = IpAddress.valueOf(ipPacket.getDestinationAddress()); |
| 158 | enqueuePacket(ipPacket, destIpAddress); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Forwards IP packets in the buffer to the destination IP address. |
| 163 | * It is called when the controller finds the destination MAC address |
Saurav Das | 8897918 | 2015-10-19 14:37:36 -0700 | [diff] [blame] | 164 | * via ARP responses. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 165 | * |
| 166 | * @param deviceId switch device ID |
| 167 | * @param destIpAddress destination IP address |
| 168 | */ |
| 169 | public void forwardPackets(DeviceId deviceId, Ip4Address destIpAddress) { |
Srikanth Vavilapalli | c56a2f3 | 2015-06-05 11:40:14 -0700 | [diff] [blame] | 170 | if (ipPacketQueue.get(destIpAddress) == null) { |
| 171 | return; |
| 172 | } |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 173 | for (IP ipPacket : ipPacketQueue.get(destIpAddress)) { |
| 174 | if (ipPacket.getVersion() == ((byte) 4)) { |
| 175 | IPv4 ipv4Packet = (IPv4) ipPacket; |
| 176 | Ip4Address destAddress = Ip4Address.valueOf(ipv4Packet.getDestinationAddress()); |
| 177 | if (config.inSameSubnet(deviceId, destAddress)) { |
| 178 | ipv4Packet.setTtl((byte) (ipv4Packet.getTtl() - 1)); |
| 179 | ipv4Packet.setChecksum((short) 0); |
| 180 | for (Host dest : srManager.hostService.getHostsByIp(destIpAddress)) { |
| 181 | Ethernet eth = new Ethernet(); |
| 182 | eth.setDestinationMACAddress(dest.mac()); |
| 183 | try { |
| 184 | eth.setSourceMACAddress(config.getDeviceMac(deviceId)); |
| 185 | } catch (DeviceConfigNotFoundException e) { |
| 186 | log.warn(e.getMessage() |
| 187 | + " Skipping forwardPackets for this destination."); |
| 188 | continue; |
| 189 | } |
| 190 | eth.setEtherType(Ethernet.TYPE_IPV4); |
| 191 | eth.setPayload(ipv4Packet); |
| 192 | forwardToHost(deviceId, eth, dest); |
| 193 | ipPacketQueue.get(destIpAddress).remove(ipPacket); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 194 | } |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 195 | ipPacketQueue.get(destIpAddress).remove(ipPacket); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | } |
| 199 | } |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 200 | |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 201 | ////////////////////// |
| 202 | // IPv6 Handling // |
| 203 | //////////////////// |
| 204 | |
| 205 | /** |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 206 | * Processes incoming IPv6 packets. |
| 207 | * |
| 208 | * If it is an IPv6 packet for known host, then forward it to the host. |
| 209 | * If it is an IPv6 packet for unknown host in subnet, then send an NDP request |
| 210 | * to the subnet. |
| 211 | * |
| 212 | * @param pkt incoming packet |
| 213 | * @param connectPoint the target device |
| 214 | */ |
| 215 | public void processPacketIn(IPv6 pkt, ConnectPoint connectPoint) { |
| 216 | |
| 217 | DeviceId deviceId = connectPoint.deviceId(); |
| 218 | Ip6Address destinationAddress = Ip6Address.valueOf(pkt.getDestinationAddress()); |
| 219 | |
| 220 | // IPv6 packet for know hosts |
| 221 | if (!srManager.hostService.getHostsByIp(destinationAddress).isEmpty()) { |
| 222 | forwardPackets(deviceId, destinationAddress); |
| 223 | |
| 224 | // IPv6 packet for unknown host in one of the configured subnets of the router |
| 225 | } else if (config.inSameSubnet(deviceId, destinationAddress)) { |
| 226 | srManager.icmpHandler.sendNdpRequest(deviceId, destinationAddress, connectPoint); |
| 227 | |
| 228 | // IPv6 packets for unknown host |
| 229 | } else { |
| 230 | log.debug("IPv6 packet for unknown host {} which is not in the subnet", |
| 231 | destinationAddress); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Adds the IPv6 packet to a buffer. |
| 237 | * The packets are forwarded to corresponding destination when the destination |
| 238 | * MAC address is known via NDP response. |
| 239 | * |
| 240 | * @param ipPacket IP packet to add to the buffer |
| 241 | */ |
| 242 | public void addToPacketBuffer(IPv6 ipPacket) { |
| 243 | |
| 244 | // Better not buffer TCP packets due to out-of-order packet transfer |
| 245 | if (ipPacket.getNextHeader() == IPv6.PROTOCOL_TCP) { |
| 246 | return; |
| 247 | } |
| 248 | IpAddress destIpAddress = IpAddress.valueOf(INET6, ipPacket.getDestinationAddress()); |
| 249 | enqueuePacket(ipPacket, destIpAddress); |
| 250 | } |
| 251 | |
| 252 | /** |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 253 | * Forwards IP packets in the buffer to the destination IP address. |
| 254 | * It is called when the controller finds the destination MAC address |
| 255 | * via NDP replies. |
| 256 | * |
| 257 | * @param deviceId the target device |
| 258 | * @param destIpAddress the destination ip address |
| 259 | */ |
| 260 | public void forwardPackets(DeviceId deviceId, Ip6Address destIpAddress) { |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 261 | if (ipPacketQueue.get(destIpAddress) == null) { |
| 262 | return; |
| 263 | } |
| 264 | for (IP ipPacket : ipPacketQueue.get(destIpAddress)) { |
| 265 | if (ipPacket.getVersion() == ((byte) 6)) { |
| 266 | IPv6 ipv6Packet = (IPv6) ipPacket; |
| 267 | Ip6Address destAddress = Ip6Address.valueOf(ipv6Packet.getDestinationAddress()); |
| 268 | if (config.inSameSubnet(deviceId, destAddress)) { |
| 269 | ipv6Packet.setHopLimit((byte) (ipv6Packet.getHopLimit() - 1)); |
| 270 | for (Host dest : srManager.hostService.getHostsByIp(destIpAddress)) { |
| 271 | Ethernet eth = new Ethernet(); |
| 272 | eth.setDestinationMACAddress(dest.mac()); |
| 273 | try { |
| 274 | eth.setSourceMACAddress(config.getDeviceMac(deviceId)); |
| 275 | } catch (DeviceConfigNotFoundException e) { |
| 276 | log.warn(e.getMessage() |
| 277 | + " Skipping forwardPackets for this destination."); |
| 278 | continue; |
| 279 | } |
| 280 | eth.setEtherType(Ethernet.TYPE_IPV6); |
| 281 | eth.setPayload(ipv6Packet); |
| 282 | forwardToHost(deviceId, eth, dest); |
| 283 | ipPacketQueue.get(destIpAddress).remove(ipPacket); |
| 284 | } |
| 285 | ipPacketQueue.get(destIpAddress).remove(ipPacket); |
| 286 | } |
| 287 | } |
| 288 | ipPacketQueue.get(destIpAddress).remove(ipPacket); |
| 289 | } |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 290 | } |
| 291 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 292 | } |