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