blob: 80b233b9ee3b7b55545737479bfc1a2c811bf1dd [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;
Pier Ventreb6a7f342016-11-26 21:05:22 -080025import org.onosproject.incubator.net.neighbour.NeighbourMessageContext;
sangho80f11cb2015-04-01 13:05:26 -070026import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.DeviceId;
28import org.onosproject.net.flow.DefaultTrafficTreatment;
29import org.onosproject.net.flow.TrafficTreatment;
30import org.onosproject.net.packet.DefaultOutboundPacket;
31import org.onosproject.net.packet.InboundPacket;
32import org.onosproject.net.packet.OutboundPacket;
Charles Chan319d1a22015-11-03 10:42:14 -080033import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
34import org.onosproject.segmentrouting.config.DeviceConfiguration;
sangho80f11cb2015-04-01 13:05:26 -070035import org.slf4j.Logger;
36import org.slf4j.LoggerFactory;
37
Jonathan Hartd53ebc42015-04-07 16:46:33 -070038import java.nio.ByteBuffer;
Saurav Dasc28b3432015-10-30 17:45:38 -070039import java.util.Set;
Jonathan Hartd53ebc42015-04-07 16:46:33 -070040
sangho80f11cb2015-04-01 13:05:26 -070041import static com.google.common.base.Preconditions.checkNotNull;
42
Charles Chanb7f75ac2016-01-11 18:28:54 -080043/**
44 * Handler of ICMP packets that responses or forwards ICMP packets that
45 * are sent to the controller.
46 */
sangho80f11cb2015-04-01 13:05:26 -070047public class IcmpHandler {
48
49 private static Logger log = LoggerFactory.getLogger(IcmpHandler.class);
50 private SegmentRoutingManager srManager;
sangho9b169e32015-04-14 16:27:13 -070051 private DeviceConfiguration config;
sangho80f11cb2015-04-01 13:05:26 -070052
53 /**
54 * Creates an IcmpHandler object.
55 *
56 * @param srManager SegmentRoutingManager object
57 */
58 public IcmpHandler(SegmentRoutingManager srManager) {
59 this.srManager = srManager;
sangho9b169e32015-04-14 16:27:13 -070060 this.config = checkNotNull(srManager.deviceConfiguration);
sangho80f11cb2015-04-01 13:05:26 -070061 }
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 Vachuska8a075092015-04-15 18:20:08 -070070 * @param pkt inbound packet
sangho80f11cb2015-04-01 13:05:26 -070071 */
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 Ventreb6a7f342016-11-26 21:05:22 -080081 Set<IpAddress> gatewayIpAddresses = config.getPortIPs(deviceId);
Pier Ventreadb4ae62016-11-23 09:57:42 -080082 IpAddress routerIp;
Charles Chan319d1a22015-11-03 10:42:14 -080083 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -080084 routerIp = config.getRouterIpv4(deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -080085 } catch (DeviceConfigNotFoundException e) {
86 log.warn(e.getMessage() + " Aborting processPacketIn.");
87 return;
88 }
sangho9b169e32015-04-14 16:27:13 -070089 IpPrefix routerIpPrefix = IpPrefix.valueOf(routerIp, IpPrefix.MAX_INET_MASK_LENGTH);
sangho80f11cb2015-04-01 13:05:26 -070090 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 Vavilapalli37a461b2015-04-07 15:12:32 -070095 gatewayIpAddresses.contains(destinationAddress))) {
Pier Ventreadb4ae62016-11-23 09:57:42 -080096 sendIcmpResponse(ethernet, connectPoint);
sangho80f11cb2015-04-01 13:05:26 -070097
98 // ICMP for any known host
99 } else if (!srManager.hostService.getHostsByIp(destinationAddress).isEmpty()) {
Saurav Das2d94d312015-11-24 23:21:05 -0800100 // TODO: known host packet should not be coming to controller - resend flows?
sangho80f11cb2015-04-01 13:05:26 -0700101 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 Chanf4586112015-11-09 16:37:23 -0800114 /**
Pier Ventreb6a7f342016-11-26 21:05:22 -0800115 * 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 Chanf4586112015-11-09 16:37:23 -0800128 * 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 Ventreadb4ae62016-11-23 09:57:42 -0800135 private void sendIcmpResponse(Ethernet icmpRequest, ConnectPoint outport) {
Charles Chanf4586112015-11-09 16:37:23 -0800136 // Note: We assume that packets arrive at the edge switches have
137 // untagged VLAN.
sangho80f11cb2015-04-01 13:05:26 -0700138 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 Hartd53ebc42015-04-07 16:46:33 -0700149 ICMP icmpReply = new ICMP();
Saurav Dasa5bb7cb2015-09-30 10:00:49 -0700150 icmpReply.setPayload(((ICMP) icmpRequestIpv4.getPayload()).getPayload());
sangho80f11cb2015-04-01 13:05:26 -0700151 icmpReply.setIcmpType(ICMP.TYPE_ECHO_REPLY);
152 icmpReply.setIcmpCode(ICMP.SUBTYPE_ECHO_REPLY);
153 icmpReply.setChecksum((short) 0);
sangho80f11cb2015-04-01 13:05:26 -0700154 icmpReplyIpv4.setPayload(icmpReply);
155
156 icmpReplyEth.setPayload(icmpReplyIpv4);
157 icmpReplyEth.setEtherType(Ethernet.TYPE_IPV4);
158 icmpReplyEth.setDestinationMACAddress(icmpRequest.getSourceMACAddress());
159 icmpReplyEth.setSourceMACAddress(icmpRequest.getDestinationMACAddress());
sangho80f11cb2015-04-01 13:05:26 -0700160
161 Ip4Address destIpAddress = Ip4Address.valueOf(icmpReplyIpv4.getDestinationAddress());
sangho9b169e32015-04-14 16:27:13 -0700162 Ip4Address destRouterAddress = config.getRouterIpAddressForASubnetHost(destIpAddress);
Pier Ventreadb4ae62016-11-23 09:57:42 -0800163 int destSid = config.getIPv4SegmentId(destRouterAddress);
Charles Chan70661362016-12-09 12:54:49 -0800164 if (destSid < 0) {
sangho80f11cb2015-04-01 13:05:26 -0700165 log.warn("Cannot find the Segment ID for {}", destAddress);
166 return;
167 }
168
Charles Chan70661362016-12-09 12:54:49 -0800169 sendPacketOut(outport, icmpReplyEth, destSid);
sangho80f11cb2015-04-01 13:05:26 -0700170
171 }
172
Charles Chan70661362016-12-09 12:54:49 -0800173 private void sendPacketOut(ConnectPoint outport, Ethernet payload, int destSid) {
sangho80f11cb2015-04-01 13:05:26 -0700174
175 IPv4 ipPacket = (IPv4) payload.getPayload();
176 Ip4Address destIpAddress = Ip4Address.valueOf(ipPacket.getDestinationAddress());
177
Pier Ventreadb4ae62016-11-23 09:57:42 -0800178 if (destSid == -1 || config.getIPv4SegmentId(payload.getDestinationMAC()) == destSid ||
sangho80f11cb2015-04-01 13:05:26 -0700179 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 Dasd44e8802016-10-21 14:06:29 -0700186 log.debug("Send a MPLS packet as a ICMP response");
sangho80f11cb2015-04-01 13:05:26 -0700187 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
188 .setOutput(outport.port())
189 .build();
190
191 payload.setEtherType(Ethernet.MPLS_UNICAST);
192 MPLS mplsPkt = new MPLS();
Charles Chan70661362016-12-09 12:54:49 -0800193 mplsPkt.setLabel(destSid);
sangho80f11cb2015-04-01 13:05:26 -0700194 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 }
sangho9b169e32015-04-14 16:27:13 -0700204
205
sangho80f11cb2015-04-01 13:05:26 -0700206}