blob: 8e58eac5995eb19375bf5722b6f5d8237861d8e3 [file] [log] [blame]
Sangho Shin2f263692014-09-15 14:09:41 -07001package net.onrc.onos.apps.segmentrouting;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import net.floodlightcontroller.core.IFloodlightProviderService;
7import net.floodlightcontroller.core.IOFSwitch;
8import net.floodlightcontroller.core.module.FloodlightModuleContext;
9import net.floodlightcontroller.util.MACAddress;
10import net.onrc.onos.api.packet.IPacketListener;
11import net.onrc.onos.api.packet.IPacketService;
12import net.onrc.onos.core.flowprogrammer.IFlowPusherService;
13import net.onrc.onos.core.packet.Ethernet;
Sangho Shin79c8d452014-09-18 09:50:21 -070014import net.onrc.onos.core.packet.ICMP;
Sangho Shin2f263692014-09-15 14:09:41 -070015import net.onrc.onos.core.packet.IPv4;
16import net.onrc.onos.core.topology.ITopologyService;
17import net.onrc.onos.core.topology.MutableTopology;
18import net.onrc.onos.core.topology.Port;
19import net.onrc.onos.core.topology.Switch;
Sangho Shin79c8d452014-09-18 09:50:21 -070020import net.onrc.onos.core.util.SwitchPort;
Sangho Shin2f263692014-09-15 14:09:41 -070021
Sangho Shin1aa93542014-09-22 09:49:44 -070022import org.json.JSONArray;
23import org.json.JSONException;
Sangho Shin2f263692014-09-15 14:09:41 -070024import org.projectfloodlight.openflow.protocol.OFFactory;
25import org.projectfloodlight.openflow.protocol.OFMatchV3;
26import org.projectfloodlight.openflow.protocol.OFMessage;
27import org.projectfloodlight.openflow.protocol.OFOxmList;
Sangho Shinf41ac0a2014-09-19 09:50:30 -070028import org.projectfloodlight.openflow.protocol.OFPacketOut;
Sangho Shin2f263692014-09-15 14:09:41 -070029import org.projectfloodlight.openflow.protocol.action.OFAction;
30import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
31import org.projectfloodlight.openflow.protocol.oxm.OFOxmEthDst;
32import org.projectfloodlight.openflow.protocol.oxm.OFOxmEthSrc;
33import org.projectfloodlight.openflow.protocol.oxm.OFOxmEthType;
Sangho Shin1aa93542014-09-22 09:49:44 -070034import org.projectfloodlight.openflow.protocol.oxm.OFOxmInPort;
Sangho Shin2f263692014-09-15 14:09:41 -070035import org.projectfloodlight.openflow.protocol.oxm.OFOxmIpv4DstMasked;
Sangho Shinf41ac0a2014-09-19 09:50:30 -070036import org.projectfloodlight.openflow.protocol.oxm.OFOxmMplsLabel;
37import org.projectfloodlight.openflow.protocol.oxm.OFOxmVlanVid;
Sangho Shin2f263692014-09-15 14:09:41 -070038import org.projectfloodlight.openflow.types.EthType;
39import org.projectfloodlight.openflow.types.IPv4Address;
40import org.projectfloodlight.openflow.types.MacAddress;
41import org.projectfloodlight.openflow.types.OFBufferId;
42import org.projectfloodlight.openflow.types.OFPort;
Sangho Shinf41ac0a2014-09-19 09:50:30 -070043import org.projectfloodlight.openflow.types.OFVlanVidMatch;
Sangho Shin2f263692014-09-15 14:09:41 -070044import org.projectfloodlight.openflow.types.TableId;
Sangho Shinf41ac0a2014-09-19 09:50:30 -070045import org.projectfloodlight.openflow.types.U32;
Sangho Shin2f263692014-09-15 14:09:41 -070046import org.slf4j.Logger;
47import org.slf4j.LoggerFactory;
48
49public class IcmpHandler implements IPacketListener {
50
51 private SegmentRoutingManager srManager;
52 private IFloodlightProviderService floodlightProvider;
53 private MutableTopology mutableTopology;
54 private IPacketService packetService;
55 private ITopologyService topologyService;
56 private static final Logger log = LoggerFactory
Sangho Shinf41ac0a2014-09-19 09:50:30 -070057 .getLogger(IcmpHandler.class);
Sangho Shin2f263692014-09-15 14:09:41 -070058
59 private IFlowPusherService flowPusher;
Sangho Shin1aa93542014-09-22 09:49:44 -070060 private boolean controllerPortAllowed = false;
Sangho Shin2f263692014-09-15 14:09:41 -070061
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 Shin1aa93542014-09-22 09:49:44 -070075 private static final int ICMP_TYPE_ECHO = 0x08;
76 private static final int ICMP_TYPE_REPLY = 0x00;
77
Sangho Shin2f263692014-09-15 14:09:41 -070078
79 public IcmpHandler(FloodlightModuleContext context, SegmentRoutingManager manager) {
80
81 this.floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
82 this.flowPusher = context.getServiceImpl(IFlowPusherService.class);
83 this.packetService = context.getServiceImpl(IPacketService.class);
84 this.topologyService = context.getServiceImpl(ITopologyService.class);
85 this.mutableTopology = topologyService.getTopology();
86
87 this.srManager = manager;
88
89 packetService.registerPacketListener(this);
90
91 }
92
93 @Override
94 public void receive(Switch sw, Port inPort, Ethernet payload) {
95
96 if (payload.getEtherType() == Ethernet.TYPE_IPV4) {
97
98 IPv4 ipv4 = (IPv4)payload.getPayload();
Sangho Shin79c8d452014-09-18 09:50:21 -070099
Sangho Shin2f263692014-09-15 14:09:41 -0700100 if (ipv4.getProtocol() == IPv4.PROTOCOL_ICMP) {
Sangho Shin79c8d452014-09-18 09:50:21 -0700101 int destinationAddress = ipv4.getDestinationAddress();
Sangho Shin2f263692014-09-15 14:09:41 -0700102
Sangho Shin79c8d452014-09-18 09:50:21 -0700103 // Check if it is ICMP request to the switch
104 String switchIpAddressSlash = sw.getStringAttribute("routerIp");
105 if (switchIpAddressSlash != null) {
106 String switchIpAddressStr = switchIpAddressSlash.substring(0, switchIpAddressSlash.indexOf('/'));
107 IPv4Address switchIpAddress = IPv4Address.of(switchIpAddressStr);
Sangho Shin2f263692014-09-15 14:09:41 -0700108
Sangho Shin1aa93542014-09-22 09:49:44 -0700109 if (((ICMP)ipv4.getPayload()).getIcmpType() == ICMP_TYPE_ECHO &&
Sangho Shin79c8d452014-09-18 09:50:21 -0700110 destinationAddress == switchIpAddress.getInt()) {
111 sendICMPResponse(sw, inPort, payload);
112 return;
113 }
114 }
115
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700116
Sangho Shin79c8d452014-09-18 09:50:21 -0700117 // Check if the destination is any host known to TopologyService
118 for (net.onrc.onos.core.topology.Host host: mutableTopology.getHosts()) {
119 IPv4Address hostIpAddress = IPv4Address.of(host.getIpAddress());
120 if (hostIpAddress != null && hostIpAddress.getInt() == destinationAddress) {
121 byte[] destinationMacAddress = host.getMacAddress().toBytes();
122 addRouteToHost(sw, destinationAddress, destinationMacAddress);
123 return;
124 }
125 }
Sangho Shin2f263692014-09-15 14:09:41 -0700126 }
127
128 }
129
130 }
131
Sangho Shin79c8d452014-09-18 09:50:21 -0700132
Sangho Shin79c8d452014-09-18 09:50:21 -0700133
134 /**
135 * Send ICMP reply back
136 *
137 * @param sw Switch
138 * @param inPort Port the ICMP packet is forwarded from
139 * @param icmpRequest the ICMP request to handle
140 * @param destinationAddress destination address to send ICMP response to
141 */
142 private void sendICMPResponse(Switch sw, Port inPort, Ethernet icmpRequest) {
143
144 Ethernet icmpReplyEth = new Ethernet();
145
146 IPv4 icmpRequestIpv4 = (IPv4) icmpRequest.getPayload();
147 IPv4 icmpReplyIpv4 = new IPv4();
148 int destAddress = icmpRequestIpv4.getDestinationAddress();
149 icmpReplyIpv4.setDestinationAddress(icmpRequestIpv4.getSourceAddress());
150 icmpReplyIpv4.setSourceAddress(destAddress);
151 icmpReplyIpv4.setTtl((byte)64);
152 icmpReplyIpv4.setChecksum((short)0);
153
154
155 ICMP icmpReply = (ICMP)icmpRequestIpv4.getPayload().clone();
156 icmpReply.setIcmpCode((byte)0x00);
Sangho Shin1aa93542014-09-22 09:49:44 -0700157 icmpReply.setIcmpType((byte) ICMP_TYPE_REPLY);
Sangho Shin79c8d452014-09-18 09:50:21 -0700158 icmpReply.setChecksum((short)0);
159
160 icmpReplyIpv4.setPayload(icmpReply);
161
162 icmpReplyEth.setPayload(icmpReplyIpv4);
163 icmpReplyEth.setEtherType(Ethernet.TYPE_IPV4);
164 icmpReplyEth.setDestinationMACAddress(icmpRequest.getSourceMACAddress());
165 icmpReplyEth.setSourceMACAddress(icmpRequest.getDestinationMACAddress());
166
Sangho Shin1aa93542014-09-22 09:49:44 -0700167 sendPacketOut(sw, icmpReplyEth, new SwitchPort(sw.getDpid(), inPort.getPortNumber()), false);
Sangho Shin79c8d452014-09-18 09:50:21 -0700168
169 log.debug("Send an ICMP response {}", icmpReplyIpv4.toString());
170
171 }
172
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700173 /**
Sangho Shin1aa93542014-09-22 09:49:44 -0700174 * Send PACKET_OUT message with actions
175 * If switches support OFPP_TABLE action, it sends out packet to TABLE port
176 * Otherwise, it sends the packet to the port the packet came from
177 * (in this case, MPLS label is added if the packet needs go through transit switches)
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700178 *
179 * @param sw Switch the packet came from
180 * @param packet Ethernet packet to send
181 * @param switchPort port to send the packet
182 */
Sangho Shin1aa93542014-09-22 09:49:44 -0700183 private void sendPacketOut(Switch sw, Ethernet packet, SwitchPort switchPort, boolean supportOfppTable) {
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700184
185 boolean sameSubnet = false;
186 IOFSwitch ofSwitch = floodlightProvider.getMasterSwitch(sw.getDpid().value());
187 OFFactory factory = ofSwitch.getFactory();
188
189 List<OFAction> actions = new ArrayList<>();
190
Sangho Shin1aa93542014-09-22 09:49:44 -0700191 // If OFPP_TABLE action is not supported in the switch, MPLS label needs to be set
192 // if the packet needs to be delivered crossing switches
193 if (!supportOfppTable) {
194 // Check if the destination is the host attached to the switch
195 int destinationAddress = ((IPv4)packet.getPayload()).getDestinationAddress();
196 for (net.onrc.onos.core.topology.Host host: mutableTopology.getHosts(switchPort)) {
197 IPv4Address hostIpAddress = IPv4Address.of(host.getIpAddress());
198 if (hostIpAddress != null && hostIpAddress.getInt() == destinationAddress) {
199 sameSubnet = true;
200 break;
201 }
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700202 }
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700203
Sangho Shin1aa93542014-09-22 09:49:44 -0700204 // If the destination host is not attached in the switch, add MPLS label
205 if (!sameSubnet) {
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700206
Sangho Shin1aa93542014-09-22 09:49:44 -0700207 IPv4Address targetAddress = IPv4Address.of(((IPv4)packet.getPayload()).getDestinationAddress());
208 int mplsLabel = getMplsLabelFromConfig(targetAddress);
209 if (mplsLabel > 0) {
210 OFAction pushlabel = factory.actions().pushMpls(EthType.MPLS_UNICAST);
211 OFOxmMplsLabel l = factory.oxms()
212 .mplsLabel(U32.of(mplsLabel));
213 OFAction setlabelid = factory.actions().buildSetField()
214 .setField(l).build();
215 OFAction copyTtlOut = factory.actions().copyTtlOut();
216 actions.add(pushlabel);
217 actions.add(setlabelid);
218 actions.add(copyTtlOut);
219 }
220 }
221
222 OFAction outport = factory.actions().output(OFPort.of(switchPort.getPortNumber().shortValue()), Short.MAX_VALUE);
223 actions.add(outport);
224 }
225 // If OFPP_TABLE action is supported, first set a rule to allow packet from CONTROLLER port.
226 // Then, send the packet to the table port
227 else {
228 if (!controllerPortAllowed) {
229 addControlPortInVlanTable(sw);
230 controllerPortAllowed = true;
231 }
232 OFAction outport = factory.actions().output(OFPort.TABLE, Short.MAX_VALUE);
233 actions.add(outport);
234 }
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700235
236 OFPacketOut po = factory.buildPacketOut()
237 .setData(packet.serialize())
238 .setActions(actions)
239 .build();
240
241 flowPusher.add(sw.getDpid(), po);
242 }
243
Sangho Shin2f263692014-09-15 14:09:41 -0700244 /**
Sangho Shin1aa93542014-09-22 09:49:44 -0700245 * Get MPLS label for the target address from the network config file
246 *
247 * @param targetAddress - IP address of the target host
248 * @return MPLS label of the switch to send packets to the target address
249 */
250 private int getMplsLabelFromConfig(IPv4Address targetAddress) {
251
252 int mplsLabel = -1;
253
254 for (Switch sw: mutableTopology.getSwitches()) {
255
256 String subnets = sw.getStringAttribute("subnets");
257 try {
258 JSONArray arry = new JSONArray(subnets);
259 for (int i = 0; i < arry.length(); i++) {
260 String subnetIp = (String) arry.getJSONObject(i).get("subnetIp");
261 if (srManager.netMatch(subnetIp, targetAddress.toString())) {
262 String mplsLabelStr = sw.getStringAttribute("nodeSid");
263 if (mplsLabelStr != null)
264 mplsLabel = Integer.parseInt(mplsLabelStr);
265 }
266 }
267 } catch (JSONException e) {
268 // TODO Auto-generated catch block
269 e.printStackTrace();
270 }
271 }
272
273 return mplsLabel;
274 }
275
276 /**
Sangho Shin2f263692014-09-15 14:09:41 -0700277 * Add routing rules to forward packets to known hosts
278 *
279 * @param sw Switch
280 * @param hostIp Host IP address to forwards packets to
281 */
Sangho Shin79c8d452014-09-18 09:50:21 -0700282 private void addRouteToHost(Switch sw, int destinationAddress, byte[] destinationMacAddress) {
Sangho Shin2f263692014-09-15 14:09:41 -0700283
284 IOFSwitch ofSwitch = floodlightProvider.getMasterSwitch(sw.getDpid().value());
285 OFFactory factory = ofSwitch.getFactory();
Sangho Shin2f263692014-09-15 14:09:41 -0700286
Sangho Shin2f263692014-09-15 14:09:41 -0700287
288 OFOxmEthType ethTypeIp = factory.oxms()
289 .ethType(EthType.IPv4);
290 OFOxmIpv4DstMasked ipPrefix = factory.oxms()
291 .ipv4DstMasked(
292 IPv4Address.of(destinationAddress),
293 IPv4Address.NO_MASK); // host addr should be /32
294 OFOxmList oxmListSlash32 = OFOxmList.of(ethTypeIp, ipPrefix);
295 OFMatchV3 match = factory.buildMatchV3()
296 .setOxmList(oxmListSlash32).build();
297 OFAction setDmac = null;
298 OFOxmEthDst dmac = factory.oxms()
299 .ethDst(MacAddress.of(destinationMacAddress));
300 setDmac = factory.actions().buildSetField()
301 .setField(dmac).build();
302
303 OFAction decTtl = factory.actions().decNwTtl();
304
305 // Set the source MAC address with the switch MAC address
306 String switchMacAddress = sw.getStringAttribute("routerMac");
307 OFOxmEthSrc srcAddr = factory.oxms().ethSrc(MacAddress.of(switchMacAddress));
308 OFAction setSA = factory.actions().buildSetField()
309 .setField(srcAddr).build();
310
311 List<OFAction> actionList = new ArrayList<OFAction>();
312 actionList.add(setDmac);
313 actionList.add(decTtl);
314 actionList.add(setSA);
315
316
317 /* TODO : need to check the config file for all packets
318 String subnets = sw.getStringAttribute("subnets");
319 try {
320 JSONArray arry = new JSONArray(subnets);
321 for (int i = 0; i < arry.length(); i++) {
322 String subnetIp = (String) arry.getJSONObject(i).get("subnetIp");
323 int portNo = (int) arry.getJSONObject(i).get("portNo");
324
325 if (netMatch(subnetIp, IPv4Address.of(hostIp.getDestinationAddress()).toString())) {
326 OFAction out = factory.actions().buildOutput()
327 .setPort(OFPort.of(portNo)).build();
328 actionList.add(out);
329 }
330 }
331 } catch (JSONException e) {
332 // TODO Auto-generated catch block
333 e.printStackTrace();
334 }
335 */
336
337 // Set output port
338 net.onrc.onos.core.topology.Host host = mutableTopology.getHostByMac(MACAddress.valueOf(destinationMacAddress));
339 if (host != null) {
340 for (Port port: host.getAttachmentPoints()) {
341 OFAction out = factory.actions().buildOutput()
342 .setPort(OFPort.of(port.getPortNumber().shortValue())).build();
343 actionList.add(out);
344 }
345 }
346
347 OFInstruction writeInstr = factory.instructions().buildWriteActions()
348 .setActions(actionList).build();
349
350 List<OFInstruction> instructions = new ArrayList<OFInstruction>();
351 instructions.add(writeInstr);
352
353 OFMessage myIpEntry = factory.buildFlowAdd()
354 .setTableId(TableId.of(TABLE_IPv4_UNICAST))
355 .setMatch(match)
356 .setInstructions(instructions)
357 .setPriority(MAX_PRIORITY)
358 .setBufferId(OFBufferId.NO_BUFFER)
359 .setIdleTimeout(0)
360 .setHardTimeout(0)
361 //.setXid(getNextTransactionId())
362 .build();
363
364 log.debug("Sending 'Routing information' OF message to the switch {}.", sw.getDpid().toString());
365
366 flowPusher.add(sw.getDpid(), myIpEntry);
367
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700368 }
369
370 /**
371 * Add a new rule to VLAN table to forward packets from any port to the next table
372 * It is required to forward packets from controller to pipeline
373 *
374 * @param sw Switch the packet came from
375 */
376 private void addControlPortInVlanTable(Switch sw) {
377
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700378 IOFSwitch ofSwitch = floodlightProvider.getMasterSwitch(sw.getDpid().value());
379 OFFactory factory = ofSwitch.getFactory();
380
Sangho Shin1aa93542014-09-22 09:49:44 -0700381 OFOxmInPort oxp = factory.oxms().inPort(OFPort.CONTROLLER);
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700382 OFOxmVlanVid oxv = factory.oxms()
383 .vlanVid(OFVlanVidMatch.UNTAGGED);
384 OFOxmList oxmList = OFOxmList.of(oxv);
385
Sangho Shin1aa93542014-09-22 09:49:44 -0700386 /* Cqpd switch does not seems to support CONTROLLER port as in_port match rule */
387 //OFOxmList oxmList = OFOxmList.of(oxp, oxv);
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700388
389 OFMatchV3 match = factory.buildMatchV3()
390 .setOxmList(oxmList)
391 .build();
392
393 OFInstruction gotoTbl = factory.instructions().buildGotoTable()
394 .setTableId(TableId.of(TABLE_TMAC)).build();
395 List<OFInstruction> instructions = new ArrayList<OFInstruction>();
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700396 instructions.add(gotoTbl);
397 OFMessage flowEntry = factory.buildFlowAdd()
398 .setTableId(TableId.of(TABLE_VLAN))
399 .setMatch(match)
400 .setInstructions(instructions)
401 .setPriority(1000) // does not matter - all rules
402 // exclusive
403 .setBufferId(OFBufferId.NO_BUFFER)
404 .setIdleTimeout(0)
405 .setHardTimeout(0)
406 //.setXid(getNextTransactionId())
407 .build();
408
409 flowPusher.add(sw.getDpid(), flowEntry);;
Sangho Shin1aa93542014-09-22 09:49:44 -0700410 log.debug("Adding a new vlan-rules in sw {}", sw.getDpid());
Sangho Shin2f263692014-09-15 14:09:41 -0700411
412 }
413
414}