blob: 9366a2e6f709a34272d5b7aa498c50a9b8e8876b [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;
12import java.util.List;
13
14import net.floodlightcontroller.core.IFloodlightProviderService;
15import net.floodlightcontroller.core.IOFSwitch;
16import net.floodlightcontroller.core.module.FloodlightModuleContext;
Sangho Shin2f263692014-09-15 14:09:41 -070017import net.onrc.onos.core.flowprogrammer.IFlowPusherService;
18import net.onrc.onos.core.packet.Ethernet;
Sangho Shin79c8d452014-09-18 09:50:21 -070019import net.onrc.onos.core.packet.ICMP;
Sangho Shin2f263692014-09-15 14:09:41 -070020import net.onrc.onos.core.packet.IPv4;
Srikanth Vavilapalli5ddf8f02014-09-24 11:02:28 -070021import net.onrc.onos.core.topology.Host;
Sangho Shin2f263692014-09-15 14:09:41 -070022import net.onrc.onos.core.topology.ITopologyService;
23import net.onrc.onos.core.topology.MutableTopology;
24import net.onrc.onos.core.topology.Port;
25import net.onrc.onos.core.topology.Switch;
Sangho Shin79c8d452014-09-18 09:50:21 -070026import net.onrc.onos.core.util.SwitchPort;
Sangho Shin2f263692014-09-15 14:09:41 -070027
Sangho Shin1aa93542014-09-22 09:49:44 -070028import org.json.JSONArray;
29import org.json.JSONException;
Sangho Shin2f263692014-09-15 14:09:41 -070030import org.projectfloodlight.openflow.protocol.OFFactory;
31import org.projectfloodlight.openflow.protocol.OFMatchV3;
32import org.projectfloodlight.openflow.protocol.OFMessage;
33import org.projectfloodlight.openflow.protocol.OFOxmList;
Sangho Shinf41ac0a2014-09-19 09:50:30 -070034import org.projectfloodlight.openflow.protocol.OFPacketOut;
Sangho Shin2f263692014-09-15 14:09:41 -070035import org.projectfloodlight.openflow.protocol.action.OFAction;
36import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
Sangho Shin1aa93542014-09-22 09:49:44 -070037import org.projectfloodlight.openflow.protocol.oxm.OFOxmInPort;
Sangho Shinf41ac0a2014-09-19 09:50:30 -070038import org.projectfloodlight.openflow.protocol.oxm.OFOxmMplsLabel;
39import org.projectfloodlight.openflow.protocol.oxm.OFOxmVlanVid;
Sangho Shin2f263692014-09-15 14:09:41 -070040import org.projectfloodlight.openflow.types.EthType;
41import org.projectfloodlight.openflow.types.IPv4Address;
Sangho Shin2f263692014-09-15 14:09:41 -070042import org.projectfloodlight.openflow.types.OFBufferId;
43import org.projectfloodlight.openflow.types.OFPort;
Sangho Shinf41ac0a2014-09-19 09:50:30 -070044import org.projectfloodlight.openflow.types.OFVlanVidMatch;
Sangho Shin2f263692014-09-15 14:09:41 -070045import org.projectfloodlight.openflow.types.TableId;
Sangho Shinf41ac0a2014-09-19 09:50:30 -070046import org.projectfloodlight.openflow.types.U32;
Sangho Shin2f263692014-09-15 14:09:41 -070047import org.slf4j.Logger;
48import org.slf4j.LoggerFactory;
49
Srikanth Vavilapallib1fce732014-09-24 14:09:50 -070050public class IcmpHandler {
Sangho Shin2f263692014-09-15 14:09:41 -070051
52 private SegmentRoutingManager srManager;
53 private IFloodlightProviderService floodlightProvider;
54 private MutableTopology mutableTopology;
Sangho Shin2f263692014-09-15 14:09:41 -070055 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 public IcmpHandler(FloodlightModuleContext context, SegmentRoutingManager manager) {
79
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -070080 this.floodlightProvider = context
81 .getServiceImpl(IFloodlightProviderService.class);
Sangho Shin2f263692014-09-15 14:09:41 -070082 this.flowPusher = context.getServiceImpl(IFlowPusherService.class);
Sangho Shin2f263692014-09-15 14:09:41 -070083 this.topologyService = context.getServiceImpl(ITopologyService.class);
84 this.mutableTopology = topologyService.getTopology();
85
86 this.srManager = manager;
Sangho Shin2f263692014-09-15 14:09:41 -070087 }
88
Sangho Shin463bee52014-09-29 15:14:43 -070089 /**
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -070090 * handle ICMP packets If it is for ICMP echo to router IP or any subnet GW
91 * IP, then send ICMP response on behalf of the switch. If it is for any
92 * hosts in subnets of the switches, but if the MAC address is not known,
93 * then send an ARP request to the subent. If the MAC address is known, then
94 * set the routing rule to the switch
Sangho Shinfbc572c2014-10-02 16:37:05 -070095 *
Sangho Shin463bee52014-09-29 15:14:43 -070096 * @param sw
97 * @param inPort
98 * @param payload
99 */
Srikanth Vavilapallib1fce732014-09-24 14:09:50 -0700100 public void processPacketIn(Switch sw, Port inPort, Ethernet payload) {
Sangho Shin2f263692014-09-15 14:09:41 -0700101
102 if (payload.getEtherType() == Ethernet.TYPE_IPV4) {
103
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700104 IPv4 ipv4 = (IPv4) payload.getPayload();
Sangho Shin79c8d452014-09-18 09:50:21 -0700105
Sangho Shin2f263692014-09-15 14:09:41 -0700106 if (ipv4.getProtocol() == IPv4.PROTOCOL_ICMP) {
Srikanth Vavilapalli5ddf8f02014-09-24 11:02:28 -0700107
108 log.debug("ICMPHandler: Received a ICMP packet {} from sw {} ",
109 payload.toString(), sw.getDpid());
Sangho Shin463bee52014-09-29 15:14:43 -0700110 IPv4Address destinationAddress =
111 IPv4Address.of(ipv4.getDestinationAddress());
Sangho Shin2f263692014-09-15 14:09:41 -0700112
Sangho Shin79c8d452014-09-18 09:50:21 -0700113 // Check if it is ICMP request to the switch
114 String switchIpAddressSlash = sw.getStringAttribute("routerIp");
115 if (switchIpAddressSlash != null) {
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700116 String switchIpAddressStr = switchIpAddressSlash.substring(0,
117 switchIpAddressSlash.indexOf('/'));
Sangho Shin79c8d452014-09-18 09:50:21 -0700118 IPv4Address switchIpAddress = IPv4Address.of(switchIpAddressStr);
Sangho Shineb083032014-09-22 16:11:34 -0700119 List<String> gatewayIps = getSubnetGatewayIps(sw);
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700120 if (((ICMP) ipv4.getPayload()).getIcmpType() == ICMP_TYPE_ECHO &&
Sangho Shin463bee52014-09-29 15:14:43 -0700121 (destinationAddress.getInt() == switchIpAddress.getInt() ||
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700122 gatewayIps.contains(destinationAddress.toString()))) {
Srikanth Vavilapalli5ddf8f02014-09-24 11:02:28 -0700123 log.debug("ICMPHandler: ICMP packet for sw {} and "
124 + "sending ICMP response ", sw.getDpid());
Sangho Shin79c8d452014-09-18 09:50:21 -0700125 sendICMPResponse(sw, inPort, payload);
Sangho Shin463bee52014-09-29 15:14:43 -0700126 srManager.getIpPacketFromQueue(destinationAddress.getBytes());
Sangho Shin79c8d452014-09-18 09:50:21 -0700127 return;
128 }
129 }
Srikanth Vavilapalli5ddf8f02014-09-24 11:02:28 -0700130
131 /* Check if ICMP is for any switch known host */
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700132 for (Host host : sw.getHosts()) {
Srikanth Vavilapalli5ddf8f02014-09-24 11:02:28 -0700133 IPv4Address hostIpAddress =
134 IPv4Address.of(host.getIpAddress());
135 if (hostIpAddress != null &&
136 hostIpAddress.equals(destinationAddress)) {
137 /* TODO: We should not have come here as ARP itself
138 * would have installed a Route to the host. See if
139 * we can remove this code
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700140 * - It can happen when the rule is set later in switches
141 * (Ex: Ping reply arrives before the rules is set in the table)
142 * - The rule must be set by ARP handler. But, we set the rule
143 * again just in case and flush any pending packets to the host.
Srikanth Vavilapalli5ddf8f02014-09-24 11:02:28 -0700144 */
145 log.debug("ICMPHandler: ICMP request for known host {}",
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700146 hostIpAddress);
Srikanth Vavilapalli5ddf8f02014-09-24 11:02:28 -0700147 byte[] destinationMacAddress = host.getMacAddress().toBytes();
148 srManager.addRouteToHost(sw,
Sangho Shin463bee52014-09-29 15:14:43 -0700149 destinationAddress.getInt(), destinationMacAddress);
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700150 byte[] destIp = destinationAddress.getBytes();
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700151 for (IPv4 ipPacket : srManager.getIpPacketFromQueue(destIp)) {
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700152 if (ipPacket != null && !inSameSubnet(sw, ipPacket)) {
153 Ethernet eth = new Ethernet();
Sangho Shinfbc572c2014-10-02 16:37:05 -0700154 eth.setDestinationMACAddress(destinationMacAddress);
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700155 eth.setSourceMACAddress(sw
156 .getStringAttribute("routerMac"));
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700157 eth.setEtherType(Ethernet.TYPE_IPV4);
158 eth.setPayload(ipPacket);
Sangho Shinfbc572c2014-10-02 16:37:05 -0700159 for (Port port: host.getAttachmentPoints())
160 sendPacketOut(sw, eth, new SwitchPort(
161 port.getDpid(),port.getNumber()), false);
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700162 }
163 }
164
Srikanth Vavilapalli5ddf8f02014-09-24 11:02:28 -0700165 return;
166 }
167 }
168 /* ICMP for an unknown host */
169 log.debug("ICMPHandler: ICMP request for unknown host {}"
170 + " and sending ARP request", destinationAddress);
Sangho Shin463bee52014-09-29 15:14:43 -0700171 srManager.sendArpRequest(sw, destinationAddress.getInt(), inPort);
Sangho Shin2f263692014-09-15 14:09:41 -0700172 }
173
174 }
Sangho Shineb083032014-09-22 16:11:34 -0700175 }
Sangho Shin2f263692014-09-15 14:09:41 -0700176
Sangho Shineb083032014-09-22 16:11:34 -0700177 /**
178 * Retrieve Gateway IP address of all subnets defined in net config file
Sangho Shinfbc572c2014-10-02 16:37:05 -0700179 *
Sangho Shineb083032014-09-22 16:11:34 -0700180 * @param sw Switch to retrieve subnet GW IPs for
181 * @return list of GW IP addresses for all subnets
182 */
183 private List<String> getSubnetGatewayIps(Switch sw) {
184
185 List<String> gatewayIps = new ArrayList<String>();
186
187 String subnets = sw.getStringAttribute("subnets");
188 try {
189 JSONArray arry = new JSONArray(subnets);
190 for (int i = 0; i < arry.length(); i++) {
191 String subnetIpSlash = (String) arry.getJSONObject(i).get("subnetIp");
192 if (subnetIpSlash != null) {
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700193 String subnetIp = subnetIpSlash.substring(0,
194 subnetIpSlash.indexOf('/'));
Sangho Shineb083032014-09-22 16:11:34 -0700195 gatewayIps.add(subnetIp);
196 }
197 }
198 } catch (JSONException e) {
199 // TODO Auto-generated catch block
200 e.printStackTrace();
201 }
202
203 return gatewayIps;
Sangho Shin2f263692014-09-15 14:09:41 -0700204 }
205
Sangho Shin79c8d452014-09-18 09:50:21 -0700206 /**
207 * Send ICMP reply back
Sangho Shinfbc572c2014-10-02 16:37:05 -0700208 *
Sangho Shin79c8d452014-09-18 09:50:21 -0700209 * @param sw Switch
210 * @param inPort Port the ICMP packet is forwarded from
211 * @param icmpRequest the ICMP request to handle
212 * @param destinationAddress destination address to send ICMP response to
213 */
214 private void sendICMPResponse(Switch sw, Port inPort, Ethernet icmpRequest) {
215
216 Ethernet icmpReplyEth = new Ethernet();
217
218 IPv4 icmpRequestIpv4 = (IPv4) icmpRequest.getPayload();
219 IPv4 icmpReplyIpv4 = new IPv4();
220 int destAddress = icmpRequestIpv4.getDestinationAddress();
221 icmpReplyIpv4.setDestinationAddress(icmpRequestIpv4.getSourceAddress());
222 icmpReplyIpv4.setSourceAddress(destAddress);
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700223 icmpReplyIpv4.setTtl((byte) 64);
224 icmpReplyIpv4.setChecksum((short) 0);
Sangho Shin79c8d452014-09-18 09:50:21 -0700225
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700226 ICMP icmpReply = (ICMP) icmpRequestIpv4.getPayload().clone();
227 icmpReply.setIcmpCode((byte) 0x00);
Sangho Shin1aa93542014-09-22 09:49:44 -0700228 icmpReply.setIcmpType((byte) ICMP_TYPE_REPLY);
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700229 icmpReply.setChecksum((short) 0);
Sangho Shin79c8d452014-09-18 09:50:21 -0700230
231 icmpReplyIpv4.setPayload(icmpReply);
232
233 icmpReplyEth.setPayload(icmpReplyIpv4);
234 icmpReplyEth.setEtherType(Ethernet.TYPE_IPV4);
235 icmpReplyEth.setDestinationMACAddress(icmpRequest.getSourceMACAddress());
236 icmpReplyEth.setSourceMACAddress(icmpRequest.getDestinationMACAddress());
237
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700238 sendPacketOut(sw, icmpReplyEth,
239 new SwitchPort(sw.getDpid(), inPort.getPortNumber()), false);
Sangho Shin79c8d452014-09-18 09:50:21 -0700240
241 log.debug("Send an ICMP response {}", icmpReplyIpv4.toString());
242
243 }
244
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700245 /**
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700246 * Send PACKET_OUT message with actions If switches support OFPP_TABLE
247 * action, it sends out packet to TABLE port Otherwise, it sends the packet
248 * to the port the packet came from (in this case, MPLS label is added if
249 * the packet needs go through transit switches)
Sangho Shinfbc572c2014-10-02 16:37:05 -0700250 *
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700251 * @param sw Switch the packet came from
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700252 * @param packet Ethernet packet to send
253 * @param switchPort port to send the packet
254 */
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700255 private void sendPacketOut(Switch sw, Ethernet packet, SwitchPort switchPort,
256 boolean supportOfppTable) {
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700257
258 boolean sameSubnet = false;
259 IOFSwitch ofSwitch = floodlightProvider.getMasterSwitch(sw.getDpid().value());
260 OFFactory factory = ofSwitch.getFactory();
261
262 List<OFAction> actions = new ArrayList<>();
263
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700264 // If OFPP_TABLE action is not supported in the switch, MPLS label needs
265 // to be set
Sangho Shin1aa93542014-09-22 09:49:44 -0700266 // if the packet needs to be delivered crossing switches
267 if (!supportOfppTable) {
268 // Check if the destination is the host attached to the switch
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700269 int destinationAddress = ((IPv4) packet.getPayload()).getDestinationAddress();
270 for (net.onrc.onos.core.topology.Host host : mutableTopology
271 .getHosts(switchPort)) {
Sangho Shin1aa93542014-09-22 09:49:44 -0700272 IPv4Address hostIpAddress = IPv4Address.of(host.getIpAddress());
273 if (hostIpAddress != null && hostIpAddress.getInt() == destinationAddress) {
274 sameSubnet = true;
275 break;
276 }
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700277 }
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700278
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700279 IPv4Address targetAddress = IPv4Address.of(((IPv4) packet.getPayload())
280 .getDestinationAddress());
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700281 String destMacAddress = packet.getDestinationMAC().toString();
282 // If the destination host is not attached in the switch
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700283 // and the destination is not the neighbor switch, then add MPLS
284 // label
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700285 String targetMac = getRouterMACFromConfig(targetAddress);
286 if (!sameSubnet && !targetMac.equals(destMacAddress)) {
Sangho Shin1aa93542014-09-22 09:49:44 -0700287 int mplsLabel = getMplsLabelFromConfig(targetAddress);
288 if (mplsLabel > 0) {
289 OFAction pushlabel = factory.actions().pushMpls(EthType.MPLS_UNICAST);
290 OFOxmMplsLabel l = factory.oxms()
291 .mplsLabel(U32.of(mplsLabel));
292 OFAction setlabelid = factory.actions().buildSetField()
293 .setField(l).build();
294 OFAction copyTtlOut = factory.actions().copyTtlOut();
295 actions.add(pushlabel);
296 actions.add(setlabelid);
297 actions.add(copyTtlOut);
298 }
299 }
300
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700301 OFAction outport = factory.actions().output(
302 OFPort.of(switchPort.getPortNumber().shortValue()), Short.MAX_VALUE);
Sangho Shin1aa93542014-09-22 09:49:44 -0700303 actions.add(outport);
304 }
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700305 // If OFPP_TABLE action is supported, first set a rule to allow packet
306 // from CONTROLLER port.
Sangho Shin1aa93542014-09-22 09:49:44 -0700307 // Then, send the packet to the table port
308 else {
309 if (!controllerPortAllowed) {
310 addControlPortInVlanTable(sw);
311 controllerPortAllowed = true;
312 }
313 OFAction outport = factory.actions().output(OFPort.TABLE, Short.MAX_VALUE);
314 actions.add(outport);
315 }
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700316
317 OFPacketOut po = factory.buildPacketOut()
318 .setData(packet.serialize())
319 .setActions(actions)
320 .build();
321
322 flowPusher.add(sw.getDpid(), po);
323 }
324
Sangho Shin2f263692014-09-15 14:09:41 -0700325 /**
Sangho Shin1aa93542014-09-22 09:49:44 -0700326 * Get MPLS label for the target address from the network config file
Sangho Shinfbc572c2014-10-02 16:37:05 -0700327 *
Sangho Shin1aa93542014-09-22 09:49:44 -0700328 * @param targetAddress - IP address of the target host
329 * @return MPLS label of the switch to send packets to the target address
330 */
331 private int getMplsLabelFromConfig(IPv4Address targetAddress) {
332
333 int mplsLabel = -1;
334
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700335 for (Switch sw : mutableTopology.getSwitches()) {
Sangho Shin1aa93542014-09-22 09:49:44 -0700336
337 String subnets = sw.getStringAttribute("subnets");
338 try {
339 JSONArray arry = new JSONArray(subnets);
340 for (int i = 0; i < arry.length(); i++) {
341 String subnetIp = (String) arry.getJSONObject(i).get("subnetIp");
342 if (srManager.netMatch(subnetIp, targetAddress.toString())) {
343 String mplsLabelStr = sw.getStringAttribute("nodeSid");
344 if (mplsLabelStr != null)
345 mplsLabel = Integer.parseInt(mplsLabelStr);
346 }
347 }
348 } catch (JSONException e) {
349 // TODO Auto-generated catch block
350 e.printStackTrace();
351 }
352 }
353
354 return mplsLabel;
355 }
356
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700357 /**
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700358 * Get Router MAC Address for the target address from the network config
359 * file
Sangho Shinfbc572c2014-10-02 16:37:05 -0700360 *
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700361 * @param targetAddress - IP address of the target host
362 * @return Router MAC of the switch to send packets to the target address
363 */
364 private String getRouterMACFromConfig(IPv4Address targetAddress) {
365
366 String routerMac = null;
367
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700368 for (Switch sw : mutableTopology.getSwitches()) {
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700369
370 String subnets = sw.getStringAttribute("subnets");
371 try {
372 JSONArray arry = new JSONArray(subnets);
373 for (int i = 0; i < arry.length(); i++) {
374 String subnetIp = (String) arry.getJSONObject(i).get("subnetIp");
375 if (srManager.netMatch(subnetIp, targetAddress.toString())) {
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700376 routerMac = sw.getStringAttribute("routerMac");
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700377 }
378 }
379 } catch (JSONException e) {
380 // TODO Auto-generated catch block
381 e.printStackTrace();
382 }
383 }
384
385 return routerMac;
386 }
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700387
388 /**
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700389 * Add a new rule to VLAN table to forward packets from any port to the next
390 * table It is required to forward packets from controller to pipeline
Sangho Shinfbc572c2014-10-02 16:37:05 -0700391 *
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700392 * @param sw Switch the packet came from
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700393 */
394 private void addControlPortInVlanTable(Switch sw) {
395
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700396 IOFSwitch ofSwitch = floodlightProvider.getMasterSwitch(sw.getDpid().value());
397 OFFactory factory = ofSwitch.getFactory();
398
Sangho Shin1aa93542014-09-22 09:49:44 -0700399 OFOxmInPort oxp = factory.oxms().inPort(OFPort.CONTROLLER);
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700400 OFOxmVlanVid oxv = factory.oxms()
401 .vlanVid(OFVlanVidMatch.UNTAGGED);
402 OFOxmList oxmList = OFOxmList.of(oxv);
403
Sangho Shin1aa93542014-09-22 09:49:44 -0700404 /* Cqpd switch does not seems to support CONTROLLER port as in_port match rule */
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700405 // OFOxmList oxmList = OFOxmList.of(oxp, oxv);
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700406
407 OFMatchV3 match = factory.buildMatchV3()
408 .setOxmList(oxmList)
409 .build();
410
411 OFInstruction gotoTbl = factory.instructions().buildGotoTable()
412 .setTableId(TableId.of(TABLE_TMAC)).build();
413 List<OFInstruction> instructions = new ArrayList<OFInstruction>();
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700414 instructions.add(gotoTbl);
415 OFMessage flowEntry = factory.buildFlowAdd()
416 .setTableId(TableId.of(TABLE_VLAN))
417 .setMatch(match)
418 .setInstructions(instructions)
419 .setPriority(1000) // does not matter - all rules
420 // exclusive
421 .setBufferId(OFBufferId.NO_BUFFER)
422 .setIdleTimeout(0)
423 .setHardTimeout(0)
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700424 // .setXid(getNextTransactionId())
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700425 .build();
426
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700427 flowPusher.add(sw.getDpid(), flowEntry);
Sangho Shin1aa93542014-09-22 09:49:44 -0700428 log.debug("Adding a new vlan-rules in sw {}", sw.getDpid());
Sangho Shin2f263692014-09-15 14:09:41 -0700429
430 }
431
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700432 /**
433 * Check if the source IP and destination IP are in the same subnet
Sangho Shinfbc572c2014-10-02 16:37:05 -0700434 *
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700435 * @param sw Switch
436 * @param ipv4 IP address to check
437 * @return return true if the IP packet is within the same subnet
438 */
439 private boolean inSameSubnet(Switch sw, IPv4 ipv4) {
440
441 String gwIpSrc = getGwIpForSubnet(ipv4.getSourceAddress());
442 String gwIpDest = getGwIpForSubnet(ipv4.getDestinationAddress());
443
444 if (gwIpSrc.equals(gwIpDest)) {
445 return true;
446 }
447 else
448 return false;
449 }
450
451 /**
452 * Get router IP address for the given IP address
Sangho Shinfbc572c2014-10-02 16:37:05 -0700453 *
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700454 * @param sourceAddress
455 * @return
456 */
457 private String getGwIpForSubnet(int sourceAddress) {
458
459 String gwIp = null;
460 IPv4Address srcIp = IPv4Address.of(sourceAddress);
461
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700462 for (Switch sw : mutableTopology.getSwitches()) {
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700463
464 String subnets = sw.getStringAttribute("subnets");
465 try {
466 JSONArray arry = new JSONArray(subnets);
467 for (int i = 0; i < arry.length(); i++) {
468 String subnetIpSlash = (String) arry.getJSONObject(i).get("subnetIp");
469 if (srManager.netMatch(subnetIpSlash, srcIp.toString())) {
470 gwIp = subnetIpSlash;
471 }
472 }
473 } catch (JSONException e) {
474 // TODO Auto-generated catch block
475 e.printStackTrace();
476 }
477 }
478
479 return gwIp;
480 }
481
Sangho Shin2f263692014-09-15 14:09:41 -0700482}