blob: 3b65d555f2f67ca57eb8fe7b978392785e8c32b1 [file] [log] [blame]
Sangho Shin463bee52014-09-29 15:14:43 -07001
2/*******************************************************************************
3 * Copyright (c) 2014 Open Networking Laboratory.
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Apache License v2.0
6 * which accompanies this distribution, and is available at
7 * http://www.apache.org/licenses/LICENSE-2.0
8 ******************************************************************************/
9
Sangho Shin2f263692014-09-15 14:09:41 -070010package net.onrc.onos.apps.segmentrouting;
11
12import java.util.ArrayList;
Sangho Shin463bee52014-09-29 15:14:43 -070013import java.util.LinkedList;
Sangho Shin2f263692014-09-15 14:09:41 -070014import java.util.List;
Sangho Shin463bee52014-09-29 15:14:43 -070015import java.util.Queue;
Sangho Shin2f263692014-09-15 14:09:41 -070016
17import net.floodlightcontroller.core.IFloodlightProviderService;
18import net.floodlightcontroller.core.IOFSwitch;
19import net.floodlightcontroller.core.module.FloodlightModuleContext;
Sangho Shin2f263692014-09-15 14:09:41 -070020import net.onrc.onos.core.flowprogrammer.IFlowPusherService;
21import net.onrc.onos.core.packet.Ethernet;
Sangho Shin79c8d452014-09-18 09:50:21 -070022import net.onrc.onos.core.packet.ICMP;
Sangho Shin2f263692014-09-15 14:09:41 -070023import net.onrc.onos.core.packet.IPv4;
Srikanth Vavilapalli5ddf8f02014-09-24 11:02:28 -070024import net.onrc.onos.core.topology.Host;
Sangho Shin2f263692014-09-15 14:09:41 -070025import net.onrc.onos.core.topology.ITopologyService;
26import net.onrc.onos.core.topology.MutableTopology;
27import net.onrc.onos.core.topology.Port;
28import net.onrc.onos.core.topology.Switch;
Sangho Shin79c8d452014-09-18 09:50:21 -070029import net.onrc.onos.core.util.SwitchPort;
Sangho Shin2f263692014-09-15 14:09:41 -070030
Sangho Shin1aa93542014-09-22 09:49:44 -070031import org.json.JSONArray;
32import org.json.JSONException;
Sangho Shin2f263692014-09-15 14:09:41 -070033import org.projectfloodlight.openflow.protocol.OFFactory;
34import org.projectfloodlight.openflow.protocol.OFMatchV3;
35import org.projectfloodlight.openflow.protocol.OFMessage;
36import org.projectfloodlight.openflow.protocol.OFOxmList;
Sangho Shinf41ac0a2014-09-19 09:50:30 -070037import org.projectfloodlight.openflow.protocol.OFPacketOut;
Sangho Shin2f263692014-09-15 14:09:41 -070038import org.projectfloodlight.openflow.protocol.action.OFAction;
39import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
Sangho Shin1aa93542014-09-22 09:49:44 -070040import org.projectfloodlight.openflow.protocol.oxm.OFOxmInPort;
Sangho Shinf41ac0a2014-09-19 09:50:30 -070041import org.projectfloodlight.openflow.protocol.oxm.OFOxmMplsLabel;
42import org.projectfloodlight.openflow.protocol.oxm.OFOxmVlanVid;
Sangho Shin2f263692014-09-15 14:09:41 -070043import org.projectfloodlight.openflow.types.EthType;
44import org.projectfloodlight.openflow.types.IPv4Address;
Sangho Shin2f263692014-09-15 14:09:41 -070045import org.projectfloodlight.openflow.types.OFBufferId;
46import org.projectfloodlight.openflow.types.OFPort;
Sangho Shinf41ac0a2014-09-19 09:50:30 -070047import org.projectfloodlight.openflow.types.OFVlanVidMatch;
Sangho Shin2f263692014-09-15 14:09:41 -070048import org.projectfloodlight.openflow.types.TableId;
Sangho Shinf41ac0a2014-09-19 09:50:30 -070049import org.projectfloodlight.openflow.types.U32;
Sangho Shin2f263692014-09-15 14:09:41 -070050import org.slf4j.Logger;
51import org.slf4j.LoggerFactory;
52
Srikanth Vavilapallib1fce732014-09-24 14:09:50 -070053public class IcmpHandler {
Sangho Shin2f263692014-09-15 14:09:41 -070054
55 private SegmentRoutingManager srManager;
56 private IFloodlightProviderService floodlightProvider;
57 private MutableTopology mutableTopology;
Sangho Shin2f263692014-09-15 14:09:41 -070058 private ITopologyService topologyService;
59 private static final Logger log = LoggerFactory
Sangho Shinf41ac0a2014-09-19 09:50:30 -070060 .getLogger(IcmpHandler.class);
Sangho Shin2f263692014-09-15 14:09:41 -070061
62 private IFlowPusherService flowPusher;
Sangho Shin1aa93542014-09-22 09:49:44 -070063 private boolean controllerPortAllowed = false;
Sangho Shin2f263692014-09-15 14:09:41 -070064
Sangho Shin463bee52014-09-29 15:14:43 -070065 private Queue<IPv4> icmpQueue = new LinkedList<IPv4>();
66
Sangho Shin2f263692014-09-15 14:09:41 -070067 private static final int TABLE_VLAN = 0;
68 private static final int TABLE_TMAC = 1;
69 private static final int TABLE_IPv4_UNICAST = 2;
70 private static final int TABLE_MPLS = 3;
71 private static final int TABLE_META = 4;
72 private static final int TABLE_ACL = 5;
73
74 private static final short MAX_PRIORITY = (short) 0xffff;
75 private static final short SLASH_24_PRIORITY = (short) 0xfff0;
76 private static final short SLASH_16_PRIORITY = (short) 0xff00;
77 private static final short SLASH_8_PRIORITY = (short) 0xf000;
78 private static final short MIN_PRIORITY = 0x0;
79
Sangho Shin1aa93542014-09-22 09:49:44 -070080 private static final int ICMP_TYPE_ECHO = 0x08;
81 private static final int ICMP_TYPE_REPLY = 0x00;
82
Sangho Shin2f263692014-09-15 14:09:41 -070083
84 public IcmpHandler(FloodlightModuleContext context, SegmentRoutingManager manager) {
85
86 this.floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
87 this.flowPusher = context.getServiceImpl(IFlowPusherService.class);
Sangho Shin2f263692014-09-15 14:09:41 -070088 this.topologyService = context.getServiceImpl(ITopologyService.class);
89 this.mutableTopology = topologyService.getTopology();
90
91 this.srManager = manager;
Sangho Shin2f263692014-09-15 14:09:41 -070092 }
93
Sangho Shin463bee52014-09-29 15:14:43 -070094 /**
95 * handle ICMP packets
96 * If it is for ICMP echo to router IP or any subnet GW IP,
97 * then send ICMP response on behalf of the switch.
98 * If it is for any hosts in subnets of the switches, but if the MAC
99 * address is not known, then send an ARP request to the subent.
100 * If the MAC address is known, then set the routing rule to the switch
101 *
102 * @param sw
103 * @param inPort
104 * @param payload
105 */
Srikanth Vavilapallib1fce732014-09-24 14:09:50 -0700106 public void processPacketIn(Switch sw, Port inPort, Ethernet payload) {
Sangho Shin2f263692014-09-15 14:09:41 -0700107
108 if (payload.getEtherType() == Ethernet.TYPE_IPV4) {
109
110 IPv4 ipv4 = (IPv4)payload.getPayload();
Sangho Shin79c8d452014-09-18 09:50:21 -0700111
Sangho Shin2f263692014-09-15 14:09:41 -0700112 if (ipv4.getProtocol() == IPv4.PROTOCOL_ICMP) {
Srikanth Vavilapalli5ddf8f02014-09-24 11:02:28 -0700113
114 log.debug("ICMPHandler: Received a ICMP packet {} from sw {} ",
115 payload.toString(), sw.getDpid());
Sangho Shin463bee52014-09-29 15:14:43 -0700116 IPv4Address destinationAddress =
117 IPv4Address.of(ipv4.getDestinationAddress());
Sangho Shin2f263692014-09-15 14:09:41 -0700118
Sangho Shin79c8d452014-09-18 09:50:21 -0700119 // Check if it is ICMP request to the switch
120 String switchIpAddressSlash = sw.getStringAttribute("routerIp");
121 if (switchIpAddressSlash != null) {
Sangho Shineb083032014-09-22 16:11:34 -0700122 String switchIpAddressStr
123 = switchIpAddressSlash.substring(0, switchIpAddressSlash.indexOf('/'));
Sangho Shin79c8d452014-09-18 09:50:21 -0700124 IPv4Address switchIpAddress = IPv4Address.of(switchIpAddressStr);
Sangho Shineb083032014-09-22 16:11:34 -0700125 List<String> gatewayIps = getSubnetGatewayIps(sw);
Sangho Shin1aa93542014-09-22 09:49:44 -0700126 if (((ICMP)ipv4.getPayload()).getIcmpType() == ICMP_TYPE_ECHO &&
Sangho Shin463bee52014-09-29 15:14:43 -0700127 (destinationAddress.getInt() == switchIpAddress.getInt() ||
128 gatewayIps.contains(destinationAddress.toString()))) {
Srikanth Vavilapalli5ddf8f02014-09-24 11:02:28 -0700129 log.debug("ICMPHandler: ICMP packet for sw {} and "
130 + "sending ICMP response ", sw.getDpid());
Sangho Shin79c8d452014-09-18 09:50:21 -0700131 sendICMPResponse(sw, inPort, payload);
Sangho Shin463bee52014-09-29 15:14:43 -0700132 srManager.getIpPacketFromQueue(destinationAddress.getBytes());
Sangho Shin79c8d452014-09-18 09:50:21 -0700133 return;
134 }
135 }
Srikanth Vavilapalli5ddf8f02014-09-24 11:02:28 -0700136
137 /* Check if ICMP is for any switch known host */
138 for (Host host: sw.getHosts()) {
139 IPv4Address hostIpAddress =
140 IPv4Address.of(host.getIpAddress());
141 if (hostIpAddress != null &&
142 hostIpAddress.equals(destinationAddress)) {
143 /* TODO: We should not have come here as ARP itself
144 * would have installed a Route to the host. See if
145 * we can remove this code
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700146 * - It can happen when the rule is set later in switches
147 * (Ex: Ping reply arrives before the rules is set in the table)
148 * - The rule must be set by ARP handler. But, we set the rule
149 * again just in case and flush any pending packets to the host.
Srikanth Vavilapalli5ddf8f02014-09-24 11:02:28 -0700150 */
151 log.debug("ICMPHandler: ICMP request for known host {}",
152 hostIpAddress);
153 byte[] destinationMacAddress = host.getMacAddress().toBytes();
154 srManager.addRouteToHost(sw,
Sangho Shin463bee52014-09-29 15:14:43 -0700155 destinationAddress.getInt(), destinationMacAddress);
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700156
157 byte[] destIp = destinationAddress.getBytes();
158 for (IPv4 ipPacket: srManager.getIpPacketFromQueue(destIp)) {
159 if (ipPacket != null && !inSameSubnet(sw, ipPacket)) {
160 Ethernet eth = new Ethernet();
161 eth.setDestinationMACAddress(payload.getSourceMACAddress());
162 eth.setSourceMACAddress(sw.getStringAttribute("routerMac"));
163 eth.setEtherType(Ethernet.TYPE_IPV4);
164 eth.setPayload(ipPacket);
165 sendPacketOut(sw, eth, inPort.getSwitchPort(), false);
166 }
167 }
168
Srikanth Vavilapalli5ddf8f02014-09-24 11:02:28 -0700169 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 Shin463bee52014-09-29 15:14:43 -0700175 srManager.sendArpRequest(sw, destinationAddress.getInt(), inPort);
Sangho Shin2f263692014-09-15 14:09:41 -0700176 }
177
178 }
Sangho Shineb083032014-09-22 16:11:34 -0700179 }
Sangho Shin2f263692014-09-15 14:09:41 -0700180
Sangho Shin463bee52014-09-29 15:14:43 -0700181
182
Sangho Shineb083032014-09-22 16:11:34 -0700183 /**
184 * Retrieve Gateway IP address of all subnets defined in net config file
185 *
186 * @param sw Switch to retrieve subnet GW IPs for
187 * @return list of GW IP addresses for all subnets
188 */
189 private List<String> getSubnetGatewayIps(Switch sw) {
190
191 List<String> gatewayIps = new ArrayList<String>();
192
193 String subnets = sw.getStringAttribute("subnets");
194 try {
195 JSONArray arry = new JSONArray(subnets);
196 for (int i = 0; i < arry.length(); i++) {
197 String subnetIpSlash = (String) arry.getJSONObject(i).get("subnetIp");
198 if (subnetIpSlash != null) {
199 String subnetIp = subnetIpSlash.substring(0, subnetIpSlash.indexOf('/'));
200 gatewayIps.add(subnetIp);
201 }
202 }
203 } catch (JSONException e) {
204 // TODO Auto-generated catch block
205 e.printStackTrace();
206 }
207
208 return gatewayIps;
Sangho Shin2f263692014-09-15 14:09:41 -0700209 }
210
Sangho Shin79c8d452014-09-18 09:50:21 -0700211
Sangho Shin79c8d452014-09-18 09:50:21 -0700212 /**
213 * Send ICMP reply back
214 *
215 * @param sw Switch
216 * @param inPort Port the ICMP packet is forwarded from
217 * @param icmpRequest the ICMP request to handle
218 * @param destinationAddress destination address to send ICMP response to
219 */
220 private void sendICMPResponse(Switch sw, Port inPort, Ethernet icmpRequest) {
221
222 Ethernet icmpReplyEth = new Ethernet();
223
224 IPv4 icmpRequestIpv4 = (IPv4) icmpRequest.getPayload();
225 IPv4 icmpReplyIpv4 = new IPv4();
226 int destAddress = icmpRequestIpv4.getDestinationAddress();
227 icmpReplyIpv4.setDestinationAddress(icmpRequestIpv4.getSourceAddress());
228 icmpReplyIpv4.setSourceAddress(destAddress);
229 icmpReplyIpv4.setTtl((byte)64);
230 icmpReplyIpv4.setChecksum((short)0);
231
232
233 ICMP icmpReply = (ICMP)icmpRequestIpv4.getPayload().clone();
234 icmpReply.setIcmpCode((byte)0x00);
Sangho Shin1aa93542014-09-22 09:49:44 -0700235 icmpReply.setIcmpType((byte) ICMP_TYPE_REPLY);
Sangho Shin79c8d452014-09-18 09:50:21 -0700236 icmpReply.setChecksum((short)0);
237
238 icmpReplyIpv4.setPayload(icmpReply);
239
240 icmpReplyEth.setPayload(icmpReplyIpv4);
241 icmpReplyEth.setEtherType(Ethernet.TYPE_IPV4);
242 icmpReplyEth.setDestinationMACAddress(icmpRequest.getSourceMACAddress());
243 icmpReplyEth.setSourceMACAddress(icmpRequest.getDestinationMACAddress());
244
Sangho Shin1aa93542014-09-22 09:49:44 -0700245 sendPacketOut(sw, icmpReplyEth, new SwitchPort(sw.getDpid(), inPort.getPortNumber()), false);
Sangho Shin79c8d452014-09-18 09:50:21 -0700246
247 log.debug("Send an ICMP response {}", icmpReplyIpv4.toString());
248
249 }
250
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700251 /**
Sangho Shin1aa93542014-09-22 09:49:44 -0700252 * Send PACKET_OUT message with actions
253 * If switches support OFPP_TABLE action, it sends out packet to TABLE port
254 * Otherwise, it sends the packet to the port the packet came from
255 * (in this case, MPLS label is added if the packet needs go through transit switches)
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700256 *
257 * @param sw Switch the packet came from
258 * @param packet Ethernet packet to send
259 * @param switchPort port to send the packet
260 */
Sangho Shin1aa93542014-09-22 09:49:44 -0700261 private void sendPacketOut(Switch sw, Ethernet packet, SwitchPort switchPort, boolean supportOfppTable) {
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700262
263 boolean sameSubnet = false;
264 IOFSwitch ofSwitch = floodlightProvider.getMasterSwitch(sw.getDpid().value());
265 OFFactory factory = ofSwitch.getFactory();
266
267 List<OFAction> actions = new ArrayList<>();
268
Sangho Shin1aa93542014-09-22 09:49:44 -0700269 // If OFPP_TABLE action is not supported in the switch, MPLS label needs to be set
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
273 int destinationAddress = ((IPv4)packet.getPayload()).getDestinationAddress();
274 for (net.onrc.onos.core.topology.Host host: mutableTopology.getHosts(switchPort)) {
275 IPv4Address hostIpAddress = IPv4Address.of(host.getIpAddress());
276 if (hostIpAddress != null && hostIpAddress.getInt() == destinationAddress) {
277 sameSubnet = true;
278 break;
279 }
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700280 }
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700281
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700282 IPv4Address targetAddress = IPv4Address.of(((IPv4)packet.getPayload()).getDestinationAddress());
283 String destMacAddress = packet.getDestinationMAC().toString();
284 // If the destination host is not attached in the switch
285 // and the destination is not the neighbor switch, then add MPLS label
286 String targetMac = getRouterMACFromConfig(targetAddress);
287 if (!sameSubnet && !targetMac.equals(destMacAddress)) {
Sangho Shin1aa93542014-09-22 09:49:44 -0700288 int mplsLabel = getMplsLabelFromConfig(targetAddress);
289 if (mplsLabel > 0) {
290 OFAction pushlabel = factory.actions().pushMpls(EthType.MPLS_UNICAST);
291 OFOxmMplsLabel l = factory.oxms()
292 .mplsLabel(U32.of(mplsLabel));
293 OFAction setlabelid = factory.actions().buildSetField()
294 .setField(l).build();
295 OFAction copyTtlOut = factory.actions().copyTtlOut();
296 actions.add(pushlabel);
297 actions.add(setlabelid);
298 actions.add(copyTtlOut);
299 }
300 }
301
302 OFAction outport = factory.actions().output(OFPort.of(switchPort.getPortNumber().shortValue()), Short.MAX_VALUE);
303 actions.add(outport);
304 }
305 // If OFPP_TABLE action is supported, first set a rule to allow packet from CONTROLLER port.
306 // Then, send the packet to the table port
307 else {
308 if (!controllerPortAllowed) {
309 addControlPortInVlanTable(sw);
310 controllerPortAllowed = true;
311 }
312 OFAction outport = factory.actions().output(OFPort.TABLE, Short.MAX_VALUE);
313 actions.add(outport);
314 }
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700315
316 OFPacketOut po = factory.buildPacketOut()
317 .setData(packet.serialize())
318 .setActions(actions)
319 .build();
320
321 flowPusher.add(sw.getDpid(), po);
322 }
323
Sangho Shin2f263692014-09-15 14:09:41 -0700324 /**
Sangho Shin1aa93542014-09-22 09:49:44 -0700325 * Get MPLS label for the target address from the network config file
326 *
327 * @param targetAddress - IP address of the target host
328 * @return MPLS label of the switch to send packets to the target address
329 */
330 private int getMplsLabelFromConfig(IPv4Address targetAddress) {
331
332 int mplsLabel = -1;
333
334 for (Switch sw: mutableTopology.getSwitches()) {
335
336 String subnets = sw.getStringAttribute("subnets");
337 try {
338 JSONArray arry = new JSONArray(subnets);
339 for (int i = 0; i < arry.length(); i++) {
340 String subnetIp = (String) arry.getJSONObject(i).get("subnetIp");
341 if (srManager.netMatch(subnetIp, targetAddress.toString())) {
342 String mplsLabelStr = sw.getStringAttribute("nodeSid");
343 if (mplsLabelStr != null)
344 mplsLabel = Integer.parseInt(mplsLabelStr);
345 }
346 }
347 } catch (JSONException e) {
348 // TODO Auto-generated catch block
349 e.printStackTrace();
350 }
351 }
352
353 return mplsLabel;
354 }
355
Sangho Shin2f263692014-09-15 14:09:41 -0700356
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700357 /**
358 * Get Router MAC Address for the target address from the network config file
359 *
360 * @param targetAddress - IP address of the target host
361 * @return Router MAC of the switch to send packets to the target address
362 */
363 private String getRouterMACFromConfig(IPv4Address targetAddress) {
364
365 String routerMac = null;
366
367 for (Switch sw: mutableTopology.getSwitches()) {
368
369 String subnets = sw.getStringAttribute("subnets");
370 try {
371 JSONArray arry = new JSONArray(subnets);
372 for (int i = 0; i < arry.length(); i++) {
373 String subnetIp = (String) arry.getJSONObject(i).get("subnetIp");
374 if (srManager.netMatch(subnetIp, targetAddress.toString())) {
375 routerMac = sw.getStringAttribute("routerMac");
376 }
377 }
378 } catch (JSONException e) {
379 // TODO Auto-generated catch block
380 e.printStackTrace();
381 }
382 }
383
384 return routerMac;
385 }
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700386
387 /**
388 * Add a new rule to VLAN table to forward packets from any port to the next table
389 * It is required to forward packets from controller to pipeline
390 *
391 * @param sw Switch the packet came from
392 */
393 private void addControlPortInVlanTable(Switch sw) {
394
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700395 IOFSwitch ofSwitch = floodlightProvider.getMasterSwitch(sw.getDpid().value());
396 OFFactory factory = ofSwitch.getFactory();
397
Sangho Shin1aa93542014-09-22 09:49:44 -0700398 OFOxmInPort oxp = factory.oxms().inPort(OFPort.CONTROLLER);
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700399 OFOxmVlanVid oxv = factory.oxms()
400 .vlanVid(OFVlanVidMatch.UNTAGGED);
401 OFOxmList oxmList = OFOxmList.of(oxv);
402
Sangho Shin1aa93542014-09-22 09:49:44 -0700403 /* Cqpd switch does not seems to support CONTROLLER port as in_port match rule */
404 //OFOxmList oxmList = OFOxmList.of(oxp, oxv);
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700405
406 OFMatchV3 match = factory.buildMatchV3()
407 .setOxmList(oxmList)
408 .build();
409
410 OFInstruction gotoTbl = factory.instructions().buildGotoTable()
411 .setTableId(TableId.of(TABLE_TMAC)).build();
412 List<OFInstruction> instructions = new ArrayList<OFInstruction>();
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700413 instructions.add(gotoTbl);
414 OFMessage flowEntry = factory.buildFlowAdd()
415 .setTableId(TableId.of(TABLE_VLAN))
416 .setMatch(match)
417 .setInstructions(instructions)
418 .setPriority(1000) // does not matter - all rules
419 // exclusive
420 .setBufferId(OFBufferId.NO_BUFFER)
421 .setIdleTimeout(0)
422 .setHardTimeout(0)
423 //.setXid(getNextTransactionId())
424 .build();
425
426 flowPusher.add(sw.getDpid(), flowEntry);;
Sangho Shin1aa93542014-09-22 09:49:44 -0700427 log.debug("Adding a new vlan-rules in sw {}", sw.getDpid());
Sangho Shin2f263692014-09-15 14:09:41 -0700428
429 }
430
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700431 /**
432 * Check if the source IP and destination IP are in the same subnet
433 *
434 * @param sw Switch
435 * @param ipv4 IP address to check
436 * @return return true if the IP packet is within the same subnet
437 */
438 private boolean inSameSubnet(Switch sw, IPv4 ipv4) {
439
440 String gwIpSrc = getGwIpForSubnet(ipv4.getSourceAddress());
441 String gwIpDest = getGwIpForSubnet(ipv4.getDestinationAddress());
442
443 if (gwIpSrc.equals(gwIpDest)) {
444 return true;
445 }
446 else
447 return false;
448 }
449
450 /**
451 * Get router IP address for the given IP address
452 *
453 * @param sourceAddress
454 * @return
455 */
456 private String getGwIpForSubnet(int sourceAddress) {
457
458 String gwIp = null;
459 IPv4Address srcIp = IPv4Address.of(sourceAddress);
460
461 for (Switch sw: mutableTopology.getSwitches()) {
462
463 String subnets = sw.getStringAttribute("subnets");
464 try {
465 JSONArray arry = new JSONArray(subnets);
466 for (int i = 0; i < arry.length(); i++) {
467 String subnetIpSlash = (String) arry.getJSONObject(i).get("subnetIp");
468 if (srManager.netMatch(subnetIpSlash, srcIp.toString())) {
469 gwIp = subnetIpSlash;
470 }
471 }
472 } catch (JSONException e) {
473 // TODO Auto-generated catch block
474 e.printStackTrace();
475 }
476 }
477
478 return gwIp;
479 }
480
Sangho Shin2f263692014-09-15 14:09:41 -0700481}