blob: b724e27e7bb2faebf275e99b83e58e033a8d3b14 [file] [log] [blame]
Srikanth Vavilapalli7f479d92014-09-24 13:36:46 -07001/*******************************************************************************
2 * Copyright (c) 2014 Open Networking Laboratory.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Apache License v2.0
5 * which accompanies this distribution, and is available at
6 * http://www.apache.org/licenses/LICENSE-2.0
7 ******************************************************************************/
8
9package net.onrc.onos.apps.segmentrouting;
10
11import java.util.ArrayList;
Srikanth Vavilapalli27f2a122014-09-24 15:56:02 -070012import java.util.HashSet;
Srikanth Vavilapalli7f479d92014-09-24 13:36:46 -070013import java.util.Iterator;
14import java.util.List;
15
16import net.floodlightcontroller.core.IFloodlightProviderService;
17import net.floodlightcontroller.core.IOFSwitch;
18import net.floodlightcontroller.core.module.FloodlightModuleContext;
19import net.floodlightcontroller.util.MACAddress;
Srikanth Vavilapalli7f479d92014-09-24 13:36:46 -070020import net.onrc.onos.core.flowprogrammer.IFlowPusherService;
21import net.onrc.onos.core.packet.ARP;
22import net.onrc.onos.core.packet.Ethernet;
23import net.onrc.onos.core.packet.IPv4;
Srikanth Vavilapalli27f2a122014-09-24 15:56:02 -070024import net.onrc.onos.core.topology.Host;
Srikanth Vavilapalli7f479d92014-09-24 13:36:46 -070025import net.onrc.onos.core.topology.ITopologyService;
26import net.onrc.onos.core.topology.MutableTopology;
27import net.onrc.onos.core.topology.Port;
28import net.onrc.onos.core.topology.Switch;
29
30import org.json.JSONArray;
31import org.json.JSONException;
32import org.projectfloodlight.openflow.protocol.OFFactory;
33import org.projectfloodlight.openflow.protocol.OFPacketOut;
34import org.projectfloodlight.openflow.protocol.action.OFAction;
35import org.projectfloodlight.openflow.types.IPv4Address;
36import org.projectfloodlight.openflow.types.MacAddress;
37import org.projectfloodlight.openflow.types.OFPort;
38import org.projectfloodlight.openflow.types.U32;
39import org.slf4j.Logger;
40import org.slf4j.LoggerFactory;
41
42import com.esotericsoftware.minlog.Log;
43
44/**
45 * Handling ARP requests to switches for Segment Routing.
46 * <p/>
47 * The module is for handling ARP requests to switches. It sends ARP response for any known
48 * hosts to the controllers.
49 * TODO: need to check the network config file for all hosts and packets
50 */
Srikanth Vavilapallib1fce732014-09-24 14:09:50 -070051public class ArpHandler {
Srikanth Vavilapalli7f479d92014-09-24 13:36:46 -070052
53 private static final Logger log = LoggerFactory
54 .getLogger(ArpHandler.class);
55
56 private IFloodlightProviderService floodlightProvider;
Srikanth Vavilapalli7f479d92014-09-24 13:36:46 -070057 private IFlowPusherService flowPusher;
58 private ITopologyService topologyService;
59 private MutableTopology mutableTopology;
60 //private List<ArpEntry> arpEntries;
61 private SegmentRoutingManager srManager;
62
63 private static final short IDLE_TIMEOUT = 0;
64 private static final short HARD_TIMEOUT = 0;
65
66 private static final int TABLE_VLAN = 0;
67 private static final int TABLE_TMAC = 1;
68 private static final int TABLE_IPv4_UNICAST = 2;
69 private static final int TABLE_MPLS = 3;
70 private static final int TABLE_META = 4;
71 private static final int TABLE_ACL = 5;
72
73 private static final short MAX_PRIORITY = (short) 0xffff;
74 private static final short SLASH_24_PRIORITY = (short) 0xfff0;
75 private static final short SLASH_16_PRIORITY = (short) 0xff00;
76 private static final short SLASH_8_PRIORITY = (short) 0xf000;
77 private static final short MIN_PRIORITY = 0x0;
78
79
80 /*
81 * Default Constructor
82 */
83 public ArpHandler(FloodlightModuleContext context, SegmentRoutingManager segmentRoutingManager) {
84
85 this.floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
Srikanth Vavilapalli7f479d92014-09-24 13:36:46 -070086 this.flowPusher = context.getServiceImpl(IFlowPusherService.class);
87 this.topologyService = context.getServiceImpl(ITopologyService.class);
88 this.srManager = segmentRoutingManager;
89 this.mutableTopology = topologyService.getTopology();
90
Srikanth Vavilapalli7f479d92014-09-24 13:36:46 -070091 Log.debug("Arp Handler is initialized");
92
93 }
94
Sangho Shin463bee52014-09-29 15:14:43 -070095 /**
96 * process ARP packets from switches
97 * It add a IP routing rule to the host
98 * If it is an ARP response, then flush out all pending packets to the host
99 *
100 * @param sw
101 * @param inPort
102 * @param payload
103 */
Srikanth Vavilapallib1fce732014-09-24 14:09:50 -0700104 public void processPacketIn(Switch sw, Port inPort, Ethernet payload){
Srikanth Vavilapalli7f479d92014-09-24 13:36:46 -0700105
Srikanth Vavilapallib1fce732014-09-24 14:09:50 -0700106 log.debug("ArpHandler: Received a ARP packet from sw {} ", sw.getDpid());
Srikanth Vavilapalli7f479d92014-09-24 13:36:46 -0700107
Srikanth Vavilapallib1fce732014-09-24 14:09:50 -0700108 ARP arp = (ARP)payload.getPayload();
Srikanth Vavilapalli7f479d92014-09-24 13:36:46 -0700109
Sangho Shin463bee52014-09-29 15:14:43 -0700110 byte[] senderMacAddressByte = arp.getSenderHardwareAddress();
111 IPv4Address hostIpAddress = IPv4Address.of(arp.getSenderProtocolAddress());
112 log.debug("ArpHandler: Add IP route to Host {} ", hostIpAddress);
113 srManager.addRouteToHost(sw,hostIpAddress.getInt(), senderMacAddressByte);
114
Srikanth Vavilapallib1fce732014-09-24 14:09:50 -0700115 if (arp.getOpCode() == ARP.OP_REQUEST) {
116 log.debug("ArpHandler: Received a ARP Requestfrom sw {} ", sw.getDpid());
117 handleArpRequest(sw, inPort, payload);
Srikanth Vavilapalli7f479d92014-09-24 13:36:46 -0700118 }
Sangho Shin463bee52014-09-29 15:14:43 -0700119 else {
120 byte[] destIp = arp.getSenderProtocolAddress();
121 for (IPv4 ipPacket: srManager.getIpPacketFromQueue(destIp)) {
122 if (ipPacket != null && !inSameSubnet(sw, ipPacket)) {
123 Ethernet eth = new Ethernet();
124 eth.setDestinationMACAddress(payload.getSourceMACAddress());
125 eth.setSourceMACAddress(sw.getStringAttribute("routerMac"));
126 eth.setEtherType(Ethernet.TYPE_IPV4);
127 eth.setPayload(ipPacket);
128 sendPacketOut(sw, eth, inPort.getNumber().shortValue());
129 }
130 }
131 }
Srikanth Vavilapalli7f479d92014-09-24 13:36:46 -0700132 }
133
Srikanth Vavilapalli7f479d92014-09-24 13:36:46 -0700134 /**
135 * Send an ARP response for the ARP request to the known switches
136 *
137 * @param sw Switch
138 * @param inPort port to send ARP response to
139 * @param arpRequest ARP request packet to handle
140 */
Srikanth Vavilapallib1fce732014-09-24 14:09:50 -0700141 private void handleArpRequest(Switch sw, Port inPort, Ethernet payload) {
Srikanth Vavilapalli7f479d92014-09-24 13:36:46 -0700142
Srikanth Vavilapallib1fce732014-09-24 14:09:50 -0700143 ARP arpRequest = (ARP)payload.getPayload();
Srikanth Vavilapalli27f2a122014-09-24 15:56:02 -0700144 MACAddress targetMac = null;
Srikanth Vavilapalli7f479d92014-09-24 13:36:46 -0700145
Srikanth Vavilapalli27f2a122014-09-24 15:56:02 -0700146 if (isArpReqForSwitch(sw, arpRequest)) {
147 String switchMacAddressStr = sw.getStringAttribute("routerMac");
148 targetMac = MACAddress.valueOf(switchMacAddressStr);
149 log.debug("ArpHandler: Received a ARP query for a sw {} ", sw.getDpid());
150 }
Srikanth Vavilapalli7f479d92014-09-24 13:36:46 -0700151
Srikanth Vavilapalli27f2a122014-09-24 15:56:02 -0700152 Host knownHost = isArpReqForKnownHost(sw, arpRequest);
153 if (knownHost != null) {
154 targetMac = knownHost.getMacAddress();
155 log.debug("ArpHandler: Received a ARP query for a known host {} ",
156 IPv4Address.of(knownHost.getIpAddress()));
157 }
Srikanth Vavilapalli7f479d92014-09-24 13:36:46 -0700158
Srikanth Vavilapalli27f2a122014-09-24 15:56:02 -0700159 if (targetMac != null) {
160 /* ARP Destination is known. Packet out ARP Reply */
161 ARP arpReply = new ARP();
162 arpReply.setHardwareType(ARP.HW_TYPE_ETHERNET)
163 .setProtocolType(ARP.PROTO_TYPE_IP)
164 .setHardwareAddressLength(
165 (byte) Ethernet.DATALAYER_ADDRESS_LENGTH)
166 .setProtocolAddressLength((byte) IPv4.ADDRESS_LENGTH)
167 .setOpCode(ARP.OP_REPLY)
168 .setSenderHardwareAddress(targetMac.toBytes())
169 .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress())
170 .setTargetHardwareAddress(arpRequest.getSenderHardwareAddress())
171 .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress());
172
173 Ethernet eth = new Ethernet();
174 eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress())
175 .setSourceMACAddress(targetMac.toBytes())
176 .setEtherType(Ethernet.TYPE_ARP).setPayload(arpReply);
177
178 sendPacketOut(sw, eth, inPort.getPortNumber().shortValue());
179 }
180 else
181 {
182 /* Broadcast the received ARP request to all switch ports
183 * that subnets are connected to except the port from which
184 * ARP request is received
185 */
186 log.debug("ArpHandler: Received a ARP query for unknown host {} ",
187 IPv4Address.of(arpRequest.getTargetProtocolAddress()));
188 for (Integer portNo : getSwitchSubnetPorts(sw)) {
189 if (portNo.shortValue() == inPort.getPortNumber().shortValue())
190 continue;
191 log.debug("ArpHandler: Sending ARP request on switch {} port {}",
192 sw.getDpid(), portNo.shortValue());
193 sendPacketOut(sw, payload, portNo.shortValue());
194 }
195 }
Srikanth Vavilapalli7f479d92014-09-24 13:36:46 -0700196 }
197
Sangho Shin463bee52014-09-29 15:14:43 -0700198 /**
199 * Check if the ARP request is to known hosts
200 *
201 * @param sw Switch
202 * @param arpRequest ARP request to check
203 */
Srikanth Vavilapalli27f2a122014-09-24 15:56:02 -0700204 private Host isArpReqForKnownHost(Switch sw, ARP arpRequest) {
205 Host knownHost = null;
206
207 IPv4Address targetIPAddress = IPv4Address.of(
208 arpRequest.getTargetProtocolAddress());
209
210 for (Host host:sw.getHosts()) {
211 if (host.getIpAddress() == targetIPAddress.getInt()) {
212 knownHost = host;
213 break;
214 }
215 }
216 return knownHost;
217
218 }
Sangho Shin463bee52014-09-29 15:14:43 -0700219
220 /**
221 *
222 * Check if the ARP is for the switch
223 *
224 * @param sw Switch
225 * @param arpRequest ARP request to check
226 * @return true if the ARP is for the switch
227 */
Srikanth Vavilapalli27f2a122014-09-24 15:56:02 -0700228 private boolean isArpReqForSwitch(Switch sw, ARP arpRequest) {
229 List<String> subnetGatewayIPs = getSubnetGatewayIps(sw);
230 boolean isArpForSwitch = false;
231 if (!subnetGatewayIPs.isEmpty()) {
232 IPv4Address targetProtocolAddress = IPv4Address.of(arpRequest.getTargetProtocolAddress());
233 if (subnetGatewayIPs.contains(targetProtocolAddress.toString())) {
234 isArpForSwitch = true;
235 }
236 }
237 return isArpForSwitch;
238 }
Sangho Shin463bee52014-09-29 15:14:43 -0700239
Srikanth Vavilapalli7f479d92014-09-24 13:36:46 -0700240 /**
241 * Retrieve Gateway IP address of all subnets defined in net config file
242 *
243 * @param sw Switch to retrieve subnet GW IPs for
244 * @return list of GW IP addresses for all subnets
245 */
246 private List<String> getSubnetGatewayIps(Switch sw) {
247
248 List<String> gatewayIps = new ArrayList<String>();
249
250 String subnets = sw.getStringAttribute("subnets");
251 try {
252 JSONArray arry = new JSONArray(subnets);
253 for (int i = 0; i < arry.length(); i++) {
254 String subnetIpSlash = (String) arry.getJSONObject(i).get("subnetIp");
255 if (subnetIpSlash != null) {
256 String subnetIp = subnetIpSlash.substring(0, subnetIpSlash.indexOf('/'));
257 gatewayIps.add(subnetIp);
258 }
259 }
260 } catch (JSONException e) {
261 // TODO Auto-generated catch block
262 e.printStackTrace();
263 }
264
265 return gatewayIps;
266 }
267
Srikanth Vavilapalli27f2a122014-09-24 15:56:02 -0700268 private HashSet<Integer> getSwitchSubnetPorts(Switch sw) {
269 HashSet<Integer> switchSubnetPorts = new HashSet<Integer>();
270
271 String subnets = sw.getStringAttribute("subnets");
272 try {
273 JSONArray arry = new JSONArray(subnets);
274 for (int i = 0; i < arry.length(); i++) {
275 Integer subnetPort = (Integer)arry.getJSONObject(i).get("portNo");
276 switchSubnetPorts.add(subnetPort);
277 }
278 } catch (JSONException e) {
279 // TODO Auto-generated catch block
280 e.printStackTrace();
281 }
282
283 return switchSubnetPorts;
284 }
285
Srikanth Vavilapalli7f479d92014-09-24 13:36:46 -0700286 /**
287 * Send an ARP request
288 *
289 * @param sw Switch
290 * @param targetAddress Target IP address
291 * @param inPort Port to send the ARP request
292 *
293 */
294 public void sendArpRequest(Switch sw, int targetAddressInt, Port inPort) {
295
296 IPv4Address targetAddress = IPv4Address.of(targetAddressInt);
297 String senderMacAddressStr = sw.getStringAttribute("routerMac");
298 String senderIpAddressSlash = sw.getStringAttribute("routerIp");
299 if (senderMacAddressStr == null || senderIpAddressSlash == null)
300 return;
301 String senderIpAddressStr =
302 senderIpAddressSlash.substring(0, senderIpAddressSlash.indexOf('/'));
303 byte[] senderMacAddress = MacAddress.of(senderMacAddressStr).getBytes();
304 byte[] senderIpAddress = IPv4Address.of(senderIpAddressStr).getBytes();
305
306 ARP arpRequest = new ARP();
307 arpRequest.setHardwareType(ARP.HW_TYPE_ETHERNET)
308 .setProtocolType(ARP.PROTO_TYPE_IP)
309 .setHardwareAddressLength(
310 (byte) Ethernet.DATALAYER_ADDRESS_LENGTH)
311 .setProtocolAddressLength((byte) IPv4.ADDRESS_LENGTH)
312 .setOpCode(ARP.OP_REQUEST)
313 .setSenderHardwareAddress(senderMacAddress)
314 .setTargetHardwareAddress(MacAddress.NONE.getBytes())
315 .setSenderProtocolAddress(senderIpAddress)
316 .setTargetProtocolAddress(targetAddress.getBytes());
317
318 Ethernet eth = new Ethernet();
319 eth.setDestinationMACAddress(MacAddress.BROADCAST.getBytes())
320 .setSourceMACAddress(senderMacAddress)
321 .setEtherType(Ethernet.TYPE_ARP).setPayload(arpRequest);
322
Srikanth Vavilapallia95d5832014-09-24 16:38:52 -0700323 /* Broadcast the ARP request to all switch ports
324 * that subnets are connected to except the port from which
325 * ARP request is received
326 */
327 for (Integer portNo : getSwitchSubnetPorts(sw)) {
328 if (portNo.shortValue() == inPort.getPortNumber().shortValue())
329 continue;
330 log.debug("ArpHandler: Sending ARP request on switch {} port {}",
331 sw.getDpid(), portNo.shortValue());
332 sendPacketOut(sw, eth, portNo.shortValue());
333 }
Srikanth Vavilapalli7f479d92014-09-24 13:36:46 -0700334 }
335
336 /**
337 * Send PACKET_OUT packet to switch
338 *
339 * @param sw Switch to send the packet to
340 * @param packet Packet to send
341 * @param switchPort port to send (if -1, broadcast)
342 */
343 private void sendPacketOut(Switch sw, Ethernet packet, short port) {
344
345 IOFSwitch ofSwitch = floodlightProvider.getMasterSwitch(sw.getDpid().value());
346 OFFactory factory = ofSwitch.getFactory();
347
348 List<OFAction> actions = new ArrayList<>();
349
350 if (port > 0) {
351 OFAction outport = factory.actions().output(OFPort.of(port), Short.MAX_VALUE);
352 actions.add(outport);
353 }
354 else {
355 Iterator<Port> iter = sw.getPorts().iterator();
356 while (iter.hasNext()) {
357 Port p = iter.next();
358 int pnum = p.getPortNumber().shortValue();
359 if (U32.of(pnum).compareTo(U32.of(OFPort.MAX.getPortNumber())) < 1) {
360 OFAction outport = factory.actions().output(OFPort.of(p.getNumber().shortValue()),
361 Short.MAX_VALUE);
362 actions.add(outport);
363 }
364 }
365 }
366
367 OFPacketOut po = factory.buildPacketOut()
368 .setData(packet.serialize())
369 .setActions(actions)
370 .build();
371
372 flowPusher.add(sw.getDpid(), po);
373 }
374
Sangho Shin463bee52014-09-29 15:14:43 -0700375 /**
376 * Check if the source IP and destination IP are in the same subnet
377 *
378 * @param sw Switch
379 * @param ipv4 IP address to check
380 * @return return true if the IP packet is within the same subnet
381 */
382 private boolean inSameSubnet(Switch sw, IPv4 ipv4) {
383
384 String gwIpSrc = getGwIpForSubnet(ipv4.getSourceAddress());
385 String gwIpDest = getGwIpForSubnet(ipv4.getDestinationAddress());
386
387 if (gwIpSrc.equals(gwIpDest)) {
388 return true;
389 }
390 else
391 return false;
392 }
393
394 /**
395 * Get router IP address for the given IP address
396 *
397 * @param sourceAddress
398 * @return
399 */
400 private String getGwIpForSubnet(int sourceAddress) {
401
402 String gwIp = null;
403 IPv4Address srcIp = IPv4Address.of(sourceAddress);
404
405 for (Switch sw: mutableTopology.getSwitches()) {
406
407 String subnets = sw.getStringAttribute("subnets");
408 try {
409 JSONArray arry = new JSONArray(subnets);
410 for (int i = 0; i < arry.length(); i++) {
411 String subnetIpSlash = (String) arry.getJSONObject(i).get("subnetIp");
412 if (srManager.netMatch(subnetIpSlash, srcIp.toString())) {
413 gwIp = subnetIpSlash;
414 }
415 }
416 } catch (JSONException e) {
417 // TODO Auto-generated catch block
418 e.printStackTrace();
419 }
420 }
421
422 return gwIp;
423 }
424
Srikanth Vavilapalli7f479d92014-09-24 13:36:46 -0700425}
426