blob: 778658adc682dcce380ba62ff7846b76d1aeedf0 [file] [log] [blame]
Sangho Shin463bee52014-09-29 15:14:43 -07001/*******************************************************************************
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 Shin2f263692014-09-15 14:09:41 -07009package net.onrc.onos.apps.segmentrouting;
10
11import java.util.ArrayList;
Sangho Shin463bee52014-09-29 15:14:43 -070012import java.util.LinkedList;
Sangho Shin2f263692014-09-15 14:09:41 -070013import java.util.List;
Sangho Shin463bee52014-09-29 15:14:43 -070014import java.util.Queue;
Sangho Shin2f263692014-09-15 14:09:41 -070015
16import net.floodlightcontroller.core.IFloodlightProviderService;
17import net.floodlightcontroller.core.IOFSwitch;
18import net.floodlightcontroller.core.module.FloodlightModuleContext;
Sangho Shin2f263692014-09-15 14:09:41 -070019import net.onrc.onos.core.flowprogrammer.IFlowPusherService;
20import net.onrc.onos.core.packet.Ethernet;
Sangho Shin79c8d452014-09-18 09:50:21 -070021import net.onrc.onos.core.packet.ICMP;
Sangho Shin2f263692014-09-15 14:09:41 -070022import net.onrc.onos.core.packet.IPv4;
Srikanth Vavilapalli5ddf8f02014-09-24 11:02:28 -070023import net.onrc.onos.core.topology.Host;
Sangho Shin2f263692014-09-15 14:09:41 -070024import net.onrc.onos.core.topology.ITopologyService;
25import net.onrc.onos.core.topology.MutableTopology;
26import net.onrc.onos.core.topology.Port;
27import net.onrc.onos.core.topology.Switch;
Sangho Shin79c8d452014-09-18 09:50:21 -070028import net.onrc.onos.core.util.SwitchPort;
Sangho Shin2f263692014-09-15 14:09:41 -070029
Sangho Shin1aa93542014-09-22 09:49:44 -070030import org.json.JSONArray;
31import org.json.JSONException;
Sangho Shin2f263692014-09-15 14:09:41 -070032import org.projectfloodlight.openflow.protocol.OFFactory;
33import org.projectfloodlight.openflow.protocol.OFMatchV3;
34import org.projectfloodlight.openflow.protocol.OFMessage;
35import org.projectfloodlight.openflow.protocol.OFOxmList;
Sangho Shinf41ac0a2014-09-19 09:50:30 -070036import org.projectfloodlight.openflow.protocol.OFPacketOut;
Sangho Shin2f263692014-09-15 14:09:41 -070037import org.projectfloodlight.openflow.protocol.action.OFAction;
38import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
Sangho Shin1aa93542014-09-22 09:49:44 -070039import org.projectfloodlight.openflow.protocol.oxm.OFOxmInPort;
Sangho Shinf41ac0a2014-09-19 09:50:30 -070040import org.projectfloodlight.openflow.protocol.oxm.OFOxmMplsLabel;
41import org.projectfloodlight.openflow.protocol.oxm.OFOxmVlanVid;
Sangho Shin2f263692014-09-15 14:09:41 -070042import org.projectfloodlight.openflow.types.EthType;
43import org.projectfloodlight.openflow.types.IPv4Address;
Sangho Shin2f263692014-09-15 14:09:41 -070044import org.projectfloodlight.openflow.types.OFBufferId;
45import org.projectfloodlight.openflow.types.OFPort;
Sangho Shinf41ac0a2014-09-19 09:50:30 -070046import org.projectfloodlight.openflow.types.OFVlanVidMatch;
Sangho Shin2f263692014-09-15 14:09:41 -070047import org.projectfloodlight.openflow.types.TableId;
Sangho Shinf41ac0a2014-09-19 09:50:30 -070048import org.projectfloodlight.openflow.types.U32;
Sangho Shin2f263692014-09-15 14:09:41 -070049import org.slf4j.Logger;
50import org.slf4j.LoggerFactory;
51
Srikanth Vavilapallib1fce732014-09-24 14:09:50 -070052public class IcmpHandler {
Sangho Shin2f263692014-09-15 14:09:41 -070053
54 private SegmentRoutingManager srManager;
55 private IFloodlightProviderService floodlightProvider;
56 private MutableTopology mutableTopology;
Sangho Shin2f263692014-09-15 14:09:41 -070057 private ITopologyService topologyService;
58 private static final Logger log = LoggerFactory
Sangho Shinf41ac0a2014-09-19 09:50:30 -070059 .getLogger(IcmpHandler.class);
Sangho Shin2f263692014-09-15 14:09:41 -070060
61 private IFlowPusherService flowPusher;
Sangho Shin1aa93542014-09-22 09:49:44 -070062 private boolean controllerPortAllowed = false;
Sangho Shin2f263692014-09-15 14:09:41 -070063
Sangho Shin463bee52014-09-29 15:14:43 -070064 private Queue<IPv4> icmpQueue = new LinkedList<IPv4>();
65
Sangho Shin2f263692014-09-15 14:09:41 -070066 private static final int TABLE_VLAN = 0;
67 private static final int TABLE_TMAC = 1;
68 private static final int TABLE_IPv4_UNICAST = 2;
69 private static final int TABLE_MPLS = 3;
70 private static final int TABLE_META = 4;
71 private static final int TABLE_ACL = 5;
72
73 private static final short MAX_PRIORITY = (short) 0xffff;
74 private static final short SLASH_24_PRIORITY = (short) 0xfff0;
75 private static final short SLASH_16_PRIORITY = (short) 0xff00;
76 private static final short SLASH_8_PRIORITY = (short) 0xf000;
77 private static final short MIN_PRIORITY = 0x0;
78
Sangho Shin1aa93542014-09-22 09:49:44 -070079 private static final int ICMP_TYPE_ECHO = 0x08;
80 private static final int ICMP_TYPE_REPLY = 0x00;
81
Sangho Shin2f263692014-09-15 14:09:41 -070082 public IcmpHandler(FloodlightModuleContext context, SegmentRoutingManager manager) {
83
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -070084 this.floodlightProvider = context
85 .getServiceImpl(IFloodlightProviderService.class);
Sangho Shin2f263692014-09-15 14:09:41 -070086 this.flowPusher = context.getServiceImpl(IFlowPusherService.class);
Sangho Shin2f263692014-09-15 14:09:41 -070087 this.topologyService = context.getServiceImpl(ITopologyService.class);
88 this.mutableTopology = topologyService.getTopology();
89
90 this.srManager = manager;
Sangho Shin2f263692014-09-15 14:09:41 -070091 }
92
Sangho Shin463bee52014-09-29 15:14:43 -070093 /**
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -070094 * handle ICMP packets If it is for ICMP echo to router IP or any subnet GW
95 * IP, then send ICMP response on behalf of the switch. If it is for any
96 * hosts in subnets of the switches, but if the MAC address is not known,
97 * then send an ARP request to the subent. If the MAC address is known, then
98 * set the routing rule to the switch
99 *
Sangho Shin463bee52014-09-29 15:14:43 -0700100 * @param sw
101 * @param inPort
102 * @param payload
103 */
Srikanth Vavilapallib1fce732014-09-24 14:09:50 -0700104 public void processPacketIn(Switch sw, Port inPort, Ethernet payload) {
Sangho Shin2f263692014-09-15 14:09:41 -0700105
106 if (payload.getEtherType() == Ethernet.TYPE_IPV4) {
107
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700108 IPv4 ipv4 = (IPv4) payload.getPayload();
Sangho Shin79c8d452014-09-18 09:50:21 -0700109
Sangho Shin2f263692014-09-15 14:09:41 -0700110 if (ipv4.getProtocol() == IPv4.PROTOCOL_ICMP) {
Srikanth Vavilapalli5ddf8f02014-09-24 11:02:28 -0700111
112 log.debug("ICMPHandler: Received a ICMP packet {} from sw {} ",
113 payload.toString(), sw.getDpid());
Sangho Shin463bee52014-09-29 15:14:43 -0700114 IPv4Address destinationAddress =
115 IPv4Address.of(ipv4.getDestinationAddress());
Sangho Shin2f263692014-09-15 14:09:41 -0700116
Sangho Shin79c8d452014-09-18 09:50:21 -0700117 // Check if it is ICMP request to the switch
118 String switchIpAddressSlash = sw.getStringAttribute("routerIp");
119 if (switchIpAddressSlash != null) {
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700120 String switchIpAddressStr = switchIpAddressSlash.substring(0,
121 switchIpAddressSlash.indexOf('/'));
Sangho Shin79c8d452014-09-18 09:50:21 -0700122 IPv4Address switchIpAddress = IPv4Address.of(switchIpAddressStr);
Sangho Shineb083032014-09-22 16:11:34 -0700123 List<String> gatewayIps = getSubnetGatewayIps(sw);
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700124 if (((ICMP) ipv4.getPayload()).getIcmpType() == ICMP_TYPE_ECHO &&
Sangho Shin463bee52014-09-29 15:14:43 -0700125 (destinationAddress.getInt() == switchIpAddress.getInt() ||
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700126 gatewayIps.contains(destinationAddress.toString()))) {
Srikanth Vavilapalli5ddf8f02014-09-24 11:02:28 -0700127 log.debug("ICMPHandler: ICMP packet for sw {} and "
128 + "sending ICMP response ", sw.getDpid());
Sangho Shin79c8d452014-09-18 09:50:21 -0700129 sendICMPResponse(sw, inPort, payload);
Sangho Shin463bee52014-09-29 15:14:43 -0700130 srManager.getIpPacketFromQueue(destinationAddress.getBytes());
Sangho Shin79c8d452014-09-18 09:50:21 -0700131 return;
132 }
133 }
Srikanth Vavilapalli5ddf8f02014-09-24 11:02:28 -0700134
135 /* Check if ICMP is for any switch known host */
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700136 for (Host host : sw.getHosts()) {
Srikanth Vavilapalli5ddf8f02014-09-24 11:02:28 -0700137 IPv4Address hostIpAddress =
138 IPv4Address.of(host.getIpAddress());
139 if (hostIpAddress != null &&
140 hostIpAddress.equals(destinationAddress)) {
141 /* TODO: We should not have come here as ARP itself
142 * would have installed a Route to the host. See if
143 * we can remove this code
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700144 * - It can happen when the rule is set later in switches
145 * (Ex: Ping reply arrives before the rules is set in the table)
146 * - The rule must be set by ARP handler. But, we set the rule
147 * again just in case and flush any pending packets to the host.
Srikanth Vavilapalli5ddf8f02014-09-24 11:02:28 -0700148 */
149 log.debug("ICMPHandler: ICMP request for known host {}",
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700150 hostIpAddress);
Srikanth Vavilapalli5ddf8f02014-09-24 11:02:28 -0700151 byte[] destinationMacAddress = host.getMacAddress().toBytes();
152 srManager.addRouteToHost(sw,
Sangho Shin463bee52014-09-29 15:14:43 -0700153 destinationAddress.getInt(), destinationMacAddress);
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700154
155 byte[] destIp = destinationAddress.getBytes();
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700156 for (IPv4 ipPacket : srManager.getIpPacketFromQueue(destIp)) {
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700157 if (ipPacket != null && !inSameSubnet(sw, ipPacket)) {
158 Ethernet eth = new Ethernet();
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700159 eth.setDestinationMACAddress(payload
160 .getSourceMACAddress());
161 eth.setSourceMACAddress(sw
162 .getStringAttribute("routerMac"));
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700163 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 Shineb083032014-09-22 16:11:34 -0700181 /**
182 * Retrieve Gateway IP address of all subnets defined in net config file
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700183 *
Sangho Shineb083032014-09-22 16:11:34 -0700184 * @param sw Switch to retrieve subnet GW IPs for
185 * @return list of GW IP addresses for all subnets
186 */
187 private List<String> getSubnetGatewayIps(Switch sw) {
188
189 List<String> gatewayIps = new ArrayList<String>();
190
191 String subnets = sw.getStringAttribute("subnets");
192 try {
193 JSONArray arry = new JSONArray(subnets);
194 for (int i = 0; i < arry.length(); i++) {
195 String subnetIpSlash = (String) arry.getJSONObject(i).get("subnetIp");
196 if (subnetIpSlash != null) {
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700197 String subnetIp = subnetIpSlash.substring(0,
198 subnetIpSlash.indexOf('/'));
Sangho Shineb083032014-09-22 16:11:34 -0700199 gatewayIps.add(subnetIp);
200 }
201 }
202 } catch (JSONException e) {
203 // TODO Auto-generated catch block
204 e.printStackTrace();
205 }
206
207 return gatewayIps;
Sangho Shin2f263692014-09-15 14:09:41 -0700208 }
209
Sangho Shin79c8d452014-09-18 09:50:21 -0700210 /**
211 * Send ICMP reply back
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700212 *
Sangho Shin79c8d452014-09-18 09:50:21 -0700213 * @param sw Switch
214 * @param inPort Port the ICMP packet is forwarded from
215 * @param icmpRequest the ICMP request to handle
216 * @param destinationAddress destination address to send ICMP response to
217 */
218 private void sendICMPResponse(Switch sw, Port inPort, Ethernet icmpRequest) {
219
220 Ethernet icmpReplyEth = new Ethernet();
221
222 IPv4 icmpRequestIpv4 = (IPv4) icmpRequest.getPayload();
223 IPv4 icmpReplyIpv4 = new IPv4();
224 int destAddress = icmpRequestIpv4.getDestinationAddress();
225 icmpReplyIpv4.setDestinationAddress(icmpRequestIpv4.getSourceAddress());
226 icmpReplyIpv4.setSourceAddress(destAddress);
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700227 icmpReplyIpv4.setTtl((byte) 64);
228 icmpReplyIpv4.setChecksum((short) 0);
Sangho Shin79c8d452014-09-18 09:50:21 -0700229
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700230 ICMP icmpReply = (ICMP) icmpRequestIpv4.getPayload().clone();
231 icmpReply.setIcmpCode((byte) 0x00);
Sangho Shin1aa93542014-09-22 09:49:44 -0700232 icmpReply.setIcmpType((byte) ICMP_TYPE_REPLY);
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700233 icmpReply.setChecksum((short) 0);
Sangho Shin79c8d452014-09-18 09:50:21 -0700234
235 icmpReplyIpv4.setPayload(icmpReply);
236
237 icmpReplyEth.setPayload(icmpReplyIpv4);
238 icmpReplyEth.setEtherType(Ethernet.TYPE_IPV4);
239 icmpReplyEth.setDestinationMACAddress(icmpRequest.getSourceMACAddress());
240 icmpReplyEth.setSourceMACAddress(icmpRequest.getDestinationMACAddress());
241
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700242 sendPacketOut(sw, icmpReplyEth,
243 new SwitchPort(sw.getDpid(), inPort.getPortNumber()), false);
Sangho Shin79c8d452014-09-18 09:50:21 -0700244
245 log.debug("Send an ICMP response {}", icmpReplyIpv4.toString());
246
247 }
248
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700249 /**
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700250 * Send PACKET_OUT message with actions If switches support OFPP_TABLE
251 * action, it sends out packet to TABLE port Otherwise, it sends the packet
252 * to the port the packet came from (in this case, MPLS label is added if
253 * the packet needs go through transit switches)
254 *
255 * @param sw Switch the packet came from
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700256 * @param packet Ethernet packet to send
257 * @param switchPort port to send the packet
258 */
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700259 private void sendPacketOut(Switch sw, Ethernet packet, SwitchPort switchPort,
260 boolean supportOfppTable) {
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700261
262 boolean sameSubnet = false;
263 IOFSwitch ofSwitch = floodlightProvider.getMasterSwitch(sw.getDpid().value());
264 OFFactory factory = ofSwitch.getFactory();
265
266 List<OFAction> actions = new ArrayList<>();
267
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700268 // If OFPP_TABLE action is not supported in the switch, MPLS label needs
269 // to be set
Sangho Shin1aa93542014-09-22 09:49:44 -0700270 // if the packet needs to be delivered crossing switches
271 if (!supportOfppTable) {
272 // Check if the destination is the host attached to the switch
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700273 int destinationAddress = ((IPv4) packet.getPayload()).getDestinationAddress();
274 for (net.onrc.onos.core.topology.Host host : mutableTopology
275 .getHosts(switchPort)) {
Sangho Shin1aa93542014-09-22 09:49:44 -0700276 IPv4Address hostIpAddress = IPv4Address.of(host.getIpAddress());
277 if (hostIpAddress != null && hostIpAddress.getInt() == destinationAddress) {
278 sameSubnet = true;
279 break;
280 }
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700281 }
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700282
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700283 IPv4Address targetAddress = IPv4Address.of(((IPv4) packet.getPayload())
284 .getDestinationAddress());
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700285 String destMacAddress = packet.getDestinationMAC().toString();
286 // If the destination host is not attached in the switch
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700287 // and the destination is not the neighbor switch, then add MPLS
288 // label
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700289 String targetMac = getRouterMACFromConfig(targetAddress);
290 if (!sameSubnet && !targetMac.equals(destMacAddress)) {
Sangho Shin1aa93542014-09-22 09:49:44 -0700291 int mplsLabel = getMplsLabelFromConfig(targetAddress);
292 if (mplsLabel > 0) {
293 OFAction pushlabel = factory.actions().pushMpls(EthType.MPLS_UNICAST);
294 OFOxmMplsLabel l = factory.oxms()
295 .mplsLabel(U32.of(mplsLabel));
296 OFAction setlabelid = factory.actions().buildSetField()
297 .setField(l).build();
298 OFAction copyTtlOut = factory.actions().copyTtlOut();
299 actions.add(pushlabel);
300 actions.add(setlabelid);
301 actions.add(copyTtlOut);
302 }
303 }
304
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700305 OFAction outport = factory.actions().output(
306 OFPort.of(switchPort.getPortNumber().shortValue()), Short.MAX_VALUE);
Sangho Shin1aa93542014-09-22 09:49:44 -0700307 actions.add(outport);
308 }
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700309 // If OFPP_TABLE action is supported, first set a rule to allow packet
310 // from CONTROLLER port.
Sangho Shin1aa93542014-09-22 09:49:44 -0700311 // Then, send the packet to the table port
312 else {
313 if (!controllerPortAllowed) {
314 addControlPortInVlanTable(sw);
315 controllerPortAllowed = true;
316 }
317 OFAction outport = factory.actions().output(OFPort.TABLE, Short.MAX_VALUE);
318 actions.add(outport);
319 }
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700320
321 OFPacketOut po = factory.buildPacketOut()
322 .setData(packet.serialize())
323 .setActions(actions)
324 .build();
325
326 flowPusher.add(sw.getDpid(), po);
327 }
328
Sangho Shin2f263692014-09-15 14:09:41 -0700329 /**
Sangho Shin1aa93542014-09-22 09:49:44 -0700330 * Get MPLS label for the target address from the network config file
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700331 *
Sangho Shin1aa93542014-09-22 09:49:44 -0700332 * @param targetAddress - IP address of the target host
333 * @return MPLS label of the switch to send packets to the target address
334 */
335 private int getMplsLabelFromConfig(IPv4Address targetAddress) {
336
337 int mplsLabel = -1;
338
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700339 for (Switch sw : mutableTopology.getSwitches()) {
Sangho Shin1aa93542014-09-22 09:49:44 -0700340
341 String subnets = sw.getStringAttribute("subnets");
342 try {
343 JSONArray arry = new JSONArray(subnets);
344 for (int i = 0; i < arry.length(); i++) {
345 String subnetIp = (String) arry.getJSONObject(i).get("subnetIp");
346 if (srManager.netMatch(subnetIp, targetAddress.toString())) {
347 String mplsLabelStr = sw.getStringAttribute("nodeSid");
348 if (mplsLabelStr != null)
349 mplsLabel = Integer.parseInt(mplsLabelStr);
350 }
351 }
352 } catch (JSONException e) {
353 // TODO Auto-generated catch block
354 e.printStackTrace();
355 }
356 }
357
358 return mplsLabel;
359 }
360
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700361 /**
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700362 * Get Router MAC Address for the target address from the network config
363 * file
364 *
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700365 * @param targetAddress - IP address of the target host
366 * @return Router MAC of the switch to send packets to the target address
367 */
368 private String getRouterMACFromConfig(IPv4Address targetAddress) {
369
370 String routerMac = null;
371
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700372 for (Switch sw : mutableTopology.getSwitches()) {
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700373
374 String subnets = sw.getStringAttribute("subnets");
375 try {
376 JSONArray arry = new JSONArray(subnets);
377 for (int i = 0; i < arry.length(); i++) {
378 String subnetIp = (String) arry.getJSONObject(i).get("subnetIp");
379 if (srManager.netMatch(subnetIp, targetAddress.toString())) {
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700380 routerMac = sw.getStringAttribute("routerMac");
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700381 }
382 }
383 } catch (JSONException e) {
384 // TODO Auto-generated catch block
385 e.printStackTrace();
386 }
387 }
388
389 return routerMac;
390 }
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700391
392 /**
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700393 * Add a new rule to VLAN table to forward packets from any port to the next
394 * table It is required to forward packets from controller to pipeline
395 *
396 * @param sw Switch the packet came from
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700397 */
398 private void addControlPortInVlanTable(Switch sw) {
399
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700400 IOFSwitch ofSwitch = floodlightProvider.getMasterSwitch(sw.getDpid().value());
401 OFFactory factory = ofSwitch.getFactory();
402
Sangho Shin1aa93542014-09-22 09:49:44 -0700403 OFOxmInPort oxp = factory.oxms().inPort(OFPort.CONTROLLER);
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700404 OFOxmVlanVid oxv = factory.oxms()
405 .vlanVid(OFVlanVidMatch.UNTAGGED);
406 OFOxmList oxmList = OFOxmList.of(oxv);
407
Sangho Shin1aa93542014-09-22 09:49:44 -0700408 /* Cqpd switch does not seems to support CONTROLLER port as in_port match rule */
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700409 // OFOxmList oxmList = OFOxmList.of(oxp, oxv);
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700410
411 OFMatchV3 match = factory.buildMatchV3()
412 .setOxmList(oxmList)
413 .build();
414
415 OFInstruction gotoTbl = factory.instructions().buildGotoTable()
416 .setTableId(TableId.of(TABLE_TMAC)).build();
417 List<OFInstruction> instructions = new ArrayList<OFInstruction>();
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700418 instructions.add(gotoTbl);
419 OFMessage flowEntry = factory.buildFlowAdd()
420 .setTableId(TableId.of(TABLE_VLAN))
421 .setMatch(match)
422 .setInstructions(instructions)
423 .setPriority(1000) // does not matter - all rules
424 // exclusive
425 .setBufferId(OFBufferId.NO_BUFFER)
426 .setIdleTimeout(0)
427 .setHardTimeout(0)
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700428 // .setXid(getNextTransactionId())
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700429 .build();
430
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700431 flowPusher.add(sw.getDpid(), flowEntry);
Sangho Shin1aa93542014-09-22 09:49:44 -0700432 log.debug("Adding a new vlan-rules in sw {}", sw.getDpid());
Sangho Shin2f263692014-09-15 14:09:41 -0700433
434 }
435
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700436 /**
437 * Check if the source IP and destination IP are in the same subnet
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700438 *
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700439 * @param sw Switch
440 * @param ipv4 IP address to check
441 * @return return true if the IP packet is within the same subnet
442 */
443 private boolean inSameSubnet(Switch sw, IPv4 ipv4) {
444
445 String gwIpSrc = getGwIpForSubnet(ipv4.getSourceAddress());
446 String gwIpDest = getGwIpForSubnet(ipv4.getDestinationAddress());
447
448 if (gwIpSrc.equals(gwIpDest)) {
449 return true;
450 }
451 else
452 return false;
453 }
454
455 /**
456 * Get router IP address for the given IP address
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700457 *
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700458 * @param sourceAddress
459 * @return
460 */
461 private String getGwIpForSubnet(int sourceAddress) {
462
463 String gwIp = null;
464 IPv4Address srcIp = IPv4Address.of(sourceAddress);
465
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700466 for (Switch sw : mutableTopology.getSwitches()) {
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700467
468 String subnets = sw.getStringAttribute("subnets");
469 try {
470 JSONArray arry = new JSONArray(subnets);
471 for (int i = 0; i < arry.length(); i++) {
472 String subnetIpSlash = (String) arry.getJSONObject(i).get("subnetIp");
473 if (srManager.netMatch(subnetIpSlash, srcIp.toString())) {
474 gwIp = subnetIpSlash;
475 }
476 }
477 } catch (JSONException e) {
478 // TODO Auto-generated catch block
479 e.printStackTrace();
480 }
481 }
482
483 return gwIp;
484 }
485
Sangho Shin2f263692014-09-15 14:09:41 -0700486}