blob: dc217ee8f384a73987564efdc13a26b3e04912ac [file] [log] [blame]
sangho80f11cb2015-04-01 13:05:26 -07001/*
Brian O'Connor43b53542016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
sangho80f11cb2015-04-01 13:05:26 -07003 *
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 */
16package org.onosproject.segmentrouting;
17
sangho80f11cb2015-04-01 13:05:26 -070018import org.onlab.packet.Ethernet;
19import org.onlab.packet.ICMP;
20import org.onlab.packet.IPv4;
21import org.onlab.packet.Ip4Address;
Pier Ventreadb4ae62016-11-23 09:57:42 -080022import org.onlab.packet.IpAddress;
sangho80f11cb2015-04-01 13:05:26 -070023import org.onlab.packet.IpPrefix;
24import org.onlab.packet.MPLS;
25import org.onosproject.net.ConnectPoint;
26import org.onosproject.net.DeviceId;
27import org.onosproject.net.flow.DefaultTrafficTreatment;
28import org.onosproject.net.flow.TrafficTreatment;
29import org.onosproject.net.packet.DefaultOutboundPacket;
30import org.onosproject.net.packet.InboundPacket;
31import org.onosproject.net.packet.OutboundPacket;
Charles Chan319d1a22015-11-03 10:42:14 -080032import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
33import org.onosproject.segmentrouting.config.DeviceConfiguration;
sangho80f11cb2015-04-01 13:05:26 -070034import org.slf4j.Logger;
35import org.slf4j.LoggerFactory;
36
Jonathan Hartd53ebc42015-04-07 16:46:33 -070037import java.nio.ByteBuffer;
Saurav Dasc28b3432015-10-30 17:45:38 -070038import java.util.Set;
Jonathan Hartd53ebc42015-04-07 16:46:33 -070039
sangho80f11cb2015-04-01 13:05:26 -070040import static com.google.common.base.Preconditions.checkNotNull;
41
Charles Chanb7f75ac2016-01-11 18:28:54 -080042/**
43 * Handler of ICMP packets that responses or forwards ICMP packets that
44 * are sent to the controller.
45 */
sangho80f11cb2015-04-01 13:05:26 -070046public class IcmpHandler {
47
48 private static Logger log = LoggerFactory.getLogger(IcmpHandler.class);
49 private SegmentRoutingManager srManager;
sangho9b169e32015-04-14 16:27:13 -070050 private DeviceConfiguration config;
sangho80f11cb2015-04-01 13:05:26 -070051
52 /**
53 * Creates an IcmpHandler object.
54 *
55 * @param srManager SegmentRoutingManager object
56 */
57 public IcmpHandler(SegmentRoutingManager srManager) {
58 this.srManager = srManager;
sangho9b169e32015-04-14 16:27:13 -070059 this.config = checkNotNull(srManager.deviceConfiguration);
sangho80f11cb2015-04-01 13:05:26 -070060 }
61
62 /**
63 * Process incoming ICMP packet.
64 * If it is an ICMP request to router or known host, then sends an ICMP response.
65 * If it is an ICMP packet to known host and forward the packet to the host.
66 * If it is an ICMP packet to unknown host in a subnet, then sends an ARP request
67 * to the subnet.
68 *
Thomas Vachuska8a075092015-04-15 18:20:08 -070069 * @param pkt inbound packet
sangho80f11cb2015-04-01 13:05:26 -070070 */
71 public void processPacketIn(InboundPacket pkt) {
72
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());
Saurav Dasc28b3432015-10-30 17:45:38 -070080 Set<Ip4Address> gatewayIpAddresses = config.getPortIPs(deviceId);
Pier Ventreadb4ae62016-11-23 09:57:42 -080081 IpAddress routerIp;
Charles Chan319d1a22015-11-03 10:42:14 -080082 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -080083 routerIp = config.getRouterIpv4(deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -080084 } catch (DeviceConfigNotFoundException e) {
85 log.warn(e.getMessage() + " Aborting processPacketIn.");
86 return;
87 }
sangho9b169e32015-04-14 16:27:13 -070088 IpPrefix routerIpPrefix = IpPrefix.valueOf(routerIp, IpPrefix.MAX_INET_MASK_LENGTH);
sangho80f11cb2015-04-01 13:05:26 -070089 Ip4Address routerIpAddress = routerIpPrefix.getIp4Prefix().address();
90
91 // ICMP to the router IP or gateway IP
92 if (((ICMP) ipv4.getPayload()).getIcmpType() == ICMP.TYPE_ECHO_REQUEST &&
93 (destinationAddress.equals(routerIpAddress) ||
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070094 gatewayIpAddresses.contains(destinationAddress))) {
Pier Ventreadb4ae62016-11-23 09:57:42 -080095 sendIcmpResponse(ethernet, connectPoint);
sangho80f11cb2015-04-01 13:05:26 -070096
97 // ICMP for any known host
98 } else if (!srManager.hostService.getHostsByIp(destinationAddress).isEmpty()) {
Saurav Das2d94d312015-11-24 23:21:05 -080099 // TODO: known host packet should not be coming to controller - resend flows?
sangho80f11cb2015-04-01 13:05:26 -0700100 srManager.ipHandler.forwardPackets(deviceId, destinationAddress);
101
102 // ICMP for an unknown host in the subnet of the router
103 } else if (config.inSameSubnet(deviceId, destinationAddress)) {
104 srManager.arpHandler.sendArpRequest(deviceId, destinationAddress, connectPoint);
105
106 // ICMP for an unknown host
107 } else {
108 log.debug("ICMP request for unknown host {} ", destinationAddress);
109 // Do nothing
110 }
111 }
112
Charles Chanf4586112015-11-09 16:37:23 -0800113 /**
114 * Sends an ICMP reply message.
115 *
116 * Note: we assume that packets sending from the edge switches to the hosts
117 * have untagged VLAN.
118 * @param icmpRequest the original ICMP request
119 * @param outport the output port where the ICMP reply should be sent to
120 */
Pier Ventreadb4ae62016-11-23 09:57:42 -0800121 private void sendIcmpResponse(Ethernet icmpRequest, ConnectPoint outport) {
Charles Chanf4586112015-11-09 16:37:23 -0800122 // Note: We assume that packets arrive at the edge switches have
123 // untagged VLAN.
sangho80f11cb2015-04-01 13:05:26 -0700124 Ethernet icmpReplyEth = new Ethernet();
125
126 IPv4 icmpRequestIpv4 = (IPv4) icmpRequest.getPayload();
127 IPv4 icmpReplyIpv4 = new IPv4();
128
129 int destAddress = icmpRequestIpv4.getDestinationAddress();
130 icmpReplyIpv4.setDestinationAddress(icmpRequestIpv4.getSourceAddress());
131 icmpReplyIpv4.setSourceAddress(destAddress);
132 icmpReplyIpv4.setTtl((byte) 64);
133 icmpReplyIpv4.setChecksum((short) 0);
134
Jonathan Hartd53ebc42015-04-07 16:46:33 -0700135 ICMP icmpReply = new ICMP();
Saurav Dasa5bb7cb2015-09-30 10:00:49 -0700136 icmpReply.setPayload(((ICMP) icmpRequestIpv4.getPayload()).getPayload());
sangho80f11cb2015-04-01 13:05:26 -0700137 icmpReply.setIcmpType(ICMP.TYPE_ECHO_REPLY);
138 icmpReply.setIcmpCode(ICMP.SUBTYPE_ECHO_REPLY);
139 icmpReply.setChecksum((short) 0);
sangho80f11cb2015-04-01 13:05:26 -0700140 icmpReplyIpv4.setPayload(icmpReply);
141
142 icmpReplyEth.setPayload(icmpReplyIpv4);
143 icmpReplyEth.setEtherType(Ethernet.TYPE_IPV4);
144 icmpReplyEth.setDestinationMACAddress(icmpRequest.getSourceMACAddress());
145 icmpReplyEth.setSourceMACAddress(icmpRequest.getDestinationMACAddress());
sangho80f11cb2015-04-01 13:05:26 -0700146
147 Ip4Address destIpAddress = Ip4Address.valueOf(icmpReplyIpv4.getDestinationAddress());
sangho9b169e32015-04-14 16:27:13 -0700148 Ip4Address destRouterAddress = config.getRouterIpAddressForASubnetHost(destIpAddress);
Pier Ventreadb4ae62016-11-23 09:57:42 -0800149 int destSid = config.getIPv4SegmentId(destRouterAddress);
Charles Chan70661362016-12-09 12:54:49 -0800150 if (destSid < 0) {
sangho80f11cb2015-04-01 13:05:26 -0700151 log.warn("Cannot find the Segment ID for {}", destAddress);
152 return;
153 }
154
Charles Chan70661362016-12-09 12:54:49 -0800155 sendPacketOut(outport, icmpReplyEth, destSid);
sangho80f11cb2015-04-01 13:05:26 -0700156
157 }
158
Charles Chan70661362016-12-09 12:54:49 -0800159 private void sendPacketOut(ConnectPoint outport, Ethernet payload, int destSid) {
sangho80f11cb2015-04-01 13:05:26 -0700160
161 IPv4 ipPacket = (IPv4) payload.getPayload();
162 Ip4Address destIpAddress = Ip4Address.valueOf(ipPacket.getDestinationAddress());
163
Pier Ventreadb4ae62016-11-23 09:57:42 -0800164 if (destSid == -1 || config.getIPv4SegmentId(payload.getDestinationMAC()) == destSid ||
sangho80f11cb2015-04-01 13:05:26 -0700165 config.inSameSubnet(outport.deviceId(), destIpAddress)) {
166 TrafficTreatment treatment = DefaultTrafficTreatment.builder().
167 setOutput(outport.port()).build();
168 OutboundPacket packet = new DefaultOutboundPacket(outport.deviceId(),
169 treatment, ByteBuffer.wrap(payload.serialize()));
170 srManager.packetService.emit(packet);
171 } else {
Saurav Dasd44e8802016-10-21 14:06:29 -0700172 log.debug("Send a MPLS packet as a ICMP response");
sangho80f11cb2015-04-01 13:05:26 -0700173 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
174 .setOutput(outport.port())
175 .build();
176
177 payload.setEtherType(Ethernet.MPLS_UNICAST);
178 MPLS mplsPkt = new MPLS();
Charles Chan70661362016-12-09 12:54:49 -0800179 mplsPkt.setLabel(destSid);
sangho80f11cb2015-04-01 13:05:26 -0700180 mplsPkt.setTtl(((IPv4) payload.getPayload()).getTtl());
181 mplsPkt.setPayload(payload.getPayload());
182 payload.setPayload(mplsPkt);
183
184 OutboundPacket packet = new DefaultOutboundPacket(outport.deviceId(),
185 treatment, ByteBuffer.wrap(payload.serialize()));
186
187 srManager.packetService.emit(packet);
188 }
189 }
sangho9b169e32015-04-14 16:27:13 -0700190
191
sangho80f11cb2015-04-01 13:05:26 -0700192}