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 | 01bca86 | 2014-09-12 11:18:59 -0700 | [diff] [blame] | 11 | import net.floodlightcontroller.core.IFloodlightProviderService; |
Sangho Shin | 01bca86 | 2014-09-12 11:18:59 -0700 | [diff] [blame] | 12 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
Sangho Shin | 01bca86 | 2014-09-12 11:18:59 -0700 | [diff] [blame] | 13 | import net.floodlightcontroller.util.MACAddress; |
| 14 | import net.onrc.onos.api.packet.IPacketListener; |
| 15 | import net.onrc.onos.api.packet.IPacketService; |
| 16 | import net.onrc.onos.core.flowprogrammer.IFlowPusherService; |
Sangho Shin | 01bca86 | 2014-09-12 11:18:59 -0700 | [diff] [blame] | 17 | import net.onrc.onos.core.packet.ARP; |
| 18 | import net.onrc.onos.core.packet.Ethernet; |
| 19 | import net.onrc.onos.core.packet.IPv4; |
Sangho Shin | 01bca86 | 2014-09-12 11:18:59 -0700 | [diff] [blame] | 20 | import net.onrc.onos.core.topology.ITopologyService; |
| 21 | import net.onrc.onos.core.topology.MutableTopology; |
| 22 | import net.onrc.onos.core.topology.Port; |
| 23 | import net.onrc.onos.core.topology.Switch; |
| 24 | import net.onrc.onos.core.util.SwitchPort; |
| 25 | |
Sangho Shin | 01bca86 | 2014-09-12 11:18:59 -0700 | [diff] [blame] | 26 | import org.projectfloodlight.openflow.types.IPv4Address; |
Sangho Shin | 01bca86 | 2014-09-12 11:18:59 -0700 | [diff] [blame] | 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
| 30 | import com.esotericsoftware.minlog.Log; |
| 31 | |
| 32 | /** |
| 33 | * Handling ARP requests to switches for Segment Routing. |
| 34 | * <p/> |
| 35 | * The module is for handling ARP requests to switches. It sends ARP response for any known |
| 36 | * hosts to the controllers. |
| 37 | * TODO: need to check the network config file for all hosts and packets |
| 38 | */ |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 39 | public class ArpHandler implements IPacketListener { |
Sangho Shin | 01bca86 | 2014-09-12 11:18:59 -0700 | [diff] [blame] | 40 | |
| 41 | private static final Logger log = LoggerFactory |
| 42 | .getLogger(ArpHandler.class); |
| 43 | |
| 44 | private IFloodlightProviderService floodlightProvider; |
| 45 | private IPacketService packetService; |
| 46 | private IFlowPusherService flowPusher; |
| 47 | private ITopologyService topologyService; |
| 48 | private MutableTopology mutableTopology; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 49 | //private List<ArpEntry> arpEntries; |
| 50 | private SegmentRoutingManager srManager; |
Sangho Shin | 01bca86 | 2014-09-12 11:18:59 -0700 | [diff] [blame] | 51 | |
| 52 | private static final short IDLE_TIMEOUT = 0; |
| 53 | private static final short HARD_TIMEOUT = 0; |
| 54 | |
| 55 | private static final int TABLE_VLAN = 0; |
| 56 | private static final int TABLE_TMAC = 1; |
| 57 | private static final int TABLE_IPv4_UNICAST = 2; |
| 58 | private static final int TABLE_MPLS = 3; |
| 59 | private static final int TABLE_META = 4; |
| 60 | private static final int TABLE_ACL = 5; |
| 61 | |
| 62 | private static final short MAX_PRIORITY = (short) 0xffff; |
| 63 | private static final short SLASH_24_PRIORITY = (short) 0xfff0; |
| 64 | private static final short SLASH_16_PRIORITY = (short) 0xff00; |
| 65 | private static final short SLASH_8_PRIORITY = (short) 0xf000; |
| 66 | private static final short MIN_PRIORITY = 0x0; |
| 67 | |
| 68 | |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 69 | /* |
| 70 | * Default Constructor |
| 71 | */ |
| 72 | public ArpHandler(FloodlightModuleContext context, SegmentRoutingManager segmentRoutingManager) { |
Sangho Shin | 01bca86 | 2014-09-12 11:18:59 -0700 | [diff] [blame] | 73 | |
Sangho Shin | 01bca86 | 2014-09-12 11:18:59 -0700 | [diff] [blame] | 74 | this.floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
| 75 | this.packetService = context.getServiceImpl(IPacketService.class); |
| 76 | this.flowPusher = context.getServiceImpl(IFlowPusherService.class); |
| 77 | this.topologyService = context.getServiceImpl(ITopologyService.class); |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 78 | this.srManager = segmentRoutingManager; |
| 79 | this.mutableTopology = topologyService.getTopology(); |
| 80 | |
| 81 | packetService.registerPacketListener(this); |
| 82 | //arpEntries = new ArrayList<ArpEntry>(); |
Sangho Shin | 01bca86 | 2014-09-12 11:18:59 -0700 | [diff] [blame] | 83 | |
| 84 | Log.debug("Arp Handler is initialized"); |
| 85 | |
| 86 | } |
| 87 | |
| 88 | @Override |
Sangho Shin | 01bca86 | 2014-09-12 11:18:59 -0700 | [diff] [blame] | 89 | public void receive(Switch sw, Port inPort, Ethernet payload) { |
| 90 | log.debug("Received a packet {} from sw {} ", payload.toString(), sw.getDpid()); |
| 91 | |
| 92 | if (payload.getEtherType() == Ethernet.TYPE_ARP) { |
| 93 | |
| 94 | ARP arp = (ARP)payload.getPayload(); |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 95 | srManager.updateArpCache(arp); |
Sangho Shin | 01bca86 | 2014-09-12 11:18:59 -0700 | [diff] [blame] | 96 | |
| 97 | if (arp.getOpCode() == ARP.OP_REQUEST) { |
| 98 | |
| 99 | handleArpRequest(sw, inPort, arp); |
| 100 | } |
| 101 | |
| 102 | } |
Sangho Shin | 01bca86 | 2014-09-12 11:18:59 -0700 | [diff] [blame] | 103 | |
| 104 | } |
| 105 | |
Sangho Shin | 01bca86 | 2014-09-12 11:18:59 -0700 | [diff] [blame] | 106 | |
| 107 | /** |
| 108 | * Send an ARP response for the ARP request to the known switches |
| 109 | * |
| 110 | * @param sw Switch |
| 111 | * @param inPort port to send ARP response to |
| 112 | * @param arpRequest ARP request packet to handle |
| 113 | */ |
| 114 | private void handleArpRequest(Switch sw, Port inPort, ARP arpRequest) { |
| 115 | |
| 116 | String switchIpAddressSlash = sw.getStringAttribute("routerIp"); |
| 117 | String switchMacAddressStr = sw.getStringAttribute("routerMac"); |
| 118 | if (switchIpAddressSlash != null && switchMacAddressStr != null) { |
| 119 | |
| 120 | String switchIpAddressStr = switchIpAddressSlash.substring(0, switchIpAddressSlash.indexOf('/')); |
| 121 | IPv4Address switchIpAddress = IPv4Address.of(switchIpAddressStr); |
| 122 | IPv4Address targetProtocolAddress = IPv4Address.of(arpRequest.getTargetProtocolAddress()); |
| 123 | if (targetProtocolAddress.equals(switchIpAddress)) { |
| 124 | MACAddress targetMac = MACAddress.valueOf(switchMacAddressStr); |
| 125 | |
| 126 | ARP arpReply = new ARP(); |
| 127 | arpReply.setHardwareType(ARP.HW_TYPE_ETHERNET) |
| 128 | .setProtocolType(ARP.PROTO_TYPE_IP) |
| 129 | .setHardwareAddressLength( |
| 130 | (byte) Ethernet.DATALAYER_ADDRESS_LENGTH) |
| 131 | .setProtocolAddressLength((byte) IPv4.ADDRESS_LENGTH) |
| 132 | .setOpCode(ARP.OP_REPLY) |
| 133 | .setSenderHardwareAddress(targetMac.toBytes()) |
| 134 | .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress()) |
| 135 | .setTargetHardwareAddress(arpRequest.getSenderHardwareAddress()) |
| 136 | .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress()); |
| 137 | |
| 138 | Ethernet eth = new Ethernet(); |
| 139 | eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress()) |
| 140 | .setSourceMACAddress(targetMac.toBytes()) |
| 141 | .setEtherType(Ethernet.TYPE_ARP).setPayload(arpReply); |
| 142 | |
| 143 | packetService.sendPacket(eth, new SwitchPort(sw.getDpid(), inPort.getPortNumber())); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
Sangho Shin | 01bca86 | 2014-09-12 11:18:59 -0700 | [diff] [blame] | 148 | |
Sangho Shin | 01bca86 | 2014-09-12 11:18:59 -0700 | [diff] [blame] | 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | } |