Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 1 | package net.onrc.onos.apps.segmentrouting; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.List; |
| 5 | |
| 6 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 7 | import net.floodlightcontroller.core.IOFSwitch; |
| 8 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 9 | import net.floodlightcontroller.util.MACAddress; |
| 10 | import net.onrc.onos.api.packet.IPacketListener; |
| 11 | import net.onrc.onos.api.packet.IPacketService; |
| 12 | import net.onrc.onos.core.flowprogrammer.IFlowPusherService; |
| 13 | import net.onrc.onos.core.packet.Ethernet; |
Sangho Shin | 79c8d45 | 2014-09-18 09:50:21 -0700 | [diff] [blame] | 14 | import net.onrc.onos.core.packet.ICMP; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 15 | import net.onrc.onos.core.packet.IPv4; |
| 16 | import net.onrc.onos.core.topology.ITopologyService; |
| 17 | import net.onrc.onos.core.topology.MutableTopology; |
| 18 | import net.onrc.onos.core.topology.Port; |
| 19 | import net.onrc.onos.core.topology.Switch; |
Sangho Shin | 79c8d45 | 2014-09-18 09:50:21 -0700 | [diff] [blame] | 20 | import net.onrc.onos.core.util.SwitchPort; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 21 | |
| 22 | import org.projectfloodlight.openflow.protocol.OFFactory; |
| 23 | import org.projectfloodlight.openflow.protocol.OFMatchV3; |
| 24 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 25 | import org.projectfloodlight.openflow.protocol.OFOxmList; |
Sangho Shin | f41ac0a | 2014-09-19 09:50:30 -0700 | [diff] [blame^] | 26 | import org.projectfloodlight.openflow.protocol.OFPacketOut; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 27 | import org.projectfloodlight.openflow.protocol.action.OFAction; |
| 28 | import org.projectfloodlight.openflow.protocol.instruction.OFInstruction; |
| 29 | import org.projectfloodlight.openflow.protocol.oxm.OFOxmEthDst; |
| 30 | import org.projectfloodlight.openflow.protocol.oxm.OFOxmEthSrc; |
| 31 | import org.projectfloodlight.openflow.protocol.oxm.OFOxmEthType; |
| 32 | import org.projectfloodlight.openflow.protocol.oxm.OFOxmIpv4DstMasked; |
Sangho Shin | f41ac0a | 2014-09-19 09:50:30 -0700 | [diff] [blame^] | 33 | import org.projectfloodlight.openflow.protocol.oxm.OFOxmMplsLabel; |
| 34 | import org.projectfloodlight.openflow.protocol.oxm.OFOxmVlanVid; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 35 | import org.projectfloodlight.openflow.types.EthType; |
| 36 | import org.projectfloodlight.openflow.types.IPv4Address; |
| 37 | import org.projectfloodlight.openflow.types.MacAddress; |
| 38 | import org.projectfloodlight.openflow.types.OFBufferId; |
| 39 | import org.projectfloodlight.openflow.types.OFPort; |
Sangho Shin | f41ac0a | 2014-09-19 09:50:30 -0700 | [diff] [blame^] | 40 | import org.projectfloodlight.openflow.types.OFVlanVidMatch; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 41 | import org.projectfloodlight.openflow.types.TableId; |
Sangho Shin | f41ac0a | 2014-09-19 09:50:30 -0700 | [diff] [blame^] | 42 | import org.projectfloodlight.openflow.types.U32; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 43 | import org.slf4j.Logger; |
| 44 | import org.slf4j.LoggerFactory; |
| 45 | |
| 46 | public class IcmpHandler implements IPacketListener { |
| 47 | |
| 48 | private SegmentRoutingManager srManager; |
| 49 | private IFloodlightProviderService floodlightProvider; |
| 50 | private MutableTopology mutableTopology; |
| 51 | private IPacketService packetService; |
| 52 | private ITopologyService topologyService; |
| 53 | private static final Logger log = LoggerFactory |
Sangho Shin | f41ac0a | 2014-09-19 09:50:30 -0700 | [diff] [blame^] | 54 | .getLogger(IcmpHandler.class); |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 55 | |
| 56 | private IFlowPusherService flowPusher; |
Sangho Shin | f41ac0a | 2014-09-19 09:50:30 -0700 | [diff] [blame^] | 57 | private boolean added; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 58 | |
| 59 | private static final int TABLE_VLAN = 0; |
| 60 | private static final int TABLE_TMAC = 1; |
| 61 | private static final int TABLE_IPv4_UNICAST = 2; |
| 62 | private static final int TABLE_MPLS = 3; |
| 63 | private static final int TABLE_META = 4; |
| 64 | private static final int TABLE_ACL = 5; |
| 65 | |
| 66 | private static final short MAX_PRIORITY = (short) 0xffff; |
| 67 | private static final short SLASH_24_PRIORITY = (short) 0xfff0; |
| 68 | private static final short SLASH_16_PRIORITY = (short) 0xff00; |
| 69 | private static final short SLASH_8_PRIORITY = (short) 0xf000; |
| 70 | private static final short MIN_PRIORITY = 0x0; |
| 71 | |
| 72 | |
| 73 | public IcmpHandler(FloodlightModuleContext context, SegmentRoutingManager manager) { |
| 74 | |
| 75 | this.floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
| 76 | this.flowPusher = context.getServiceImpl(IFlowPusherService.class); |
| 77 | this.packetService = context.getServiceImpl(IPacketService.class); |
| 78 | this.topologyService = context.getServiceImpl(ITopologyService.class); |
| 79 | this.mutableTopology = topologyService.getTopology(); |
| 80 | |
| 81 | this.srManager = manager; |
Sangho Shin | f41ac0a | 2014-09-19 09:50:30 -0700 | [diff] [blame^] | 82 | this.added = false; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 83 | |
| 84 | packetService.registerPacketListener(this); |
| 85 | |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public void receive(Switch sw, Port inPort, Ethernet payload) { |
| 90 | |
| 91 | if (payload.getEtherType() == Ethernet.TYPE_IPV4) { |
| 92 | |
| 93 | IPv4 ipv4 = (IPv4)payload.getPayload(); |
Sangho Shin | 79c8d45 | 2014-09-18 09:50:21 -0700 | [diff] [blame] | 94 | |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 95 | if (ipv4.getProtocol() == IPv4.PROTOCOL_ICMP) { |
Sangho Shin | 79c8d45 | 2014-09-18 09:50:21 -0700 | [diff] [blame] | 96 | int destinationAddress = ipv4.getDestinationAddress(); |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 97 | |
Sangho Shin | 79c8d45 | 2014-09-18 09:50:21 -0700 | [diff] [blame] | 98 | // Check if it is ICMP request to the switch |
| 99 | String switchIpAddressSlash = sw.getStringAttribute("routerIp"); |
| 100 | if (switchIpAddressSlash != null) { |
| 101 | String switchIpAddressStr = switchIpAddressSlash.substring(0, switchIpAddressSlash.indexOf('/')); |
| 102 | IPv4Address switchIpAddress = IPv4Address.of(switchIpAddressStr); |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 103 | |
Sangho Shin | 79c8d45 | 2014-09-18 09:50:21 -0700 | [diff] [blame] | 104 | if (((ICMP)ipv4.getPayload()).getIcmpType() == 0x08 && |
| 105 | destinationAddress == switchIpAddress.getInt()) { |
Sangho Shin | f41ac0a | 2014-09-19 09:50:30 -0700 | [diff] [blame^] | 106 | //addControlPortInVlanTable(sw); |
Sangho Shin | 79c8d45 | 2014-09-18 09:50:21 -0700 | [diff] [blame] | 107 | sendICMPResponse(sw, inPort, payload); |
| 108 | return; |
| 109 | } |
| 110 | } |
| 111 | |
Sangho Shin | f41ac0a | 2014-09-19 09:50:30 -0700 | [diff] [blame^] | 112 | |
Sangho Shin | 79c8d45 | 2014-09-18 09:50:21 -0700 | [diff] [blame] | 113 | // Check if the destination is any host known to TopologyService |
| 114 | for (net.onrc.onos.core.topology.Host host: mutableTopology.getHosts()) { |
| 115 | IPv4Address hostIpAddress = IPv4Address.of(host.getIpAddress()); |
| 116 | if (hostIpAddress != null && hostIpAddress.getInt() == destinationAddress) { |
| 117 | byte[] destinationMacAddress = host.getMacAddress().toBytes(); |
| 118 | addRouteToHost(sw, destinationAddress, destinationMacAddress); |
| 119 | return; |
| 120 | } |
| 121 | } |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | } |
| 125 | |
| 126 | } |
| 127 | |
Sangho Shin | 79c8d45 | 2014-09-18 09:50:21 -0700 | [diff] [blame] | 128 | |
Sangho Shin | 79c8d45 | 2014-09-18 09:50:21 -0700 | [diff] [blame] | 129 | |
| 130 | /** |
| 131 | * Send ICMP reply back |
| 132 | * |
| 133 | * @param sw Switch |
| 134 | * @param inPort Port the ICMP packet is forwarded from |
| 135 | * @param icmpRequest the ICMP request to handle |
| 136 | * @param destinationAddress destination address to send ICMP response to |
| 137 | */ |
| 138 | private void sendICMPResponse(Switch sw, Port inPort, Ethernet icmpRequest) { |
| 139 | |
| 140 | Ethernet icmpReplyEth = new Ethernet(); |
| 141 | |
| 142 | IPv4 icmpRequestIpv4 = (IPv4) icmpRequest.getPayload(); |
| 143 | IPv4 icmpReplyIpv4 = new IPv4(); |
| 144 | int destAddress = icmpRequestIpv4.getDestinationAddress(); |
| 145 | icmpReplyIpv4.setDestinationAddress(icmpRequestIpv4.getSourceAddress()); |
| 146 | icmpReplyIpv4.setSourceAddress(destAddress); |
| 147 | icmpReplyIpv4.setTtl((byte)64); |
| 148 | icmpReplyIpv4.setChecksum((short)0); |
| 149 | |
| 150 | |
| 151 | ICMP icmpReply = (ICMP)icmpRequestIpv4.getPayload().clone(); |
| 152 | icmpReply.setIcmpCode((byte)0x00); |
| 153 | icmpReply.setIcmpType((byte) 0x00); |
| 154 | icmpReply.setChecksum((short)0); |
| 155 | |
| 156 | icmpReplyIpv4.setPayload(icmpReply); |
| 157 | |
| 158 | icmpReplyEth.setPayload(icmpReplyIpv4); |
| 159 | icmpReplyEth.setEtherType(Ethernet.TYPE_IPV4); |
| 160 | icmpReplyEth.setDestinationMACAddress(icmpRequest.getSourceMACAddress()); |
| 161 | icmpReplyEth.setSourceMACAddress(icmpRequest.getDestinationMACAddress()); |
| 162 | |
Sangho Shin | f41ac0a | 2014-09-19 09:50:30 -0700 | [diff] [blame^] | 163 | sendPacketOut(sw, icmpReplyEth, new SwitchPort(sw.getDpid(), inPort.getPortNumber())); |
Sangho Shin | 79c8d45 | 2014-09-18 09:50:21 -0700 | [diff] [blame] | 164 | |
| 165 | log.debug("Send an ICMP response {}", icmpReplyIpv4.toString()); |
| 166 | |
| 167 | } |
| 168 | |
| 169 | |
Sangho Shin | f41ac0a | 2014-09-19 09:50:30 -0700 | [diff] [blame^] | 170 | |
| 171 | /** |
| 172 | * Send PACKET_OUT message with some actions |
| 173 | * |
| 174 | * @param sw Switch the packet came from |
| 175 | * @param packet Ethernet packet to send |
| 176 | * @param switchPort port to send the packet |
| 177 | */ |
| 178 | private void sendPacketOut(Switch sw, Ethernet packet, SwitchPort switchPort) { |
| 179 | |
| 180 | boolean sameSubnet = false; |
| 181 | IOFSwitch ofSwitch = floodlightProvider.getMasterSwitch(sw.getDpid().value()); |
| 182 | OFFactory factory = ofSwitch.getFactory(); |
| 183 | |
| 184 | List<OFAction> actions = new ArrayList<>(); |
| 185 | |
| 186 | // Check if the destination is the host attached to the switch |
| 187 | int destinationAddress = ((IPv4)packet.getPayload()).getDestinationAddress(); |
| 188 | for (net.onrc.onos.core.topology.Host host: mutableTopology.getHosts(switchPort)) { |
| 189 | IPv4Address hostIpAddress = IPv4Address.of(host.getIpAddress()); |
| 190 | if (hostIpAddress != null && hostIpAddress.getInt() == destinationAddress) { |
| 191 | sameSubnet = true; |
| 192 | break; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | // If the destination host is not attached in the switch, add MPLS label |
| 197 | if (!sameSubnet) { |
| 198 | OFAction pushlabel = factory.actions().pushMpls(EthType.MPLS_UNICAST); |
| 199 | OFOxmMplsLabel l = factory.oxms() |
| 200 | .mplsLabel(U32.of(103)); |
| 201 | OFAction setlabelid = factory.actions().buildSetField() |
| 202 | .setField(l).build(); |
| 203 | OFAction copyTtlOut = factory.actions().copyTtlOut(); |
| 204 | actions.add(pushlabel); |
| 205 | actions.add(setlabelid); |
| 206 | actions.add(copyTtlOut); |
| 207 | } |
| 208 | |
| 209 | OFAction outport = factory.actions().output(OFPort.of((int) switchPort.getPortNumber().value()), Short.MAX_VALUE); |
| 210 | actions.add(outport); |
| 211 | |
| 212 | OFPacketOut po = factory.buildPacketOut() |
| 213 | .setData(packet.serialize()) |
| 214 | .setActions(actions) |
| 215 | .build(); |
| 216 | |
| 217 | flowPusher.add(sw.getDpid(), po); |
| 218 | } |
| 219 | |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 220 | /** |
| 221 | * Add routing rules to forward packets to known hosts |
| 222 | * |
| 223 | * @param sw Switch |
| 224 | * @param hostIp Host IP address to forwards packets to |
| 225 | */ |
Sangho Shin | 79c8d45 | 2014-09-18 09:50:21 -0700 | [diff] [blame] | 226 | private void addRouteToHost(Switch sw, int destinationAddress, byte[] destinationMacAddress) { |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 227 | |
| 228 | IOFSwitch ofSwitch = floodlightProvider.getMasterSwitch(sw.getDpid().value()); |
| 229 | OFFactory factory = ofSwitch.getFactory(); |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 230 | |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 231 | |
| 232 | OFOxmEthType ethTypeIp = factory.oxms() |
| 233 | .ethType(EthType.IPv4); |
| 234 | OFOxmIpv4DstMasked ipPrefix = factory.oxms() |
| 235 | .ipv4DstMasked( |
| 236 | IPv4Address.of(destinationAddress), |
| 237 | IPv4Address.NO_MASK); // host addr should be /32 |
| 238 | OFOxmList oxmListSlash32 = OFOxmList.of(ethTypeIp, ipPrefix); |
| 239 | OFMatchV3 match = factory.buildMatchV3() |
| 240 | .setOxmList(oxmListSlash32).build(); |
| 241 | OFAction setDmac = null; |
| 242 | OFOxmEthDst dmac = factory.oxms() |
| 243 | .ethDst(MacAddress.of(destinationMacAddress)); |
| 244 | setDmac = factory.actions().buildSetField() |
| 245 | .setField(dmac).build(); |
| 246 | |
| 247 | OFAction decTtl = factory.actions().decNwTtl(); |
| 248 | |
| 249 | // Set the source MAC address with the switch MAC address |
| 250 | String switchMacAddress = sw.getStringAttribute("routerMac"); |
| 251 | OFOxmEthSrc srcAddr = factory.oxms().ethSrc(MacAddress.of(switchMacAddress)); |
| 252 | OFAction setSA = factory.actions().buildSetField() |
| 253 | .setField(srcAddr).build(); |
| 254 | |
| 255 | List<OFAction> actionList = new ArrayList<OFAction>(); |
| 256 | actionList.add(setDmac); |
| 257 | actionList.add(decTtl); |
| 258 | actionList.add(setSA); |
| 259 | |
| 260 | |
| 261 | /* TODO : need to check the config file for all packets |
| 262 | String subnets = sw.getStringAttribute("subnets"); |
| 263 | try { |
| 264 | JSONArray arry = new JSONArray(subnets); |
| 265 | for (int i = 0; i < arry.length(); i++) { |
| 266 | String subnetIp = (String) arry.getJSONObject(i).get("subnetIp"); |
| 267 | int portNo = (int) arry.getJSONObject(i).get("portNo"); |
| 268 | |
| 269 | if (netMatch(subnetIp, IPv4Address.of(hostIp.getDestinationAddress()).toString())) { |
| 270 | OFAction out = factory.actions().buildOutput() |
| 271 | .setPort(OFPort.of(portNo)).build(); |
| 272 | actionList.add(out); |
| 273 | } |
| 274 | } |
| 275 | } catch (JSONException e) { |
| 276 | // TODO Auto-generated catch block |
| 277 | e.printStackTrace(); |
| 278 | } |
| 279 | */ |
| 280 | |
| 281 | // Set output port |
| 282 | net.onrc.onos.core.topology.Host host = mutableTopology.getHostByMac(MACAddress.valueOf(destinationMacAddress)); |
| 283 | if (host != null) { |
| 284 | for (Port port: host.getAttachmentPoints()) { |
| 285 | OFAction out = factory.actions().buildOutput() |
| 286 | .setPort(OFPort.of(port.getPortNumber().shortValue())).build(); |
| 287 | actionList.add(out); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | OFInstruction writeInstr = factory.instructions().buildWriteActions() |
| 292 | .setActions(actionList).build(); |
| 293 | |
| 294 | List<OFInstruction> instructions = new ArrayList<OFInstruction>(); |
| 295 | instructions.add(writeInstr); |
| 296 | |
| 297 | OFMessage myIpEntry = factory.buildFlowAdd() |
| 298 | .setTableId(TableId.of(TABLE_IPv4_UNICAST)) |
| 299 | .setMatch(match) |
| 300 | .setInstructions(instructions) |
| 301 | .setPriority(MAX_PRIORITY) |
| 302 | .setBufferId(OFBufferId.NO_BUFFER) |
| 303 | .setIdleTimeout(0) |
| 304 | .setHardTimeout(0) |
| 305 | //.setXid(getNextTransactionId()) |
| 306 | .build(); |
| 307 | |
| 308 | log.debug("Sending 'Routing information' OF message to the switch {}.", sw.getDpid().toString()); |
| 309 | |
| 310 | flowPusher.add(sw.getDpid(), myIpEntry); |
| 311 | |
Sangho Shin | f41ac0a | 2014-09-19 09:50:30 -0700 | [diff] [blame^] | 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Add a new rule to VLAN table to forward packets from any port to the next table |
| 316 | * It is required to forward packets from controller to pipeline |
| 317 | * |
| 318 | * @param sw Switch the packet came from |
| 319 | */ |
| 320 | private void addControlPortInVlanTable(Switch sw) { |
| 321 | |
| 322 | if (added) |
| 323 | return; |
| 324 | else |
| 325 | added = true; |
| 326 | |
| 327 | IOFSwitch ofSwitch = floodlightProvider.getMasterSwitch(sw.getDpid().value()); |
| 328 | OFFactory factory = ofSwitch.getFactory(); |
| 329 | |
| 330 | //OFOxmInPort oxp = factory.oxms().inPort(p.getPortNo()); |
| 331 | OFOxmVlanVid oxv = factory.oxms() |
| 332 | .vlanVid(OFVlanVidMatch.UNTAGGED); |
| 333 | OFOxmList oxmList = OFOxmList.of(oxv); |
| 334 | |
| 335 | |
| 336 | OFMatchV3 match = factory.buildMatchV3() |
| 337 | .setOxmList(oxmList) |
| 338 | .build(); |
| 339 | |
| 340 | OFInstruction gotoTbl = factory.instructions().buildGotoTable() |
| 341 | .setTableId(TableId.of(TABLE_TMAC)).build(); |
| 342 | List<OFInstruction> instructions = new ArrayList<OFInstruction>(); |
| 343 | // instructions.add(appAction); |
| 344 | instructions.add(gotoTbl); |
| 345 | OFMessage flowEntry = factory.buildFlowAdd() |
| 346 | .setTableId(TableId.of(TABLE_VLAN)) |
| 347 | .setMatch(match) |
| 348 | .setInstructions(instructions) |
| 349 | .setPriority(1000) // does not matter - all rules |
| 350 | // exclusive |
| 351 | .setBufferId(OFBufferId.NO_BUFFER) |
| 352 | .setIdleTimeout(0) |
| 353 | .setHardTimeout(0) |
| 354 | //.setXid(getNextTransactionId()) |
| 355 | .build(); |
| 356 | |
| 357 | flowPusher.add(sw.getDpid(), flowEntry);; |
| 358 | //log.debug("Adding {} vlan-rules in sw {}", msglist.size(), getStringId()); |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 359 | |
| 360 | } |
| 361 | |
| 362 | } |