blob: a46fb57959fc7111286d7b442216d07fd4b60fc1 [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);
sangho80f11cb2015-04-01 13:05:26 -070053 ipPacketQueue = new ConcurrentHashMap<Ip4Address, ConcurrentLinkedQueue<IPv4>>();
54 }
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) {
107 ConcurrentLinkedQueue<IPv4> queue = new ConcurrentLinkedQueue<IPv4>();
108 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
118 * via ARP responsees.
119 *
120 * @param deviceId switch device ID
121 * @param destIpAddress destination IP address
122 */
123 public void forwardPackets(DeviceId deviceId, Ip4Address destIpAddress) {
124 for (IPv4 ipPacket : ipPacketQueue.get(destIpAddress)) {
125 Ip4Address destAddress = Ip4Address.valueOf(ipPacket.getDestinationAddress());
126 if (ipPacket != null && config.inSameSubnet(deviceId, destAddress)) {
127 ipPacket.setTtl((byte) (ipPacket.getTtl() - 1));
128 ipPacket.setChecksum((short) 0);
129 for (Host dest: srManager.hostService.getHostsByIp(destIpAddress)) {
130 Ethernet eth = new Ethernet();
131 eth.setDestinationMACAddress(dest.mac());
sangho9b169e32015-04-14 16:27:13 -0700132 eth.setSourceMACAddress(config.getDeviceMac(deviceId));
sangho80f11cb2015-04-01 13:05:26 -0700133 eth.setEtherType(Ethernet.TYPE_IPV4);
134 eth.setPayload(ipPacket);
135
136 TrafficTreatment treatment = DefaultTrafficTreatment.builder().
137 setOutput(dest.location().port()).build();
138 OutboundPacket packet = new DefaultOutboundPacket(deviceId,
139 treatment, ByteBuffer.wrap(eth.serialize()));
140 srManager.packetService.emit(packet);
sanghofb7c7292015-04-13 15:15:58 -0700141 ipPacketQueue.get(destIpAddress).remove(ipPacket);
sangho80f11cb2015-04-01 13:05:26 -0700142 }
sangho9b169e32015-04-14 16:27:13 -0700143 ipPacketQueue.get(destIpAddress).remove(ipPacket);
sangho80f11cb2015-04-01 13:05:26 -0700144 }
145 }
146 }
sangho9b169e32015-04-14 16:27:13 -0700147
sangho80f11cb2015-04-01 13:05:26 -0700148}