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