Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -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 | |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 9 | package net.onrc.onos.apps.segmentrouting; |
| 10 | |
| 11 | import java.util.ArrayList; |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 12 | import java.util.LinkedList; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 13 | import java.util.List; |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 14 | import java.util.Queue; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 15 | |
| 16 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 17 | import net.floodlightcontroller.core.IOFSwitch; |
| 18 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 19 | import net.onrc.onos.core.flowprogrammer.IFlowPusherService; |
| 20 | import net.onrc.onos.core.packet.Ethernet; |
Sangho Shin | 79c8d45 | 2014-09-18 09:50:21 -0700 | [diff] [blame] | 21 | import net.onrc.onos.core.packet.ICMP; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 22 | import net.onrc.onos.core.packet.IPv4; |
Srikanth Vavilapalli | 5ddf8f0 | 2014-09-24 11:02:28 -0700 | [diff] [blame] | 23 | import net.onrc.onos.core.topology.Host; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 24 | import net.onrc.onos.core.topology.ITopologyService; |
| 25 | import net.onrc.onos.core.topology.MutableTopology; |
| 26 | import net.onrc.onos.core.topology.Port; |
| 27 | import net.onrc.onos.core.topology.Switch; |
Sangho Shin | 79c8d45 | 2014-09-18 09:50:21 -0700 | [diff] [blame] | 28 | import net.onrc.onos.core.util.SwitchPort; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 29 | |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 30 | import org.json.JSONArray; |
| 31 | import org.json.JSONException; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 32 | import org.projectfloodlight.openflow.protocol.OFFactory; |
| 33 | import org.projectfloodlight.openflow.protocol.OFMatchV3; |
| 34 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 35 | import org.projectfloodlight.openflow.protocol.OFOxmList; |
Sangho Shin | f41ac0a | 2014-09-19 09:50:30 -0700 | [diff] [blame] | 36 | import org.projectfloodlight.openflow.protocol.OFPacketOut; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 37 | import org.projectfloodlight.openflow.protocol.action.OFAction; |
| 38 | import org.projectfloodlight.openflow.protocol.instruction.OFInstruction; |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 39 | import org.projectfloodlight.openflow.protocol.oxm.OFOxmInPort; |
Sangho Shin | f41ac0a | 2014-09-19 09:50:30 -0700 | [diff] [blame] | 40 | import org.projectfloodlight.openflow.protocol.oxm.OFOxmMplsLabel; |
| 41 | import org.projectfloodlight.openflow.protocol.oxm.OFOxmVlanVid; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 42 | import org.projectfloodlight.openflow.types.EthType; |
| 43 | import org.projectfloodlight.openflow.types.IPv4Address; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 44 | import org.projectfloodlight.openflow.types.OFBufferId; |
| 45 | import org.projectfloodlight.openflow.types.OFPort; |
Sangho Shin | f41ac0a | 2014-09-19 09:50:30 -0700 | [diff] [blame] | 46 | import org.projectfloodlight.openflow.types.OFVlanVidMatch; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 47 | import org.projectfloodlight.openflow.types.TableId; |
Sangho Shin | f41ac0a | 2014-09-19 09:50:30 -0700 | [diff] [blame] | 48 | import org.projectfloodlight.openflow.types.U32; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 49 | import org.slf4j.Logger; |
| 50 | import org.slf4j.LoggerFactory; |
| 51 | |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 52 | public class IcmpHandler { |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 53 | |
| 54 | private SegmentRoutingManager srManager; |
| 55 | private IFloodlightProviderService floodlightProvider; |
| 56 | private MutableTopology mutableTopology; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 57 | private ITopologyService topologyService; |
| 58 | private static final Logger log = LoggerFactory |
Sangho Shin | f41ac0a | 2014-09-19 09:50:30 -0700 | [diff] [blame] | 59 | .getLogger(IcmpHandler.class); |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 60 | |
| 61 | private IFlowPusherService flowPusher; |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 62 | private boolean controllerPortAllowed = false; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 63 | |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 64 | private Queue<IPv4> icmpQueue = new LinkedList<IPv4>(); |
| 65 | |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 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 | |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 79 | private static final int ICMP_TYPE_ECHO = 0x08; |
| 80 | private static final int ICMP_TYPE_REPLY = 0x00; |
| 81 | |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 82 | public IcmpHandler(FloodlightModuleContext context, SegmentRoutingManager manager) { |
| 83 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame^] | 84 | this.floodlightProvider = context |
| 85 | .getServiceImpl(IFloodlightProviderService.class); |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 86 | this.flowPusher = context.getServiceImpl(IFlowPusherService.class); |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 87 | this.topologyService = context.getServiceImpl(ITopologyService.class); |
| 88 | this.mutableTopology = topologyService.getTopology(); |
| 89 | |
| 90 | this.srManager = manager; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 93 | /** |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame^] | 94 | * handle ICMP packets If it is for ICMP echo to router IP or any subnet GW |
| 95 | * IP, then send ICMP response on behalf of the switch. If it is for any |
| 96 | * hosts in subnets of the switches, but if the MAC address is not known, |
| 97 | * then send an ARP request to the subent. If the MAC address is known, then |
| 98 | * set the routing rule to the switch |
| 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 | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 104 | public void processPacketIn(Switch sw, Port inPort, Ethernet payload) { |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 105 | |
| 106 | if (payload.getEtherType() == Ethernet.TYPE_IPV4) { |
| 107 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame^] | 108 | IPv4 ipv4 = (IPv4) payload.getPayload(); |
Sangho Shin | 79c8d45 | 2014-09-18 09:50:21 -0700 | [diff] [blame] | 109 | |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 110 | if (ipv4.getProtocol() == IPv4.PROTOCOL_ICMP) { |
Srikanth Vavilapalli | 5ddf8f0 | 2014-09-24 11:02:28 -0700 | [diff] [blame] | 111 | |
| 112 | log.debug("ICMPHandler: Received a ICMP packet {} from sw {} ", |
| 113 | payload.toString(), sw.getDpid()); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 114 | IPv4Address destinationAddress = |
| 115 | IPv4Address.of(ipv4.getDestinationAddress()); |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 116 | |
Sangho Shin | 79c8d45 | 2014-09-18 09:50:21 -0700 | [diff] [blame] | 117 | // Check if it is ICMP request to the switch |
| 118 | String switchIpAddressSlash = sw.getStringAttribute("routerIp"); |
| 119 | if (switchIpAddressSlash != null) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame^] | 120 | String switchIpAddressStr = switchIpAddressSlash.substring(0, |
| 121 | switchIpAddressSlash.indexOf('/')); |
Sangho Shin | 79c8d45 | 2014-09-18 09:50:21 -0700 | [diff] [blame] | 122 | IPv4Address switchIpAddress = IPv4Address.of(switchIpAddressStr); |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 123 | List<String> gatewayIps = getSubnetGatewayIps(sw); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame^] | 124 | if (((ICMP) ipv4.getPayload()).getIcmpType() == ICMP_TYPE_ECHO && |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 125 | (destinationAddress.getInt() == switchIpAddress.getInt() || |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame^] | 126 | gatewayIps.contains(destinationAddress.toString()))) { |
Srikanth Vavilapalli | 5ddf8f0 | 2014-09-24 11:02:28 -0700 | [diff] [blame] | 127 | log.debug("ICMPHandler: ICMP packet for sw {} and " |
| 128 | + "sending ICMP response ", sw.getDpid()); |
Sangho Shin | 79c8d45 | 2014-09-18 09:50:21 -0700 | [diff] [blame] | 129 | sendICMPResponse(sw, inPort, payload); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 130 | srManager.getIpPacketFromQueue(destinationAddress.getBytes()); |
Sangho Shin | 79c8d45 | 2014-09-18 09:50:21 -0700 | [diff] [blame] | 131 | return; |
| 132 | } |
| 133 | } |
Srikanth Vavilapalli | 5ddf8f0 | 2014-09-24 11:02:28 -0700 | [diff] [blame] | 134 | |
| 135 | /* Check if ICMP is for any switch known host */ |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame^] | 136 | for (Host host : sw.getHosts()) { |
Srikanth Vavilapalli | 5ddf8f0 | 2014-09-24 11:02:28 -0700 | [diff] [blame] | 137 | IPv4Address hostIpAddress = |
| 138 | IPv4Address.of(host.getIpAddress()); |
| 139 | if (hostIpAddress != null && |
| 140 | hostIpAddress.equals(destinationAddress)) { |
| 141 | /* TODO: We should not have come here as ARP itself |
| 142 | * would have installed a Route to the host. See if |
| 143 | * we can remove this code |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 144 | * - It can happen when the rule is set later in switches |
| 145 | * (Ex: Ping reply arrives before the rules is set in the table) |
| 146 | * - The rule must be set by ARP handler. But, we set the rule |
| 147 | * again just in case and flush any pending packets to the host. |
Srikanth Vavilapalli | 5ddf8f0 | 2014-09-24 11:02:28 -0700 | [diff] [blame] | 148 | */ |
| 149 | log.debug("ICMPHandler: ICMP request for known host {}", |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame^] | 150 | hostIpAddress); |
Srikanth Vavilapalli | 5ddf8f0 | 2014-09-24 11:02:28 -0700 | [diff] [blame] | 151 | byte[] destinationMacAddress = host.getMacAddress().toBytes(); |
| 152 | srManager.addRouteToHost(sw, |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 153 | destinationAddress.getInt(), destinationMacAddress); |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 154 | |
| 155 | byte[] destIp = destinationAddress.getBytes(); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame^] | 156 | for (IPv4 ipPacket : srManager.getIpPacketFromQueue(destIp)) { |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 157 | if (ipPacket != null && !inSameSubnet(sw, ipPacket)) { |
| 158 | Ethernet eth = new Ethernet(); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame^] | 159 | eth.setDestinationMACAddress(payload |
| 160 | .getSourceMACAddress()); |
| 161 | eth.setSourceMACAddress(sw |
| 162 | .getStringAttribute("routerMac")); |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 163 | eth.setEtherType(Ethernet.TYPE_IPV4); |
| 164 | eth.setPayload(ipPacket); |
| 165 | sendPacketOut(sw, eth, inPort.getSwitchPort(), false); |
| 166 | } |
| 167 | } |
| 168 | |
Srikanth Vavilapalli | 5ddf8f0 | 2014-09-24 11:02:28 -0700 | [diff] [blame] | 169 | return; |
| 170 | } |
| 171 | } |
| 172 | /* ICMP for an unknown host */ |
| 173 | log.debug("ICMPHandler: ICMP request for unknown host {}" |
| 174 | + " and sending ARP request", destinationAddress); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 175 | srManager.sendArpRequest(sw, destinationAddress.getInt(), inPort); |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | } |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 179 | } |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 180 | |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 181 | /** |
| 182 | * Retrieve Gateway IP address of all subnets defined in net config file |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame^] | 183 | * |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 184 | * @param sw Switch to retrieve subnet GW IPs for |
| 185 | * @return list of GW IP addresses for all subnets |
| 186 | */ |
| 187 | private List<String> getSubnetGatewayIps(Switch sw) { |
| 188 | |
| 189 | List<String> gatewayIps = new ArrayList<String>(); |
| 190 | |
| 191 | String subnets = sw.getStringAttribute("subnets"); |
| 192 | try { |
| 193 | JSONArray arry = new JSONArray(subnets); |
| 194 | for (int i = 0; i < arry.length(); i++) { |
| 195 | String subnetIpSlash = (String) arry.getJSONObject(i).get("subnetIp"); |
| 196 | if (subnetIpSlash != null) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame^] | 197 | String subnetIp = subnetIpSlash.substring(0, |
| 198 | subnetIpSlash.indexOf('/')); |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 199 | gatewayIps.add(subnetIp); |
| 200 | } |
| 201 | } |
| 202 | } catch (JSONException e) { |
| 203 | // TODO Auto-generated catch block |
| 204 | e.printStackTrace(); |
| 205 | } |
| 206 | |
| 207 | return gatewayIps; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Sangho Shin | 79c8d45 | 2014-09-18 09:50:21 -0700 | [diff] [blame] | 210 | /** |
| 211 | * Send ICMP reply back |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame^] | 212 | * |
Sangho Shin | 79c8d45 | 2014-09-18 09:50:21 -0700 | [diff] [blame] | 213 | * @param sw Switch |
| 214 | * @param inPort Port the ICMP packet is forwarded from |
| 215 | * @param icmpRequest the ICMP request to handle |
| 216 | * @param destinationAddress destination address to send ICMP response to |
| 217 | */ |
| 218 | private void sendICMPResponse(Switch sw, Port inPort, Ethernet icmpRequest) { |
| 219 | |
| 220 | Ethernet icmpReplyEth = new Ethernet(); |
| 221 | |
| 222 | IPv4 icmpRequestIpv4 = (IPv4) icmpRequest.getPayload(); |
| 223 | IPv4 icmpReplyIpv4 = new IPv4(); |
| 224 | int destAddress = icmpRequestIpv4.getDestinationAddress(); |
| 225 | icmpReplyIpv4.setDestinationAddress(icmpRequestIpv4.getSourceAddress()); |
| 226 | icmpReplyIpv4.setSourceAddress(destAddress); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame^] | 227 | icmpReplyIpv4.setTtl((byte) 64); |
| 228 | icmpReplyIpv4.setChecksum((short) 0); |
Sangho Shin | 79c8d45 | 2014-09-18 09:50:21 -0700 | [diff] [blame] | 229 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame^] | 230 | ICMP icmpReply = (ICMP) icmpRequestIpv4.getPayload().clone(); |
| 231 | icmpReply.setIcmpCode((byte) 0x00); |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 232 | icmpReply.setIcmpType((byte) ICMP_TYPE_REPLY); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame^] | 233 | icmpReply.setChecksum((short) 0); |
Sangho Shin | 79c8d45 | 2014-09-18 09:50:21 -0700 | [diff] [blame] | 234 | |
| 235 | icmpReplyIpv4.setPayload(icmpReply); |
| 236 | |
| 237 | icmpReplyEth.setPayload(icmpReplyIpv4); |
| 238 | icmpReplyEth.setEtherType(Ethernet.TYPE_IPV4); |
| 239 | icmpReplyEth.setDestinationMACAddress(icmpRequest.getSourceMACAddress()); |
| 240 | icmpReplyEth.setSourceMACAddress(icmpRequest.getDestinationMACAddress()); |
| 241 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame^] | 242 | sendPacketOut(sw, icmpReplyEth, |
| 243 | new SwitchPort(sw.getDpid(), inPort.getPortNumber()), false); |
Sangho Shin | 79c8d45 | 2014-09-18 09:50:21 -0700 | [diff] [blame] | 244 | |
| 245 | log.debug("Send an ICMP response {}", icmpReplyIpv4.toString()); |
| 246 | |
| 247 | } |
| 248 | |
Sangho Shin | f41ac0a | 2014-09-19 09:50:30 -0700 | [diff] [blame] | 249 | /** |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame^] | 250 | * Send PACKET_OUT message with actions If switches support OFPP_TABLE |
| 251 | * action, it sends out packet to TABLE port Otherwise, it sends the packet |
| 252 | * to the port the packet came from (in this case, MPLS label is added if |
| 253 | * the packet needs go through transit switches) |
| 254 | * |
| 255 | * @param sw Switch the packet came from |
Sangho Shin | f41ac0a | 2014-09-19 09:50:30 -0700 | [diff] [blame] | 256 | * @param packet Ethernet packet to send |
| 257 | * @param switchPort port to send the packet |
| 258 | */ |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame^] | 259 | private void sendPacketOut(Switch sw, Ethernet packet, SwitchPort switchPort, |
| 260 | boolean supportOfppTable) { |
Sangho Shin | f41ac0a | 2014-09-19 09:50:30 -0700 | [diff] [blame] | 261 | |
| 262 | boolean sameSubnet = false; |
| 263 | IOFSwitch ofSwitch = floodlightProvider.getMasterSwitch(sw.getDpid().value()); |
| 264 | OFFactory factory = ofSwitch.getFactory(); |
| 265 | |
| 266 | List<OFAction> actions = new ArrayList<>(); |
| 267 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame^] | 268 | // If OFPP_TABLE action is not supported in the switch, MPLS label needs |
| 269 | // to be set |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 270 | // if the packet needs to be delivered crossing switches |
| 271 | if (!supportOfppTable) { |
| 272 | // Check if the destination is the host attached to the switch |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame^] | 273 | int destinationAddress = ((IPv4) packet.getPayload()).getDestinationAddress(); |
| 274 | for (net.onrc.onos.core.topology.Host host : mutableTopology |
| 275 | .getHosts(switchPort)) { |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 276 | IPv4Address hostIpAddress = IPv4Address.of(host.getIpAddress()); |
| 277 | if (hostIpAddress != null && hostIpAddress.getInt() == destinationAddress) { |
| 278 | sameSubnet = true; |
| 279 | break; |
| 280 | } |
Sangho Shin | f41ac0a | 2014-09-19 09:50:30 -0700 | [diff] [blame] | 281 | } |
Sangho Shin | f41ac0a | 2014-09-19 09:50:30 -0700 | [diff] [blame] | 282 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame^] | 283 | IPv4Address targetAddress = IPv4Address.of(((IPv4) packet.getPayload()) |
| 284 | .getDestinationAddress()); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 285 | String destMacAddress = packet.getDestinationMAC().toString(); |
| 286 | // If the destination host is not attached in the switch |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame^] | 287 | // and the destination is not the neighbor switch, then add MPLS |
| 288 | // label |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 289 | String targetMac = getRouterMACFromConfig(targetAddress); |
| 290 | if (!sameSubnet && !targetMac.equals(destMacAddress)) { |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 291 | int mplsLabel = getMplsLabelFromConfig(targetAddress); |
| 292 | if (mplsLabel > 0) { |
| 293 | OFAction pushlabel = factory.actions().pushMpls(EthType.MPLS_UNICAST); |
| 294 | OFOxmMplsLabel l = factory.oxms() |
| 295 | .mplsLabel(U32.of(mplsLabel)); |
| 296 | OFAction setlabelid = factory.actions().buildSetField() |
| 297 | .setField(l).build(); |
| 298 | OFAction copyTtlOut = factory.actions().copyTtlOut(); |
| 299 | actions.add(pushlabel); |
| 300 | actions.add(setlabelid); |
| 301 | actions.add(copyTtlOut); |
| 302 | } |
| 303 | } |
| 304 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame^] | 305 | OFAction outport = factory.actions().output( |
| 306 | OFPort.of(switchPort.getPortNumber().shortValue()), Short.MAX_VALUE); |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 307 | actions.add(outport); |
| 308 | } |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame^] | 309 | // If OFPP_TABLE action is supported, first set a rule to allow packet |
| 310 | // from CONTROLLER port. |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 311 | // Then, send the packet to the table port |
| 312 | else { |
| 313 | if (!controllerPortAllowed) { |
| 314 | addControlPortInVlanTable(sw); |
| 315 | controllerPortAllowed = true; |
| 316 | } |
| 317 | OFAction outport = factory.actions().output(OFPort.TABLE, Short.MAX_VALUE); |
| 318 | actions.add(outport); |
| 319 | } |
Sangho Shin | f41ac0a | 2014-09-19 09:50:30 -0700 | [diff] [blame] | 320 | |
| 321 | OFPacketOut po = factory.buildPacketOut() |
| 322 | .setData(packet.serialize()) |
| 323 | .setActions(actions) |
| 324 | .build(); |
| 325 | |
| 326 | flowPusher.add(sw.getDpid(), po); |
| 327 | } |
| 328 | |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 329 | /** |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 330 | * Get MPLS label for the target address from the network config file |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame^] | 331 | * |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 332 | * @param targetAddress - IP address of the target host |
| 333 | * @return MPLS label of the switch to send packets to the target address |
| 334 | */ |
| 335 | private int getMplsLabelFromConfig(IPv4Address targetAddress) { |
| 336 | |
| 337 | int mplsLabel = -1; |
| 338 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame^] | 339 | for (Switch sw : mutableTopology.getSwitches()) { |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 340 | |
| 341 | String subnets = sw.getStringAttribute("subnets"); |
| 342 | try { |
| 343 | JSONArray arry = new JSONArray(subnets); |
| 344 | for (int i = 0; i < arry.length(); i++) { |
| 345 | String subnetIp = (String) arry.getJSONObject(i).get("subnetIp"); |
| 346 | if (srManager.netMatch(subnetIp, targetAddress.toString())) { |
| 347 | String mplsLabelStr = sw.getStringAttribute("nodeSid"); |
| 348 | if (mplsLabelStr != null) |
| 349 | mplsLabel = Integer.parseInt(mplsLabelStr); |
| 350 | } |
| 351 | } |
| 352 | } catch (JSONException e) { |
| 353 | // TODO Auto-generated catch block |
| 354 | e.printStackTrace(); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | return mplsLabel; |
| 359 | } |
| 360 | |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 361 | /** |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame^] | 362 | * Get Router MAC Address for the target address from the network config |
| 363 | * file |
| 364 | * |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 365 | * @param targetAddress - IP address of the target host |
| 366 | * @return Router MAC of the switch to send packets to the target address |
| 367 | */ |
| 368 | private String getRouterMACFromConfig(IPv4Address targetAddress) { |
| 369 | |
| 370 | String routerMac = null; |
| 371 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame^] | 372 | for (Switch sw : mutableTopology.getSwitches()) { |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 373 | |
| 374 | String subnets = sw.getStringAttribute("subnets"); |
| 375 | try { |
| 376 | JSONArray arry = new JSONArray(subnets); |
| 377 | for (int i = 0; i < arry.length(); i++) { |
| 378 | String subnetIp = (String) arry.getJSONObject(i).get("subnetIp"); |
| 379 | if (srManager.netMatch(subnetIp, targetAddress.toString())) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame^] | 380 | routerMac = sw.getStringAttribute("routerMac"); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | } catch (JSONException e) { |
| 384 | // TODO Auto-generated catch block |
| 385 | e.printStackTrace(); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | return routerMac; |
| 390 | } |
Sangho Shin | f41ac0a | 2014-09-19 09:50:30 -0700 | [diff] [blame] | 391 | |
| 392 | /** |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame^] | 393 | * Add a new rule to VLAN table to forward packets from any port to the next |
| 394 | * table It is required to forward packets from controller to pipeline |
| 395 | * |
| 396 | * @param sw Switch the packet came from |
Sangho Shin | f41ac0a | 2014-09-19 09:50:30 -0700 | [diff] [blame] | 397 | */ |
| 398 | private void addControlPortInVlanTable(Switch sw) { |
| 399 | |
Sangho Shin | f41ac0a | 2014-09-19 09:50:30 -0700 | [diff] [blame] | 400 | IOFSwitch ofSwitch = floodlightProvider.getMasterSwitch(sw.getDpid().value()); |
| 401 | OFFactory factory = ofSwitch.getFactory(); |
| 402 | |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 403 | OFOxmInPort oxp = factory.oxms().inPort(OFPort.CONTROLLER); |
Sangho Shin | f41ac0a | 2014-09-19 09:50:30 -0700 | [diff] [blame] | 404 | OFOxmVlanVid oxv = factory.oxms() |
| 405 | .vlanVid(OFVlanVidMatch.UNTAGGED); |
| 406 | OFOxmList oxmList = OFOxmList.of(oxv); |
| 407 | |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 408 | /* Cqpd switch does not seems to support CONTROLLER port as in_port match rule */ |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame^] | 409 | // OFOxmList oxmList = OFOxmList.of(oxp, oxv); |
Sangho Shin | f41ac0a | 2014-09-19 09:50:30 -0700 | [diff] [blame] | 410 | |
| 411 | OFMatchV3 match = factory.buildMatchV3() |
| 412 | .setOxmList(oxmList) |
| 413 | .build(); |
| 414 | |
| 415 | OFInstruction gotoTbl = factory.instructions().buildGotoTable() |
| 416 | .setTableId(TableId.of(TABLE_TMAC)).build(); |
| 417 | List<OFInstruction> instructions = new ArrayList<OFInstruction>(); |
Sangho Shin | f41ac0a | 2014-09-19 09:50:30 -0700 | [diff] [blame] | 418 | instructions.add(gotoTbl); |
| 419 | OFMessage flowEntry = factory.buildFlowAdd() |
| 420 | .setTableId(TableId.of(TABLE_VLAN)) |
| 421 | .setMatch(match) |
| 422 | .setInstructions(instructions) |
| 423 | .setPriority(1000) // does not matter - all rules |
| 424 | // exclusive |
| 425 | .setBufferId(OFBufferId.NO_BUFFER) |
| 426 | .setIdleTimeout(0) |
| 427 | .setHardTimeout(0) |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame^] | 428 | // .setXid(getNextTransactionId()) |
Sangho Shin | f41ac0a | 2014-09-19 09:50:30 -0700 | [diff] [blame] | 429 | .build(); |
| 430 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame^] | 431 | flowPusher.add(sw.getDpid(), flowEntry); |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 432 | log.debug("Adding a new vlan-rules in sw {}", sw.getDpid()); |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 433 | |
| 434 | } |
| 435 | |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 436 | /** |
| 437 | * 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^] | 438 | * |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 439 | * @param sw Switch |
| 440 | * @param ipv4 IP address to check |
| 441 | * @return return true if the IP packet is within the same subnet |
| 442 | */ |
| 443 | private boolean inSameSubnet(Switch sw, IPv4 ipv4) { |
| 444 | |
| 445 | String gwIpSrc = getGwIpForSubnet(ipv4.getSourceAddress()); |
| 446 | String gwIpDest = getGwIpForSubnet(ipv4.getDestinationAddress()); |
| 447 | |
| 448 | if (gwIpSrc.equals(gwIpDest)) { |
| 449 | return true; |
| 450 | } |
| 451 | else |
| 452 | return false; |
| 453 | } |
| 454 | |
| 455 | /** |
| 456 | * Get router IP address for the given IP address |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame^] | 457 | * |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 458 | * @param sourceAddress |
| 459 | * @return |
| 460 | */ |
| 461 | private String getGwIpForSubnet(int sourceAddress) { |
| 462 | |
| 463 | String gwIp = null; |
| 464 | IPv4Address srcIp = IPv4Address.of(sourceAddress); |
| 465 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame^] | 466 | for (Switch sw : mutableTopology.getSwitches()) { |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 467 | |
| 468 | String subnets = sw.getStringAttribute("subnets"); |
| 469 | try { |
| 470 | JSONArray arry = new JSONArray(subnets); |
| 471 | for (int i = 0; i < arry.length(); i++) { |
| 472 | String subnetIpSlash = (String) arry.getJSONObject(i).get("subnetIp"); |
| 473 | if (srManager.netMatch(subnetIpSlash, srcIp.toString())) { |
| 474 | gwIp = subnetIpSlash; |
| 475 | } |
| 476 | } |
| 477 | } catch (JSONException e) { |
| 478 | // TODO Auto-generated catch block |
| 479 | e.printStackTrace(); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | return gwIp; |
| 484 | } |
| 485 | |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 486 | } |