Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 1 | /******************************************************************************* |
| 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 | |
| 9 | package net.onrc.onos.apps.segmentrouting; |
| 10 | |
| 11 | import java.util.ArrayList; |
Srikanth Vavilapalli | 27f2a12 | 2014-09-24 15:56:02 -0700 | [diff] [blame] | 12 | import java.util.HashSet; |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 13 | import java.util.Iterator; |
| 14 | import java.util.List; |
| 15 | |
| 16 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 17 | import net.floodlightcontroller.core.IOFSwitch; |
| 18 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 19 | import net.floodlightcontroller.util.MACAddress; |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 20 | import net.onrc.onos.core.flowprogrammer.IFlowPusherService; |
| 21 | import net.onrc.onos.core.packet.ARP; |
| 22 | import net.onrc.onos.core.packet.Ethernet; |
| 23 | import net.onrc.onos.core.packet.IPv4; |
Srikanth Vavilapalli | 27f2a12 | 2014-09-24 15:56:02 -0700 | [diff] [blame] | 24 | import net.onrc.onos.core.topology.Host; |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 25 | import net.onrc.onos.core.topology.ITopologyService; |
| 26 | import net.onrc.onos.core.topology.MutableTopology; |
| 27 | import net.onrc.onos.core.topology.Port; |
| 28 | import net.onrc.onos.core.topology.Switch; |
| 29 | |
| 30 | import org.json.JSONArray; |
| 31 | import org.json.JSONException; |
| 32 | import org.projectfloodlight.openflow.protocol.OFFactory; |
| 33 | import org.projectfloodlight.openflow.protocol.OFPacketOut; |
| 34 | import org.projectfloodlight.openflow.protocol.action.OFAction; |
| 35 | import org.projectfloodlight.openflow.types.IPv4Address; |
| 36 | import org.projectfloodlight.openflow.types.MacAddress; |
| 37 | import org.projectfloodlight.openflow.types.OFPort; |
| 38 | import org.projectfloodlight.openflow.types.U32; |
| 39 | import org.slf4j.Logger; |
| 40 | import org.slf4j.LoggerFactory; |
| 41 | |
| 42 | import com.esotericsoftware.minlog.Log; |
| 43 | |
| 44 | /** |
| 45 | * Handling ARP requests to switches for Segment Routing. |
| 46 | * <p/> |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 47 | * The module is for handling ARP requests to switches. It sends ARP response |
| 48 | * for any known hosts to the controllers. TODO: need to check the network |
| 49 | * config file for all hosts and packets |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 50 | */ |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 51 | public class ArpHandler { |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 52 | |
| 53 | private static final Logger log = LoggerFactory |
| 54 | .getLogger(ArpHandler.class); |
| 55 | |
| 56 | private IFloodlightProviderService floodlightProvider; |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 57 | private IFlowPusherService flowPusher; |
| 58 | private ITopologyService topologyService; |
| 59 | private MutableTopology mutableTopology; |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 60 | // private List<ArpEntry> arpEntries; |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 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 | |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 79 | /* |
| 80 | * Default Constructor |
| 81 | */ |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 82 | public ArpHandler(FloodlightModuleContext context, |
| 83 | SegmentRoutingManager segmentRoutingManager) { |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 84 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 85 | this.floodlightProvider = context |
| 86 | .getServiceImpl(IFloodlightProviderService.class); |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 87 | this.flowPusher = context.getServiceImpl(IFlowPusherService.class); |
| 88 | this.topologyService = context.getServiceImpl(ITopologyService.class); |
| 89 | this.srManager = segmentRoutingManager; |
| 90 | this.mutableTopology = topologyService.getTopology(); |
| 91 | |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 92 | Log.debug("Arp Handler is initialized"); |
| 93 | |
| 94 | } |
| 95 | |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 96 | /** |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 97 | * process ARP packets from switches It add a IP routing rule to the host If |
| 98 | * it is an ARP response, then flush out all pending packets to the host |
| 99 | * |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 100 | * @param sw |
| 101 | * @param inPort |
| 102 | * @param payload |
| 103 | */ |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 104 | public void processPacketIn(Switch sw, Port inPort, Ethernet payload) { |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 105 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 106 | log.debug("ArpHandler: Received a ARP packet from sw {} ", sw.getDpid()); |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 107 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 108 | ARP arp = (ARP) payload.getPayload(); |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 109 | |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 110 | byte[] senderMacAddressByte = arp.getSenderHardwareAddress(); |
| 111 | IPv4Address hostIpAddress = IPv4Address.of(arp.getSenderProtocolAddress()); |
| 112 | log.debug("ArpHandler: Add IP route to Host {} ", hostIpAddress); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 113 | srManager.addRouteToHost(sw, hostIpAddress.getInt(), senderMacAddressByte); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 114 | |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 115 | if (arp.getOpCode() == ARP.OP_REQUEST) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 116 | log.debug("ArpHandler: Received a ARP Requestfrom sw {} ", sw.getDpid()); |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 117 | handleArpRequest(sw, inPort, payload); |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 118 | } |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 119 | else { |
| 120 | byte[] destIp = arp.getSenderProtocolAddress(); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 121 | for (IPv4 ipPacket : srManager.getIpPacketFromQueue(destIp)) { |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 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 Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 134 | /** |
| 135 | * Send an ARP response for the ARP request to the known switches |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 136 | * |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 137 | * @param sw Switch |
| 138 | * @param inPort port to send ARP response to |
| 139 | * @param arpRequest ARP request packet to handle |
| 140 | */ |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 141 | private void handleArpRequest(Switch sw, Port inPort, Ethernet payload) { |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 142 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 143 | ARP arpRequest = (ARP) payload.getPayload(); |
| 144 | MACAddress targetMac = null; |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 145 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 146 | if (isArpReqForSwitch(sw, arpRequest)) { |
Srikanth Vavilapalli | 27f2a12 | 2014-09-24 15:56:02 -0700 | [diff] [blame] | 147 | String switchMacAddressStr = sw.getStringAttribute("routerMac"); |
| 148 | targetMac = MACAddress.valueOf(switchMacAddressStr); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 149 | log.debug("ArpHandler: Received a ARP query for a sw {} ", sw.getDpid()); |
| 150 | } |
| 151 | else { |
| 152 | Host knownHost = isArpReqForKnownHost(sw, arpRequest); |
| 153 | if (knownHost != null) { |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 154 | targetMac = knownHost.getMacAddress(); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 155 | log.debug("ArpHandler: Received a ARP query for a known host {} ", |
| 156 | IPv4Address.of(knownHost.getIpAddress())); |
| 157 | } |
| 158 | } |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 159 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 160 | if (targetMac != null) { |
| 161 | /* ARP Destination is known. Packet out ARP Reply */ |
Srikanth Vavilapalli | 27f2a12 | 2014-09-24 15:56:02 -0700 | [diff] [blame] | 162 | ARP arpReply = new ARP(); |
| 163 | arpReply.setHardwareType(ARP.HW_TYPE_ETHERNET) |
| 164 | .setProtocolType(ARP.PROTO_TYPE_IP) |
| 165 | .setHardwareAddressLength( |
| 166 | (byte) Ethernet.DATALAYER_ADDRESS_LENGTH) |
| 167 | .setProtocolAddressLength((byte) IPv4.ADDRESS_LENGTH) |
| 168 | .setOpCode(ARP.OP_REPLY) |
| 169 | .setSenderHardwareAddress(targetMac.toBytes()) |
| 170 | .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress()) |
| 171 | .setTargetHardwareAddress(arpRequest.getSenderHardwareAddress()) |
| 172 | .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress()); |
| 173 | |
| 174 | Ethernet eth = new Ethernet(); |
| 175 | eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress()) |
| 176 | .setSourceMACAddress(targetMac.toBytes()) |
| 177 | .setEtherType(Ethernet.TYPE_ARP).setPayload(arpReply); |
| 178 | |
| 179 | sendPacketOut(sw, eth, inPort.getPortNumber().shortValue()); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 180 | } |
| 181 | else |
| 182 | { |
| 183 | /* Broadcast the received ARP request to all switch ports |
| 184 | * that subnets are connected to except the port from which |
| 185 | * ARP request is received |
| 186 | */ |
| 187 | IPv4Address targetAddress = |
| 188 | IPv4Address.of(arpRequest.getTargetProtocolAddress()); |
| 189 | log.debug("ArpHandler: Received a ARP query for unknown host {} ", |
| 190 | IPv4Address.of(arpRequest.getTargetProtocolAddress())); |
| 191 | for (Integer portNo : getSwitchSubnetPorts(sw, targetAddress)) { |
| 192 | if (portNo.shortValue() == inPort.getPortNumber().shortValue()) |
| 193 | continue; |
| 194 | log.debug("ArpHandler: Sending ARP request on switch {} port {}", |
| 195 | sw.getDpid(), portNo.shortValue()); |
Srikanth Vavilapalli | 27f2a12 | 2014-09-24 15:56:02 -0700 | [diff] [blame] | 196 | sendPacketOut(sw, payload, portNo.shortValue()); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 197 | } |
| 198 | } |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 201 | /** |
| 202 | * Check if the ARP request is to known hosts |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 203 | * |
| 204 | * @param sw Switch |
| 205 | * @param arpRequest ARP request to check |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 206 | */ |
Srikanth Vavilapalli | 27f2a12 | 2014-09-24 15:56:02 -0700 | [diff] [blame] | 207 | private Host isArpReqForKnownHost(Switch sw, ARP arpRequest) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 208 | Host knownHost = null; |
Srikanth Vavilapalli | 27f2a12 | 2014-09-24 15:56:02 -0700 | [diff] [blame] | 209 | |
| 210 | IPv4Address targetIPAddress = IPv4Address.of( |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 211 | arpRequest.getTargetProtocolAddress()); |
Srikanth Vavilapalli | 27f2a12 | 2014-09-24 15:56:02 -0700 | [diff] [blame] | 212 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 213 | for (Host host : sw.getHosts()) { |
| 214 | if (host.getIpAddress() == targetIPAddress.getInt()) { |
| 215 | knownHost = host; |
| 216 | break; |
| 217 | } |
Srikanth Vavilapalli | 27f2a12 | 2014-09-24 15:56:02 -0700 | [diff] [blame] | 218 | } |
| 219 | return knownHost; |
| 220 | |
| 221 | } |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 222 | |
| 223 | /** |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 224 | * |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 225 | * Check if the ARP is for the switch |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 226 | * |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 227 | * @param sw Switch |
| 228 | * @param arpRequest ARP request to check |
| 229 | * @return true if the ARP is for the switch |
| 230 | */ |
Srikanth Vavilapalli | 27f2a12 | 2014-09-24 15:56:02 -0700 | [diff] [blame] | 231 | private boolean isArpReqForSwitch(Switch sw, ARP arpRequest) { |
| 232 | List<String> subnetGatewayIPs = getSubnetGatewayIps(sw); |
| 233 | boolean isArpForSwitch = false; |
| 234 | if (!subnetGatewayIPs.isEmpty()) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 235 | IPv4Address targetProtocolAddress = IPv4Address.of(arpRequest |
| 236 | .getTargetProtocolAddress()); |
Srikanth Vavilapalli | 27f2a12 | 2014-09-24 15:56:02 -0700 | [diff] [blame] | 237 | if (subnetGatewayIPs.contains(targetProtocolAddress.toString())) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 238 | isArpForSwitch = true; |
Srikanth Vavilapalli | 27f2a12 | 2014-09-24 15:56:02 -0700 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | return isArpForSwitch; |
| 242 | } |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 243 | |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 244 | /** |
| 245 | * Retrieve Gateway IP address of all subnets defined in net config file |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 246 | * |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 247 | * @param sw Switch to retrieve subnet GW IPs for |
| 248 | * @return list of GW IP addresses for all subnets |
| 249 | */ |
| 250 | private List<String> getSubnetGatewayIps(Switch sw) { |
| 251 | |
| 252 | List<String> gatewayIps = new ArrayList<String>(); |
| 253 | |
| 254 | String subnets = sw.getStringAttribute("subnets"); |
| 255 | try { |
| 256 | JSONArray arry = new JSONArray(subnets); |
| 257 | for (int i = 0; i < arry.length(); i++) { |
| 258 | String subnetIpSlash = (String) arry.getJSONObject(i).get("subnetIp"); |
| 259 | if (subnetIpSlash != null) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 260 | String subnetIp = subnetIpSlash.substring(0, |
| 261 | subnetIpSlash.indexOf('/')); |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 262 | gatewayIps.add(subnetIp); |
| 263 | } |
| 264 | } |
| 265 | } catch (JSONException e) { |
| 266 | // TODO Auto-generated catch block |
| 267 | e.printStackTrace(); |
| 268 | } |
| 269 | |
| 270 | return gatewayIps; |
| 271 | } |
| 272 | |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 273 | private HashSet<Integer> getSwitchSubnetPorts(Switch sw, IPv4Address targetAddress) { |
Srikanth Vavilapalli | 27f2a12 | 2014-09-24 15:56:02 -0700 | [diff] [blame] | 274 | HashSet<Integer> switchSubnetPorts = new HashSet<Integer>(); |
| 275 | |
| 276 | String subnets = sw.getStringAttribute("subnets"); |
| 277 | try { |
| 278 | JSONArray arry = new JSONArray(subnets); |
| 279 | for (int i = 0; i < arry.length(); i++) { |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 280 | String subnetIpSlash = (String) arry.getJSONObject(i).get("subnetIp"); |
| 281 | if (srManager.netMatch(subnetIpSlash, targetAddress.toString())) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 282 | Integer subnetPort = (Integer) arry.getJSONObject(i).get("portNo"); |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 283 | switchSubnetPorts.add(subnetPort); |
| 284 | } |
Srikanth Vavilapalli | 27f2a12 | 2014-09-24 15:56:02 -0700 | [diff] [blame] | 285 | } |
| 286 | } catch (JSONException e) { |
| 287 | // TODO Auto-generated catch block |
| 288 | e.printStackTrace(); |
| 289 | } |
| 290 | |
| 291 | return switchSubnetPorts; |
| 292 | } |
| 293 | |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 294 | /** |
| 295 | * Send an ARP request |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 296 | * |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 297 | * @param sw Switch |
| 298 | * @param targetAddress Target IP address |
| 299 | * @param inPort Port to send the ARP request |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 300 | * |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 301 | */ |
| 302 | public void sendArpRequest(Switch sw, int targetAddressInt, Port inPort) { |
| 303 | |
| 304 | IPv4Address targetAddress = IPv4Address.of(targetAddressInt); |
| 305 | String senderMacAddressStr = sw.getStringAttribute("routerMac"); |
| 306 | String senderIpAddressSlash = sw.getStringAttribute("routerIp"); |
| 307 | if (senderMacAddressStr == null || senderIpAddressSlash == null) |
| 308 | return; |
| 309 | String senderIpAddressStr = |
| 310 | senderIpAddressSlash.substring(0, senderIpAddressSlash.indexOf('/')); |
| 311 | byte[] senderMacAddress = MacAddress.of(senderMacAddressStr).getBytes(); |
| 312 | byte[] senderIpAddress = IPv4Address.of(senderIpAddressStr).getBytes(); |
| 313 | |
| 314 | ARP arpRequest = new ARP(); |
| 315 | arpRequest.setHardwareType(ARP.HW_TYPE_ETHERNET) |
| 316 | .setProtocolType(ARP.PROTO_TYPE_IP) |
| 317 | .setHardwareAddressLength( |
| 318 | (byte) Ethernet.DATALAYER_ADDRESS_LENGTH) |
| 319 | .setProtocolAddressLength((byte) IPv4.ADDRESS_LENGTH) |
| 320 | .setOpCode(ARP.OP_REQUEST) |
| 321 | .setSenderHardwareAddress(senderMacAddress) |
| 322 | .setTargetHardwareAddress(MacAddress.NONE.getBytes()) |
| 323 | .setSenderProtocolAddress(senderIpAddress) |
| 324 | .setTargetProtocolAddress(targetAddress.getBytes()); |
| 325 | |
| 326 | Ethernet eth = new Ethernet(); |
| 327 | eth.setDestinationMACAddress(MacAddress.BROADCAST.getBytes()) |
| 328 | .setSourceMACAddress(senderMacAddress) |
| 329 | .setEtherType(Ethernet.TYPE_ARP).setPayload(arpRequest); |
| 330 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 331 | /* Broadcast the ARP request to all switch ports |
| 332 | * that subnets are connected to except the port from which |
| 333 | * ARP request is received |
| 334 | */ |
| 335 | for (Integer portNo : getSwitchSubnetPorts(sw, targetAddress)) { |
| 336 | if (portNo.shortValue() == inPort.getPortNumber().shortValue()) |
| 337 | continue; |
| 338 | log.debug("ArpHandler: Sending ARP request on switch {} port {}", |
| 339 | sw.getDpid(), portNo.shortValue()); |
Srikanth Vavilapalli | a95d583 | 2014-09-24 16:38:52 -0700 | [diff] [blame] | 340 | sendPacketOut(sw, eth, portNo.shortValue()); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 341 | } |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | /** |
| 345 | * Send PACKET_OUT packet to switch |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 346 | * |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 347 | * @param sw Switch to send the packet to |
| 348 | * @param packet Packet to send |
| 349 | * @param switchPort port to send (if -1, broadcast) |
| 350 | */ |
| 351 | private void sendPacketOut(Switch sw, Ethernet packet, short port) { |
| 352 | |
| 353 | IOFSwitch ofSwitch = floodlightProvider.getMasterSwitch(sw.getDpid().value()); |
| 354 | OFFactory factory = ofSwitch.getFactory(); |
| 355 | |
| 356 | List<OFAction> actions = new ArrayList<>(); |
| 357 | |
| 358 | if (port > 0) { |
| 359 | OFAction outport = factory.actions().output(OFPort.of(port), Short.MAX_VALUE); |
| 360 | actions.add(outport); |
| 361 | } |
| 362 | else { |
| 363 | Iterator<Port> iter = sw.getPorts().iterator(); |
| 364 | while (iter.hasNext()) { |
| 365 | Port p = iter.next(); |
| 366 | int pnum = p.getPortNumber().shortValue(); |
| 367 | if (U32.of(pnum).compareTo(U32.of(OFPort.MAX.getPortNumber())) < 1) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 368 | OFAction outport = factory.actions().output( |
| 369 | OFPort.of(p.getNumber().shortValue()), |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 370 | Short.MAX_VALUE); |
| 371 | actions.add(outport); |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | OFPacketOut po = factory.buildPacketOut() |
| 377 | .setData(packet.serialize()) |
| 378 | .setActions(actions) |
| 379 | .build(); |
| 380 | |
| 381 | flowPusher.add(sw.getDpid(), po); |
| 382 | } |
| 383 | |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 384 | /** |
| 385 | * Check if the source IP and destination IP are in the same subnet |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 386 | * |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 387 | * @param sw Switch |
| 388 | * @param ipv4 IP address to check |
| 389 | * @return return true if the IP packet is within the same subnet |
| 390 | */ |
| 391 | private boolean inSameSubnet(Switch sw, IPv4 ipv4) { |
| 392 | |
| 393 | String gwIpSrc = getGwIpForSubnet(ipv4.getSourceAddress()); |
| 394 | String gwIpDest = getGwIpForSubnet(ipv4.getDestinationAddress()); |
| 395 | |
| 396 | if (gwIpSrc.equals(gwIpDest)) { |
| 397 | return true; |
| 398 | } |
| 399 | else |
| 400 | return false; |
| 401 | } |
| 402 | |
| 403 | /** |
| 404 | * Get router IP address for the given IP address |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 405 | * |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 406 | * @param sourceAddress |
| 407 | * @return |
| 408 | */ |
| 409 | private String getGwIpForSubnet(int sourceAddress) { |
| 410 | |
| 411 | String gwIp = null; |
| 412 | IPv4Address srcIp = IPv4Address.of(sourceAddress); |
| 413 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 414 | for (Switch sw : mutableTopology.getSwitches()) { |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 415 | |
| 416 | String subnets = sw.getStringAttribute("subnets"); |
| 417 | try { |
| 418 | JSONArray arry = new JSONArray(subnets); |
| 419 | for (int i = 0; i < arry.length(); i++) { |
| 420 | String subnetIpSlash = (String) arry.getJSONObject(i).get("subnetIp"); |
| 421 | if (srManager.netMatch(subnetIpSlash, srcIp.toString())) { |
| 422 | gwIp = subnetIpSlash; |
| 423 | } |
| 424 | } |
| 425 | } catch (JSONException e) { |
| 426 | // TODO Auto-generated catch block |
| 427 | e.printStackTrace(); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | return gwIp; |
| 432 | } |
| 433 | |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 434 | } |