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