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; |
| 12 | import java.util.Iterator; |
| 13 | import java.util.List; |
| 14 | |
| 15 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 16 | import net.floodlightcontroller.core.IOFSwitch; |
| 17 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 18 | import net.floodlightcontroller.util.MACAddress; |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 19 | import net.onrc.onos.core.flowprogrammer.IFlowPusherService; |
| 20 | import net.onrc.onos.core.packet.ARP; |
| 21 | import net.onrc.onos.core.packet.Ethernet; |
| 22 | import net.onrc.onos.core.packet.IPv4; |
| 23 | import net.onrc.onos.core.topology.ITopologyService; |
| 24 | import net.onrc.onos.core.topology.MutableTopology; |
| 25 | import net.onrc.onos.core.topology.Port; |
| 26 | import net.onrc.onos.core.topology.Switch; |
| 27 | |
| 28 | import org.json.JSONArray; |
| 29 | import org.json.JSONException; |
| 30 | import org.projectfloodlight.openflow.protocol.OFFactory; |
| 31 | import org.projectfloodlight.openflow.protocol.OFPacketOut; |
| 32 | import org.projectfloodlight.openflow.protocol.action.OFAction; |
| 33 | import org.projectfloodlight.openflow.types.IPv4Address; |
| 34 | import org.projectfloodlight.openflow.types.MacAddress; |
| 35 | import org.projectfloodlight.openflow.types.OFPort; |
| 36 | import org.projectfloodlight.openflow.types.U32; |
| 37 | import org.slf4j.Logger; |
| 38 | import org.slf4j.LoggerFactory; |
| 39 | |
| 40 | import com.esotericsoftware.minlog.Log; |
| 41 | |
| 42 | /** |
| 43 | * Handling ARP requests to switches for Segment Routing. |
| 44 | * <p/> |
| 45 | * The module is for handling ARP requests to switches. It sends ARP response for any known |
| 46 | * hosts to the controllers. |
| 47 | * TODO: need to check the network config file for all hosts and packets |
| 48 | */ |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame^] | 49 | public class ArpHandler { |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 50 | |
| 51 | private static final Logger log = LoggerFactory |
| 52 | .getLogger(ArpHandler.class); |
| 53 | |
| 54 | private IFloodlightProviderService floodlightProvider; |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 55 | private IFlowPusherService flowPusher; |
| 56 | private ITopologyService topologyService; |
| 57 | private MutableTopology mutableTopology; |
| 58 | //private List<ArpEntry> arpEntries; |
| 59 | private SegmentRoutingManager srManager; |
| 60 | |
| 61 | private static final short IDLE_TIMEOUT = 0; |
| 62 | private static final short HARD_TIMEOUT = 0; |
| 63 | |
| 64 | private static final int TABLE_VLAN = 0; |
| 65 | private static final int TABLE_TMAC = 1; |
| 66 | private static final int TABLE_IPv4_UNICAST = 2; |
| 67 | private static final int TABLE_MPLS = 3; |
| 68 | private static final int TABLE_META = 4; |
| 69 | private static final int TABLE_ACL = 5; |
| 70 | |
| 71 | private static final short MAX_PRIORITY = (short) 0xffff; |
| 72 | private static final short SLASH_24_PRIORITY = (short) 0xfff0; |
| 73 | private static final short SLASH_16_PRIORITY = (short) 0xff00; |
| 74 | private static final short SLASH_8_PRIORITY = (short) 0xf000; |
| 75 | private static final short MIN_PRIORITY = 0x0; |
| 76 | |
| 77 | |
| 78 | /* |
| 79 | * Default Constructor |
| 80 | */ |
| 81 | public ArpHandler(FloodlightModuleContext context, SegmentRoutingManager segmentRoutingManager) { |
| 82 | |
| 83 | this.floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 84 | this.flowPusher = context.getServiceImpl(IFlowPusherService.class); |
| 85 | this.topologyService = context.getServiceImpl(ITopologyService.class); |
| 86 | this.srManager = segmentRoutingManager; |
| 87 | this.mutableTopology = topologyService.getTopology(); |
| 88 | |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 89 | Log.debug("Arp Handler is initialized"); |
| 90 | |
| 91 | } |
| 92 | |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame^] | 93 | public void processPacketIn(Switch sw, Port inPort, Ethernet payload){ |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 94 | |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame^] | 95 | log.debug("ArpHandler: Received a ARP packet from sw {} ", sw.getDpid()); |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 96 | |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame^] | 97 | ARP arp = (ARP)payload.getPayload(); |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 98 | |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame^] | 99 | if (arp.getOpCode() == ARP.OP_REQUEST) { |
| 100 | log.debug("ArpHandler: Received a ARP Requestfrom sw {} ", sw.getDpid()); |
| 101 | handleArpRequest(sw, inPort, payload); |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 102 | } |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame^] | 103 | byte[] senderMacAddressByte = arp.getSenderHardwareAddress(); |
| 104 | IPv4Address hostIpAddress = IPv4Address.of(arp.getSenderProtocolAddress()); |
| 105 | log.debug("ArpHandler: Add IP route to Host {} ", hostIpAddress); |
| 106 | srManager.addRouteToHost(sw,hostIpAddress.getInt(), senderMacAddressByte); |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 109 | /** |
| 110 | * Send an ARP response for the ARP request to the known switches |
| 111 | * |
| 112 | * @param sw Switch |
| 113 | * @param inPort port to send ARP response to |
| 114 | * @param arpRequest ARP request packet to handle |
| 115 | */ |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame^] | 116 | private void handleArpRequest(Switch sw, Port inPort, Ethernet payload) { |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 117 | |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame^] | 118 | ARP arpRequest = (ARP)payload.getPayload(); |
Srikanth Vavilapalli | 7f479d9 | 2014-09-24 13:36:46 -0700 | [diff] [blame] | 119 | List<String> subnetGatewayIPs = getSubnetGatewayIps(sw); |
| 120 | String switchMacAddressStr = sw.getStringAttribute("routerMac"); |
| 121 | if (!subnetGatewayIPs.isEmpty()) { |
| 122 | IPv4Address targetProtocolAddress = IPv4Address.of(arpRequest.getTargetProtocolAddress()); |
| 123 | // Do we have to check port also ?? |
| 124 | if (subnetGatewayIPs.contains(targetProtocolAddress.toString())) { |
| 125 | MACAddress targetMac = MACAddress.valueOf(switchMacAddressStr); |
| 126 | |
| 127 | ARP arpReply = new ARP(); |
| 128 | arpReply.setHardwareType(ARP.HW_TYPE_ETHERNET) |
| 129 | .setProtocolType(ARP.PROTO_TYPE_IP) |
| 130 | .setHardwareAddressLength( |
| 131 | (byte) Ethernet.DATALAYER_ADDRESS_LENGTH) |
| 132 | .setProtocolAddressLength((byte) IPv4.ADDRESS_LENGTH) |
| 133 | .setOpCode(ARP.OP_REPLY) |
| 134 | .setSenderHardwareAddress(targetMac.toBytes()) |
| 135 | .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress()) |
| 136 | .setTargetHardwareAddress(arpRequest.getSenderHardwareAddress()) |
| 137 | .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress()); |
| 138 | |
| 139 | Ethernet eth = new Ethernet(); |
| 140 | eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress()) |
| 141 | .setSourceMACAddress(targetMac.toBytes()) |
| 142 | .setEtherType(Ethernet.TYPE_ARP).setPayload(arpReply); |
| 143 | |
| 144 | sendPacketOut(sw, eth, inPort.getPortNumber().shortValue()); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Retrieve Gateway IP address of all subnets defined in net config file |
| 151 | * |
| 152 | * @param sw Switch to retrieve subnet GW IPs for |
| 153 | * @return list of GW IP addresses for all subnets |
| 154 | */ |
| 155 | private List<String> getSubnetGatewayIps(Switch sw) { |
| 156 | |
| 157 | List<String> gatewayIps = new ArrayList<String>(); |
| 158 | |
| 159 | String subnets = sw.getStringAttribute("subnets"); |
| 160 | try { |
| 161 | JSONArray arry = new JSONArray(subnets); |
| 162 | for (int i = 0; i < arry.length(); i++) { |
| 163 | String subnetIpSlash = (String) arry.getJSONObject(i).get("subnetIp"); |
| 164 | if (subnetIpSlash != null) { |
| 165 | String subnetIp = subnetIpSlash.substring(0, subnetIpSlash.indexOf('/')); |
| 166 | gatewayIps.add(subnetIp); |
| 167 | } |
| 168 | } |
| 169 | } catch (JSONException e) { |
| 170 | // TODO Auto-generated catch block |
| 171 | e.printStackTrace(); |
| 172 | } |
| 173 | |
| 174 | return gatewayIps; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Send an ARP request |
| 179 | * |
| 180 | * @param sw Switch |
| 181 | * @param targetAddress Target IP address |
| 182 | * @param inPort Port to send the ARP request |
| 183 | * |
| 184 | */ |
| 185 | public void sendArpRequest(Switch sw, int targetAddressInt, Port inPort) { |
| 186 | |
| 187 | IPv4Address targetAddress = IPv4Address.of(targetAddressInt); |
| 188 | String senderMacAddressStr = sw.getStringAttribute("routerMac"); |
| 189 | String senderIpAddressSlash = sw.getStringAttribute("routerIp"); |
| 190 | if (senderMacAddressStr == null || senderIpAddressSlash == null) |
| 191 | return; |
| 192 | String senderIpAddressStr = |
| 193 | senderIpAddressSlash.substring(0, senderIpAddressSlash.indexOf('/')); |
| 194 | byte[] senderMacAddress = MacAddress.of(senderMacAddressStr).getBytes(); |
| 195 | byte[] senderIpAddress = IPv4Address.of(senderIpAddressStr).getBytes(); |
| 196 | |
| 197 | ARP arpRequest = new ARP(); |
| 198 | arpRequest.setHardwareType(ARP.HW_TYPE_ETHERNET) |
| 199 | .setProtocolType(ARP.PROTO_TYPE_IP) |
| 200 | .setHardwareAddressLength( |
| 201 | (byte) Ethernet.DATALAYER_ADDRESS_LENGTH) |
| 202 | .setProtocolAddressLength((byte) IPv4.ADDRESS_LENGTH) |
| 203 | .setOpCode(ARP.OP_REQUEST) |
| 204 | .setSenderHardwareAddress(senderMacAddress) |
| 205 | .setTargetHardwareAddress(MacAddress.NONE.getBytes()) |
| 206 | .setSenderProtocolAddress(senderIpAddress) |
| 207 | .setTargetProtocolAddress(targetAddress.getBytes()); |
| 208 | |
| 209 | Ethernet eth = new Ethernet(); |
| 210 | eth.setDestinationMACAddress(MacAddress.BROADCAST.getBytes()) |
| 211 | .setSourceMACAddress(senderMacAddress) |
| 212 | .setEtherType(Ethernet.TYPE_ARP).setPayload(arpRequest); |
| 213 | |
| 214 | sendPacketOut(sw, eth, (short)-1); |
| 215 | |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Send PACKET_OUT packet to switch |
| 220 | * |
| 221 | * @param sw Switch to send the packet to |
| 222 | * @param packet Packet to send |
| 223 | * @param switchPort port to send (if -1, broadcast) |
| 224 | */ |
| 225 | private void sendPacketOut(Switch sw, Ethernet packet, short port) { |
| 226 | |
| 227 | IOFSwitch ofSwitch = floodlightProvider.getMasterSwitch(sw.getDpid().value()); |
| 228 | OFFactory factory = ofSwitch.getFactory(); |
| 229 | |
| 230 | List<OFAction> actions = new ArrayList<>(); |
| 231 | |
| 232 | if (port > 0) { |
| 233 | OFAction outport = factory.actions().output(OFPort.of(port), Short.MAX_VALUE); |
| 234 | actions.add(outport); |
| 235 | } |
| 236 | else { |
| 237 | Iterator<Port> iter = sw.getPorts().iterator(); |
| 238 | while (iter.hasNext()) { |
| 239 | Port p = iter.next(); |
| 240 | int pnum = p.getPortNumber().shortValue(); |
| 241 | if (U32.of(pnum).compareTo(U32.of(OFPort.MAX.getPortNumber())) < 1) { |
| 242 | OFAction outport = factory.actions().output(OFPort.of(p.getNumber().shortValue()), |
| 243 | Short.MAX_VALUE); |
| 244 | actions.add(outport); |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | OFPacketOut po = factory.buildPacketOut() |
| 250 | .setData(packet.serialize()) |
| 251 | .setActions(actions) |
| 252 | .build(); |
| 253 | |
| 254 | flowPusher.add(sw.getDpid(), po); |
| 255 | } |
| 256 | |
| 257 | } |
| 258 | |