blob: b1682e77e8d71c27870d3467294749acfb4731b1 [file] [log] [blame]
sanghob35a6192015-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;
Charles Chan0b4e6182015-11-03 10:42:14 -080029import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
30import org.onosproject.segmentrouting.config.DeviceConfiguration;
sanghob35a6192015-04-01 13:05:26 -070031import org.slf4j.Logger;
32import org.slf4j.LoggerFactory;
33
34import java.nio.ByteBuffer;
35import java.util.concurrent.ConcurrentHashMap;
36import java.util.concurrent.ConcurrentLinkedQueue;
37
38import static com.google.common.base.Preconditions.checkNotNull;
39
40public class IpHandler {
41
42 private static Logger log = LoggerFactory.getLogger(IpHandler.class);
43 private SegmentRoutingManager srManager;
sangho666cd6d2015-04-14 16:27:13 -070044 private DeviceConfiguration config;
sanghob35a6192015-04-01 13:05:26 -070045 private ConcurrentHashMap<Ip4Address, ConcurrentLinkedQueue<IPv4>> ipPacketQueue;
46
47 /**
48 * Creates an IpHandler object.
49 *
50 * @param srManager SegmentRoutingManager object
51 */
52 public IpHandler(SegmentRoutingManager srManager) {
53 this.srManager = srManager;
sangho666cd6d2015-04-14 16:27:13 -070054 this.config = checkNotNull(srManager.deviceConfiguration);
Sho SHIMIZU6cfc02d2015-09-11 11:19:11 -070055 ipPacketQueue = new ConcurrentHashMap<>();
sanghob35a6192015-04-01 13:05:26 -070056 }
57
58 /**
59 * Processes incoming IP packets.
60 *
61 * If it is an IP packet for known host, then forward it to the host.
62 * If it is an IP packet for unknown host in subnet, then send an ARP request
63 * to the subnet.
64 *
65 * @param pkt incoming packet
66 */
67 public void processPacketIn(InboundPacket pkt) {
68 Ethernet ethernet = pkt.parsed();
69 IPv4 ipv4 = (IPv4) ethernet.getPayload();
70
71 ConnectPoint connectPoint = pkt.receivedFrom();
72 DeviceId deviceId = connectPoint.deviceId();
73 Ip4Address destinationAddress =
74 Ip4Address.valueOf(ipv4.getDestinationAddress());
75
76 // IP packet for know hosts
77 if (!srManager.hostService.getHostsByIp(destinationAddress).isEmpty()) {
78 forwardPackets(deviceId, destinationAddress);
79
80 // IP packet for unknown host in the subnet of the router
81 } else if (config.inSameSubnet(deviceId, destinationAddress)) {
82 srManager.arpHandler.sendArpRequest(deviceId, destinationAddress, connectPoint);
83
84 // IP packets for unknown host
85 } else {
86 log.debug("ICMP request for unknown host {} which is not in the subnet",
87 destinationAddress);
88 // Do nothing
89 }
90 }
91
92 /**
93 * Adds the IP packet to a buffer.
94 * The packets are forwarded to corresponding destination when the destination
95 * MAC address is known via ARP response.
96 *
97 * @param ipPacket IP packet to add to the buffer
98 */
99 public void addToPacketBuffer(IPv4 ipPacket) {
100
101 // Better not buffer TPC packets due to out-of-order packet transfer
102 if (ipPacket.getProtocol() == IPv4.PROTOCOL_TCP) {
103 return;
104 }
105
106 Ip4Address destIpAddress = Ip4Address.valueOf(ipPacket.getDestinationAddress());
107
108 if (ipPacketQueue.get(destIpAddress) == null) {
Sho SHIMIZU6cfc02d2015-09-11 11:19:11 -0700109 ConcurrentLinkedQueue<IPv4> queue = new ConcurrentLinkedQueue<>();
sanghob35a6192015-04-01 13:05:26 -0700110 queue.add(ipPacket);
111 ipPacketQueue.put(destIpAddress, queue);
112 } else {
113 ipPacketQueue.get(destIpAddress).add(ipPacket);
114 }
115 }
116
117 /**
118 * Forwards IP packets in the buffer to the destination IP address.
119 * It is called when the controller finds the destination MAC address
Saurav Dasa07f2032015-10-19 14:37:36 -0700120 * via ARP responses.
sanghob35a6192015-04-01 13:05:26 -0700121 *
122 * @param deviceId switch device ID
123 * @param destIpAddress destination IP address
124 */
125 public void forwardPackets(DeviceId deviceId, Ip4Address destIpAddress) {
Srikanth Vavilapalli78baf582015-06-05 11:40:14 -0700126 if (ipPacketQueue.get(destIpAddress) == null) {
127 return;
128 }
129
sanghob35a6192015-04-01 13:05:26 -0700130 for (IPv4 ipPacket : ipPacketQueue.get(destIpAddress)) {
131 Ip4Address destAddress = Ip4Address.valueOf(ipPacket.getDestinationAddress());
Charles Chan0b4e6182015-11-03 10:42:14 -0800132 if (config.inSameSubnet(deviceId, destAddress)) {
sanghob35a6192015-04-01 13:05:26 -0700133 ipPacket.setTtl((byte) (ipPacket.getTtl() - 1));
134 ipPacket.setChecksum((short) 0);
135 for (Host dest: srManager.hostService.getHostsByIp(destIpAddress)) {
136 Ethernet eth = new Ethernet();
137 eth.setDestinationMACAddress(dest.mac());
Charles Chan0b4e6182015-11-03 10:42:14 -0800138 try {
139 eth.setSourceMACAddress(config.getDeviceMac(deviceId));
140 } catch (DeviceConfigNotFoundException e) {
141 log.warn(e.getMessage()
142 + " Skipping forwardPackets for this destination.");
143 continue;
144 }
sanghob35a6192015-04-01 13:05:26 -0700145 eth.setEtherType(Ethernet.TYPE_IPV4);
146 eth.setPayload(ipPacket);
147
148 TrafficTreatment treatment = DefaultTrafficTreatment.builder().
149 setOutput(dest.location().port()).build();
150 OutboundPacket packet = new DefaultOutboundPacket(deviceId,
151 treatment, ByteBuffer.wrap(eth.serialize()));
152 srManager.packetService.emit(packet);
sangho20eff1d2015-04-13 15:15:58 -0700153 ipPacketQueue.get(destIpAddress).remove(ipPacket);
sanghob35a6192015-04-01 13:05:26 -0700154 }
sangho666cd6d2015-04-14 16:27:13 -0700155 ipPacketQueue.get(destIpAddress).remove(ipPacket);
sanghob35a6192015-04-01 13:05:26 -0700156 }
157 }
158 }
sangho666cd6d2015-04-14 16:27:13 -0700159
sanghob35a6192015-04-01 13:05:26 -0700160}