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