blob: 14ce679bb1f426e3b3147c784d715b697de4e565 [file] [log] [blame]
sangho80f11cb2015-04-01 13:05:26 -07001/*
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 */
16package org.onosproject.segmentrouting;
17
18import org.onlab.packet.Ethernet;
19import org.onlab.packet.IPv4;
20import org.onlab.packet.Ip4Address;
21import org.onosproject.net.ConnectPoint;
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.Host;
24import org.onosproject.net.flow.DefaultTrafficTreatment;
25import org.onosproject.net.flow.TrafficTreatment;
26import org.onosproject.net.packet.DefaultOutboundPacket;
27import org.onosproject.net.packet.InboundPacket;
28import org.onosproject.net.packet.OutboundPacket;
29import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
32import java.nio.ByteBuffer;
33import java.util.concurrent.ConcurrentHashMap;
34import java.util.concurrent.ConcurrentLinkedQueue;
35
36import static com.google.common.base.Preconditions.checkNotNull;
37
38public class IpHandler {
39
40 private static Logger log = LoggerFactory.getLogger(IpHandler.class);
41 private SegmentRoutingManager srManager;
sangho9b169e32015-04-14 16:27:13 -070042 private DeviceConfiguration config;
sangho80f11cb2015-04-01 13:05:26 -070043 private ConcurrentHashMap<Ip4Address, ConcurrentLinkedQueue<IPv4>> ipPacketQueue;
44
45 /**
46 * Creates an IpHandler object.
47 *
48 * @param srManager SegmentRoutingManager object
49 */
50 public IpHandler(SegmentRoutingManager srManager) {
51 this.srManager = srManager;
sangho9b169e32015-04-14 16:27:13 -070052 this.config = checkNotNull(srManager.deviceConfiguration);
Sho SHIMIZU47b8aa22015-09-11 11:19:11 -070053 ipPacketQueue = new ConcurrentHashMap<>();
sangho80f11cb2015-04-01 13:05:26 -070054 }
55
56 /**
57 * Processes incoming IP packets.
58 *
59 * If it is an IP packet for known host, then forward it to the host.
60 * If it is an IP packet for unknown host in subnet, then send an ARP request
61 * to the subnet.
62 *
63 * @param pkt incoming packet
64 */
65 public void processPacketIn(InboundPacket pkt) {
66 Ethernet ethernet = pkt.parsed();
67 IPv4 ipv4 = (IPv4) ethernet.getPayload();
68
69 ConnectPoint connectPoint = pkt.receivedFrom();
70 DeviceId deviceId = connectPoint.deviceId();
71 Ip4Address destinationAddress =
72 Ip4Address.valueOf(ipv4.getDestinationAddress());
73
74 // IP packet for know hosts
75 if (!srManager.hostService.getHostsByIp(destinationAddress).isEmpty()) {
76 forwardPackets(deviceId, destinationAddress);
77
78 // IP packet for unknown host in the subnet of the router
79 } else if (config.inSameSubnet(deviceId, destinationAddress)) {
80 srManager.arpHandler.sendArpRequest(deviceId, destinationAddress, connectPoint);
81
82 // IP packets for unknown host
83 } else {
84 log.debug("ICMP request for unknown host {} which is not in the subnet",
85 destinationAddress);
86 // Do nothing
87 }
88 }
89
90 /**
91 * Adds the IP packet to a buffer.
92 * The packets are forwarded to corresponding destination when the destination
93 * MAC address is known via ARP response.
94 *
95 * @param ipPacket IP packet to add to the buffer
96 */
97 public void addToPacketBuffer(IPv4 ipPacket) {
98
99 // Better not buffer TPC packets due to out-of-order packet transfer
100 if (ipPacket.getProtocol() == IPv4.PROTOCOL_TCP) {
101 return;
102 }
103
104 Ip4Address destIpAddress = Ip4Address.valueOf(ipPacket.getDestinationAddress());
105
106 if (ipPacketQueue.get(destIpAddress) == null) {
Sho SHIMIZU47b8aa22015-09-11 11:19:11 -0700107 ConcurrentLinkedQueue<IPv4> queue = new ConcurrentLinkedQueue<>();
sangho80f11cb2015-04-01 13:05:26 -0700108 queue.add(ipPacket);
109 ipPacketQueue.put(destIpAddress, queue);
110 } else {
111 ipPacketQueue.get(destIpAddress).add(ipPacket);
112 }
113 }
114
115 /**
116 * Forwards IP packets in the buffer to the destination IP address.
117 * It is called when the controller finds the destination MAC address
Saurav Das88979182015-10-19 14:37:36 -0700118 * via ARP responses.
sangho80f11cb2015-04-01 13:05:26 -0700119 *
120 * @param deviceId switch device ID
121 * @param destIpAddress destination IP address
122 */
123 public void forwardPackets(DeviceId deviceId, Ip4Address destIpAddress) {
Srikanth Vavilapallic56a2f32015-06-05 11:40:14 -0700124 if (ipPacketQueue.get(destIpAddress) == null) {
125 return;
126 }
127
sangho80f11cb2015-04-01 13:05:26 -0700128 for (IPv4 ipPacket : ipPacketQueue.get(destIpAddress)) {
129 Ip4Address destAddress = Ip4Address.valueOf(ipPacket.getDestinationAddress());
130 if (ipPacket != null && config.inSameSubnet(deviceId, destAddress)) {
131 ipPacket.setTtl((byte) (ipPacket.getTtl() - 1));
132 ipPacket.setChecksum((short) 0);
133 for (Host dest: srManager.hostService.getHostsByIp(destIpAddress)) {
134 Ethernet eth = new Ethernet();
135 eth.setDestinationMACAddress(dest.mac());
sangho9b169e32015-04-14 16:27:13 -0700136 eth.setSourceMACAddress(config.getDeviceMac(deviceId));
sangho80f11cb2015-04-01 13:05:26 -0700137 eth.setEtherType(Ethernet.TYPE_IPV4);
138 eth.setPayload(ipPacket);
139
140 TrafficTreatment treatment = DefaultTrafficTreatment.builder().
141 setOutput(dest.location().port()).build();
142 OutboundPacket packet = new DefaultOutboundPacket(deviceId,
143 treatment, ByteBuffer.wrap(eth.serialize()));
144 srManager.packetService.emit(packet);
sanghofb7c7292015-04-13 15:15:58 -0700145 ipPacketQueue.get(destIpAddress).remove(ipPacket);
sangho80f11cb2015-04-01 13:05:26 -0700146 }
sangho9b169e32015-04-14 16:27:13 -0700147 ipPacketQueue.get(destIpAddress).remove(ipPacket);
sangho80f11cb2015-04-01 13:05:26 -0700148 }
149 }
150 }
sangho9b169e32015-04-14 16:27:13 -0700151
sangho80f11cb2015-04-01 13:05:26 -0700152}