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 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 18 | import org.onlab.packet.Ethernet; |
| 19 | import org.onlab.packet.ICMP; |
| 20 | import org.onlab.packet.IPv4; |
| 21 | import org.onlab.packet.Ip4Address; |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 22 | import org.onlab.packet.IpAddress; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 23 | import org.onlab.packet.IpPrefix; |
| 24 | import org.onlab.packet.MPLS; |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame^] | 25 | import org.onosproject.incubator.net.neighbour.NeighbourMessageContext; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 26 | import org.onosproject.net.ConnectPoint; |
| 27 | import org.onosproject.net.DeviceId; |
| 28 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 29 | import org.onosproject.net.flow.TrafficTreatment; |
| 30 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 31 | import org.onosproject.net.packet.InboundPacket; |
| 32 | import org.onosproject.net.packet.OutboundPacket; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 33 | import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException; |
| 34 | import org.onosproject.segmentrouting.config.DeviceConfiguration; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 35 | import org.slf4j.Logger; |
| 36 | import org.slf4j.LoggerFactory; |
| 37 | |
Jonathan Hart | d53ebc4 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 38 | import java.nio.ByteBuffer; |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 39 | import java.util.Set; |
Jonathan Hart | d53ebc4 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 40 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 41 | import static com.google.common.base.Preconditions.checkNotNull; |
| 42 | |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 43 | /** |
| 44 | * Handler of ICMP packets that responses or forwards ICMP packets that |
| 45 | * are sent to the controller. |
| 46 | */ |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 47 | public class IcmpHandler { |
| 48 | |
| 49 | private static Logger log = LoggerFactory.getLogger(IcmpHandler.class); |
| 50 | private SegmentRoutingManager srManager; |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 51 | private DeviceConfiguration config; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * Creates an IcmpHandler object. |
| 55 | * |
| 56 | * @param srManager SegmentRoutingManager object |
| 57 | */ |
| 58 | public IcmpHandler(SegmentRoutingManager srManager) { |
| 59 | this.srManager = srManager; |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 60 | this.config = checkNotNull(srManager.deviceConfiguration); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Process incoming ICMP packet. |
| 65 | * If it is an ICMP request to router or known host, then sends an ICMP response. |
| 66 | * If it is an ICMP packet to known host and forward the packet to the host. |
| 67 | * If it is an ICMP packet to unknown host in a subnet, then sends an ARP request |
| 68 | * to the subnet. |
| 69 | * |
Thomas Vachuska | 8a07509 | 2015-04-15 18:20:08 -0700 | [diff] [blame] | 70 | * @param pkt inbound packet |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 71 | */ |
| 72 | public void processPacketIn(InboundPacket pkt) { |
| 73 | |
| 74 | Ethernet ethernet = pkt.parsed(); |
| 75 | IPv4 ipv4 = (IPv4) ethernet.getPayload(); |
| 76 | |
| 77 | ConnectPoint connectPoint = pkt.receivedFrom(); |
| 78 | DeviceId deviceId = connectPoint.deviceId(); |
| 79 | Ip4Address destinationAddress = |
| 80 | Ip4Address.valueOf(ipv4.getDestinationAddress()); |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame^] | 81 | Set<IpAddress> gatewayIpAddresses = config.getPortIPs(deviceId); |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 82 | IpAddress routerIp; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 83 | try { |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 84 | routerIp = config.getRouterIpv4(deviceId); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 85 | } catch (DeviceConfigNotFoundException e) { |
| 86 | log.warn(e.getMessage() + " Aborting processPacketIn."); |
| 87 | return; |
| 88 | } |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 89 | IpPrefix routerIpPrefix = IpPrefix.valueOf(routerIp, IpPrefix.MAX_INET_MASK_LENGTH); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 90 | Ip4Address routerIpAddress = routerIpPrefix.getIp4Prefix().address(); |
| 91 | |
| 92 | // ICMP to the router IP or gateway IP |
| 93 | if (((ICMP) ipv4.getPayload()).getIcmpType() == ICMP.TYPE_ECHO_REQUEST && |
| 94 | (destinationAddress.equals(routerIpAddress) || |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 95 | gatewayIpAddresses.contains(destinationAddress))) { |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 96 | sendIcmpResponse(ethernet, connectPoint); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 97 | |
| 98 | // ICMP for any known host |
| 99 | } else if (!srManager.hostService.getHostsByIp(destinationAddress).isEmpty()) { |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 100 | // TODO: known host packet should not be coming to controller - resend flows? |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 101 | srManager.ipHandler.forwardPackets(deviceId, destinationAddress); |
| 102 | |
| 103 | // ICMP for an unknown host in the subnet of the router |
| 104 | } else if (config.inSameSubnet(deviceId, destinationAddress)) { |
| 105 | srManager.arpHandler.sendArpRequest(deviceId, destinationAddress, connectPoint); |
| 106 | |
| 107 | // ICMP for an unknown host |
| 108 | } else { |
| 109 | log.debug("ICMP request for unknown host {} ", destinationAddress); |
| 110 | // Do nothing |
| 111 | } |
| 112 | } |
| 113 | |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 114 | /** |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame^] | 115 | * Process incoming ICMP packet. |
| 116 | * If it is an ICMP request to router or known host, then sends an ICMP response. |
| 117 | * If it is an ICMP packet to known host and forward the packet to the host. |
| 118 | * If it is an ICMP packet to unknown host in a subnet, then sends an ARP request |
| 119 | * to the subnet. |
| 120 | * |
| 121 | * @param pkt inbound packet |
| 122 | */ |
| 123 | public void processPacketIn(NeighbourMessageContext pkt) { |
| 124 | |
| 125 | } |
| 126 | |
| 127 | /** |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 128 | * Sends an ICMP reply message. |
| 129 | * |
| 130 | * Note: we assume that packets sending from the edge switches to the hosts |
| 131 | * have untagged VLAN. |
| 132 | * @param icmpRequest the original ICMP request |
| 133 | * @param outport the output port where the ICMP reply should be sent to |
| 134 | */ |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 135 | private void sendIcmpResponse(Ethernet icmpRequest, ConnectPoint outport) { |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 136 | // Note: We assume that packets arrive at the edge switches have |
| 137 | // untagged VLAN. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 138 | Ethernet icmpReplyEth = new Ethernet(); |
| 139 | |
| 140 | IPv4 icmpRequestIpv4 = (IPv4) icmpRequest.getPayload(); |
| 141 | IPv4 icmpReplyIpv4 = new IPv4(); |
| 142 | |
| 143 | int destAddress = icmpRequestIpv4.getDestinationAddress(); |
| 144 | icmpReplyIpv4.setDestinationAddress(icmpRequestIpv4.getSourceAddress()); |
| 145 | icmpReplyIpv4.setSourceAddress(destAddress); |
| 146 | icmpReplyIpv4.setTtl((byte) 64); |
| 147 | icmpReplyIpv4.setChecksum((short) 0); |
| 148 | |
Jonathan Hart | d53ebc4 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 149 | ICMP icmpReply = new ICMP(); |
Saurav Das | a5bb7cb | 2015-09-30 10:00:49 -0700 | [diff] [blame] | 150 | icmpReply.setPayload(((ICMP) icmpRequestIpv4.getPayload()).getPayload()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 151 | icmpReply.setIcmpType(ICMP.TYPE_ECHO_REPLY); |
| 152 | icmpReply.setIcmpCode(ICMP.SUBTYPE_ECHO_REPLY); |
| 153 | icmpReply.setChecksum((short) 0); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 154 | icmpReplyIpv4.setPayload(icmpReply); |
| 155 | |
| 156 | icmpReplyEth.setPayload(icmpReplyIpv4); |
| 157 | icmpReplyEth.setEtherType(Ethernet.TYPE_IPV4); |
| 158 | icmpReplyEth.setDestinationMACAddress(icmpRequest.getSourceMACAddress()); |
| 159 | icmpReplyEth.setSourceMACAddress(icmpRequest.getDestinationMACAddress()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 160 | |
| 161 | Ip4Address destIpAddress = Ip4Address.valueOf(icmpReplyIpv4.getDestinationAddress()); |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 162 | Ip4Address destRouterAddress = config.getRouterIpAddressForASubnetHost(destIpAddress); |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 163 | int destSid = config.getIPv4SegmentId(destRouterAddress); |
Charles Chan | 7066136 | 2016-12-09 12:54:49 -0800 | [diff] [blame] | 164 | if (destSid < 0) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 165 | log.warn("Cannot find the Segment ID for {}", destAddress); |
| 166 | return; |
| 167 | } |
| 168 | |
Charles Chan | 7066136 | 2016-12-09 12:54:49 -0800 | [diff] [blame] | 169 | sendPacketOut(outport, icmpReplyEth, destSid); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 170 | |
| 171 | } |
| 172 | |
Charles Chan | 7066136 | 2016-12-09 12:54:49 -0800 | [diff] [blame] | 173 | private void sendPacketOut(ConnectPoint outport, Ethernet payload, int destSid) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 174 | |
| 175 | IPv4 ipPacket = (IPv4) payload.getPayload(); |
| 176 | Ip4Address destIpAddress = Ip4Address.valueOf(ipPacket.getDestinationAddress()); |
| 177 | |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 178 | if (destSid == -1 || config.getIPv4SegmentId(payload.getDestinationMAC()) == destSid || |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 179 | config.inSameSubnet(outport.deviceId(), destIpAddress)) { |
| 180 | TrafficTreatment treatment = DefaultTrafficTreatment.builder(). |
| 181 | setOutput(outport.port()).build(); |
| 182 | OutboundPacket packet = new DefaultOutboundPacket(outport.deviceId(), |
| 183 | treatment, ByteBuffer.wrap(payload.serialize())); |
| 184 | srManager.packetService.emit(packet); |
| 185 | } else { |
Saurav Das | d44e880 | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 186 | log.debug("Send a MPLS packet as a ICMP response"); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 187 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 188 | .setOutput(outport.port()) |
| 189 | .build(); |
| 190 | |
| 191 | payload.setEtherType(Ethernet.MPLS_UNICAST); |
| 192 | MPLS mplsPkt = new MPLS(); |
Charles Chan | 7066136 | 2016-12-09 12:54:49 -0800 | [diff] [blame] | 193 | mplsPkt.setLabel(destSid); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 194 | mplsPkt.setTtl(((IPv4) payload.getPayload()).getTtl()); |
| 195 | mplsPkt.setPayload(payload.getPayload()); |
| 196 | payload.setPayload(mplsPkt); |
| 197 | |
| 198 | OutboundPacket packet = new DefaultOutboundPacket(outport.deviceId(), |
| 199 | treatment, ByteBuffer.wrap(payload.serialize())); |
| 200 | |
| 201 | srManager.packetService.emit(packet); |
| 202 | } |
| 203 | } |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 204 | |
| 205 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 206 | } |