Sangho Shin | eb08303 | 2014-09-22 16:11:34 -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; |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 10 | import net.onrc.onos.core.flowprogrammer.IFlowPusherService; |
| 11 | import net.onrc.onos.core.packet.Ethernet; |
| 12 | import net.onrc.onos.core.packet.IPv4; |
| 13 | import net.onrc.onos.core.topology.ITopologyService; |
| 14 | import net.onrc.onos.core.topology.MutableTopology; |
| 15 | import net.onrc.onos.core.topology.Port; |
| 16 | import net.onrc.onos.core.topology.Switch; |
| 17 | |
| 18 | import org.projectfloodlight.openflow.protocol.OFFactory; |
| 19 | import org.projectfloodlight.openflow.protocol.OFMatchV3; |
| 20 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 21 | import org.projectfloodlight.openflow.protocol.OFOxmList; |
| 22 | import org.projectfloodlight.openflow.protocol.action.OFAction; |
| 23 | import org.projectfloodlight.openflow.protocol.instruction.OFInstruction; |
| 24 | import org.projectfloodlight.openflow.protocol.oxm.OFOxmEthDst; |
| 25 | import org.projectfloodlight.openflow.protocol.oxm.OFOxmEthSrc; |
| 26 | import org.projectfloodlight.openflow.protocol.oxm.OFOxmEthType; |
| 27 | import org.projectfloodlight.openflow.protocol.oxm.OFOxmIpv4DstMasked; |
| 28 | import org.projectfloodlight.openflow.types.EthType; |
| 29 | import org.projectfloodlight.openflow.types.IPv4Address; |
| 30 | import org.projectfloodlight.openflow.types.MacAddress; |
| 31 | import org.projectfloodlight.openflow.types.OFBufferId; |
| 32 | import org.projectfloodlight.openflow.types.OFPort; |
| 33 | import org.projectfloodlight.openflow.types.TableId; |
| 34 | import org.slf4j.Logger; |
| 35 | import org.slf4j.LoggerFactory; |
| 36 | |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 37 | public class GenericIpHandler { |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 38 | |
| 39 | private MutableTopology mutableTopology; |
| 40 | private ITopologyService topologyService; |
| 41 | private IFloodlightProviderService floodlightProvider; |
| 42 | private IFlowPusherService flowPusher; |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 43 | private SegmentRoutingManager srManager; |
| 44 | |
| 45 | private static final Logger log = LoggerFactory |
| 46 | .getLogger(GenericIpHandler.class); |
| 47 | |
| 48 | private static final int TABLE_VLAN = 0; |
| 49 | private static final int TABLE_TMAC = 1; |
| 50 | private static final int TABLE_IPv4_UNICAST = 2; |
| 51 | private static final int TABLE_MPLS = 3; |
| 52 | private static final int TABLE_META = 4; |
| 53 | private static final int TABLE_ACL = 5; |
| 54 | |
| 55 | private static final short MAX_PRIORITY = (short) 0xffff; |
| 56 | private static final short SLASH_24_PRIORITY = (short) 0xfff0; |
| 57 | private static final short SLASH_16_PRIORITY = (short) 0xff00; |
| 58 | private static final short SLASH_8_PRIORITY = (short) 0xf000; |
| 59 | private static final short MIN_PRIORITY = 0x0; |
| 60 | |
| 61 | public GenericIpHandler(FloodlightModuleContext context, SegmentRoutingManager sr) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 62 | this.floodlightProvider = context |
| 63 | .getServiceImpl(IFloodlightProviderService.class); |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 64 | this.topologyService = context.getServiceImpl(ITopologyService.class); |
| 65 | this.mutableTopology = topologyService.getTopology(); |
| 66 | this.flowPusher = context.getServiceImpl(IFlowPusherService.class); |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 67 | this.srManager = sr; |
| 68 | |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 71 | public void processPacketIn(Switch sw, Port inPort, Ethernet payload) { |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 72 | // TODO Auto-generated method stub |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 73 | log.debug("GenericIPHandler: Received a IP packet {} from sw {} ", |
| 74 | payload.toString(), sw.getDpid()); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 75 | IPv4 ipv4 = (IPv4) payload.getPayload(); |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 76 | int destinationAddress = ipv4.getDestinationAddress(); |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 77 | |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 78 | // Check if the destination is any host known to TopologyService |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 79 | for (net.onrc.onos.core.topology.Host host : mutableTopology.getHosts()) { |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 80 | IPv4Address hostIpAddress = IPv4Address.of(host.getIpAddress()); |
| 81 | if (hostIpAddress != null && hostIpAddress.getInt() == destinationAddress) { |
| 82 | byte[] destinationMacAddress = host.getMacAddress().toBytes(); |
| 83 | addRouteToHost(sw, destinationAddress, destinationMacAddress); |
| 84 | return; |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 85 | } |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 86 | } |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 87 | |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 88 | // Check if the destination is within subnets of the swtich |
| 89 | if (isWithinSubnets(sw, IPv4Address.of(destinationAddress).toString())) { |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 90 | srManager.addPacketToPacketBuffer(ipv4); |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 91 | srManager.sendArpRequest(sw, destinationAddress, inPort); |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
| 95 | private boolean isWithinSubnets(Switch sw, String ipAddress) { |
| 96 | |
| 97 | return true; |
| 98 | } |
| 99 | |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 100 | /** |
| 101 | * Add routing rules to forward packets to known hosts |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 102 | * |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 103 | * @param sw Switch |
| 104 | * @param hostIp Host IP address to forwards packets to |
| 105 | */ |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 106 | public void addRouteToHost(Switch sw, int destinationAddress, |
| 107 | byte[] destinationMacAddress) { |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 108 | |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 109 | // If we do not know the host, then we cannot set the forwarding rule |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 110 | net.onrc.onos.core.topology.Host host = mutableTopology.getHostByMac(MACAddress |
| 111 | .valueOf(destinationMacAddress)); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 112 | if (host == null) { |
| 113 | return; |
| 114 | } |
| 115 | |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 116 | IOFSwitch ofSwitch = floodlightProvider.getMasterSwitch(sw.getDpid().value()); |
| 117 | OFFactory factory = ofSwitch.getFactory(); |
| 118 | |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 119 | OFOxmEthType ethTypeIp = factory.oxms() |
| 120 | .ethType(EthType.IPv4); |
| 121 | OFOxmIpv4DstMasked ipPrefix = factory.oxms() |
| 122 | .ipv4DstMasked( |
| 123 | IPv4Address.of(destinationAddress), |
| 124 | IPv4Address.NO_MASK); // host addr should be /32 |
| 125 | OFOxmList oxmListSlash32 = OFOxmList.of(ethTypeIp, ipPrefix); |
| 126 | OFMatchV3 match = factory.buildMatchV3() |
| 127 | .setOxmList(oxmListSlash32).build(); |
| 128 | OFAction setDmac = null; |
| 129 | OFOxmEthDst dmac = factory.oxms() |
| 130 | .ethDst(MacAddress.of(destinationMacAddress)); |
| 131 | setDmac = factory.actions().buildSetField() |
| 132 | .setField(dmac).build(); |
| 133 | |
| 134 | OFAction decTtl = factory.actions().decNwTtl(); |
| 135 | |
| 136 | // Set the source MAC address with the switch MAC address |
| 137 | String switchMacAddress = sw.getStringAttribute("routerMac"); |
| 138 | OFOxmEthSrc srcAddr = factory.oxms().ethSrc(MacAddress.of(switchMacAddress)); |
| 139 | OFAction setSA = factory.actions().buildSetField() |
| 140 | .setField(srcAddr).build(); |
| 141 | |
| 142 | List<OFAction> actionList = new ArrayList<OFAction>(); |
| 143 | actionList.add(setDmac); |
| 144 | actionList.add(decTtl); |
| 145 | actionList.add(setSA); |
| 146 | |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 147 | /* TODO : need to check the config file for all packets |
| 148 | String subnets = sw.getStringAttribute("subnets"); |
| 149 | try { |
| 150 | JSONArray arry = new JSONArray(subnets); |
| 151 | for (int i = 0; i < arry.length(); i++) { |
| 152 | String subnetIp = (String) arry.getJSONObject(i).get("subnetIp"); |
| 153 | int portNo = (int) arry.getJSONObject(i).get("portNo"); |
| 154 | |
| 155 | if (netMatch(subnetIp, IPv4Address.of(hostIp.getDestinationAddress()).toString())) { |
| 156 | OFAction out = factory.actions().buildOutput() |
| 157 | .setPort(OFPort.of(portNo)).build(); |
| 158 | actionList.add(out); |
| 159 | } |
| 160 | } |
| 161 | } catch (JSONException e) { |
| 162 | // TODO Auto-generated catch block |
| 163 | e.printStackTrace(); |
| 164 | } |
| 165 | */ |
| 166 | |
| 167 | // Set output port |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 168 | for (Port port : host.getAttachmentPoints()) { |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 169 | OFAction out = factory.actions().buildOutput() |
| 170 | .setPort(OFPort.of(port.getPortNumber().shortValue())).build(); |
| 171 | actionList.add(out); |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | OFInstruction writeInstr = factory.instructions().buildWriteActions() |
| 175 | .setActions(actionList).build(); |
| 176 | |
| 177 | List<OFInstruction> instructions = new ArrayList<OFInstruction>(); |
| 178 | instructions.add(writeInstr); |
| 179 | |
| 180 | OFMessage myIpEntry = factory.buildFlowAdd() |
| 181 | .setTableId(TableId.of(TABLE_IPv4_UNICAST)) |
| 182 | .setMatch(match) |
| 183 | .setInstructions(instructions) |
| 184 | .setPriority(MAX_PRIORITY) |
| 185 | .setBufferId(OFBufferId.NO_BUFFER) |
| 186 | .setIdleTimeout(0) |
| 187 | .setHardTimeout(0) |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 188 | // .setXid(getNextTransactionId()) |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 189 | .build(); |
| 190 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 191 | log.debug("Sending 'Routing information' OF message to the switch {}.", sw |
| 192 | .getDpid().toString()); |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 193 | |
| 194 | flowPusher.add(sw.getDpid(), myIpEntry); |
| 195 | |
| 196 | } |
| 197 | |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 198 | } |