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