blob: bca4bd502d5cf8be85cc7c77328b1e557b5ce29c [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
Sangho Shindafae192014-10-07 17:06:48 -0700108 log.debug("Received an ICMP packet from sw {} ",
109 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()))) {
Sangho Shindafae192014-10-07 17:06:48 -0700123 log.debug("ICMP packet for sw {} and "
Srikanth Vavilapalli5ddf8f02014-09-24 11:02:28 -0700124 + "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 */
Sangho Shindafae192014-10-07 17:06:48 -0700169 log.debug("ICMPHandler: ICMP request for unknown host {}",
170 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);
Sangho Shindafae192014-10-07 17:06:48 -0700286 if (targetMac == null) {
287 return; // targetMac should be always available
288 }
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700289 if (!sameSubnet && !targetMac.equals(destMacAddress)) {
Sangho Shin1aa93542014-09-22 09:49:44 -0700290 int mplsLabel = getMplsLabelFromConfig(targetAddress);
291 if (mplsLabel > 0) {
292 OFAction pushlabel = factory.actions().pushMpls(EthType.MPLS_UNICAST);
293 OFOxmMplsLabel l = factory.oxms()
294 .mplsLabel(U32.of(mplsLabel));
295 OFAction setlabelid = factory.actions().buildSetField()
296 .setField(l).build();
297 OFAction copyTtlOut = factory.actions().copyTtlOut();
298 actions.add(pushlabel);
299 actions.add(setlabelid);
300 actions.add(copyTtlOut);
301 }
302 }
303
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700304 OFAction outport = factory.actions().output(
305 OFPort.of(switchPort.getPortNumber().shortValue()), Short.MAX_VALUE);
Sangho Shin1aa93542014-09-22 09:49:44 -0700306 actions.add(outport);
307 }
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700308 // If OFPP_TABLE action is supported, first set a rule to allow packet
309 // from CONTROLLER port.
Sangho Shin1aa93542014-09-22 09:49:44 -0700310 // Then, send the packet to the table port
311 else {
312 if (!controllerPortAllowed) {
313 addControlPortInVlanTable(sw);
314 controllerPortAllowed = true;
315 }
316 OFAction outport = factory.actions().output(OFPort.TABLE, Short.MAX_VALUE);
317 actions.add(outport);
318 }
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700319
320 OFPacketOut po = factory.buildPacketOut()
321 .setData(packet.serialize())
322 .setActions(actions)
323 .build();
324
325 flowPusher.add(sw.getDpid(), po);
326 }
327
Sangho Shin2f263692014-09-15 14:09:41 -0700328 /**
Sangho Shin1aa93542014-09-22 09:49:44 -0700329 * Get MPLS label for the target address from the network config file
Sangho Shinfbc572c2014-10-02 16:37:05 -0700330 *
Sangho Shin1aa93542014-09-22 09:49:44 -0700331 * @param targetAddress - IP address of the target host
332 * @return MPLS label of the switch to send packets to the target address
333 */
334 private int getMplsLabelFromConfig(IPv4Address targetAddress) {
335
336 int mplsLabel = -1;
337
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700338 for (Switch sw : mutableTopology.getSwitches()) {
Sangho Shin1aa93542014-09-22 09:49:44 -0700339
340 String subnets = sw.getStringAttribute("subnets");
341 try {
342 JSONArray arry = new JSONArray(subnets);
343 for (int i = 0; i < arry.length(); i++) {
344 String subnetIp = (String) arry.getJSONObject(i).get("subnetIp");
345 if (srManager.netMatch(subnetIp, targetAddress.toString())) {
346 String mplsLabelStr = sw.getStringAttribute("nodeSid");
347 if (mplsLabelStr != null)
348 mplsLabel = Integer.parseInt(mplsLabelStr);
349 }
350 }
351 } catch (JSONException e) {
352 // TODO Auto-generated catch block
353 e.printStackTrace();
354 }
355 }
356
357 return mplsLabel;
358 }
359
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700360 /**
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700361 * Get Router MAC Address for the target address from the network config
362 * file
Sangho Shinfbc572c2014-10-02 16:37:05 -0700363 *
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700364 * @param targetAddress - IP address of the target host
365 * @return Router MAC of the switch to send packets to the target address
366 */
367 private String getRouterMACFromConfig(IPv4Address targetAddress) {
368
369 String routerMac = null;
370
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700371 for (Switch sw : mutableTopology.getSwitches()) {
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700372
373 String subnets = sw.getStringAttribute("subnets");
374 try {
375 JSONArray arry = new JSONArray(subnets);
376 for (int i = 0; i < arry.length(); i++) {
377 String subnetIp = (String) arry.getJSONObject(i).get("subnetIp");
378 if (srManager.netMatch(subnetIp, targetAddress.toString())) {
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700379 routerMac = sw.getStringAttribute("routerMac");
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700380 }
381 }
382 } catch (JSONException e) {
383 // TODO Auto-generated catch block
384 e.printStackTrace();
385 }
386 }
387
388 return routerMac;
389 }
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700390
391 /**
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700392 * Add a new rule to VLAN table to forward packets from any port to the next
393 * table It is required to forward packets from controller to pipeline
Sangho Shinfbc572c2014-10-02 16:37:05 -0700394 *
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700395 * @param sw Switch the packet came from
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700396 */
397 private void addControlPortInVlanTable(Switch sw) {
398
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700399 IOFSwitch ofSwitch = floodlightProvider.getMasterSwitch(sw.getDpid().value());
400 OFFactory factory = ofSwitch.getFactory();
401
Sangho Shin1aa93542014-09-22 09:49:44 -0700402 OFOxmInPort oxp = factory.oxms().inPort(OFPort.CONTROLLER);
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700403 OFOxmVlanVid oxv = factory.oxms()
404 .vlanVid(OFVlanVidMatch.UNTAGGED);
405 OFOxmList oxmList = OFOxmList.of(oxv);
406
Sangho Shin1aa93542014-09-22 09:49:44 -0700407 /* Cqpd switch does not seems to support CONTROLLER port as in_port match rule */
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700408 // OFOxmList oxmList = OFOxmList.of(oxp, oxv);
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700409
410 OFMatchV3 match = factory.buildMatchV3()
411 .setOxmList(oxmList)
412 .build();
413
414 OFInstruction gotoTbl = factory.instructions().buildGotoTable()
415 .setTableId(TableId.of(TABLE_TMAC)).build();
416 List<OFInstruction> instructions = new ArrayList<OFInstruction>();
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700417 instructions.add(gotoTbl);
418 OFMessage flowEntry = factory.buildFlowAdd()
419 .setTableId(TableId.of(TABLE_VLAN))
420 .setMatch(match)
421 .setInstructions(instructions)
422 .setPriority(1000) // does not matter - all rules
423 // exclusive
424 .setBufferId(OFBufferId.NO_BUFFER)
425 .setIdleTimeout(0)
426 .setHardTimeout(0)
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700427 // .setXid(getNextTransactionId())
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700428 .build();
429
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700430 flowPusher.add(sw.getDpid(), flowEntry);
Sangho Shin1aa93542014-09-22 09:49:44 -0700431 log.debug("Adding a new vlan-rules in sw {}", sw.getDpid());
Sangho Shin2f263692014-09-15 14:09:41 -0700432
433 }
434
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700435 /**
436 * Check if the source IP and destination IP are in the same subnet
Sangho Shinfbc572c2014-10-02 16:37:05 -0700437 *
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700438 * @param sw Switch
439 * @param ipv4 IP address to check
440 * @return return true if the IP packet is within the same subnet
441 */
442 private boolean inSameSubnet(Switch sw, IPv4 ipv4) {
443
444 String gwIpSrc = getGwIpForSubnet(ipv4.getSourceAddress());
445 String gwIpDest = getGwIpForSubnet(ipv4.getDestinationAddress());
446
447 if (gwIpSrc.equals(gwIpDest)) {
448 return true;
449 }
450 else
451 return false;
452 }
453
454 /**
455 * Get router IP address for the given IP address
Sangho Shinfbc572c2014-10-02 16:37:05 -0700456 *
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700457 * @param sourceAddress
458 * @return
459 */
460 private String getGwIpForSubnet(int sourceAddress) {
461
462 String gwIp = null;
463 IPv4Address srcIp = IPv4Address.of(sourceAddress);
464
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700465 for (Switch sw : mutableTopology.getSwitches()) {
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700466
467 String subnets = sw.getStringAttribute("subnets");
468 try {
469 JSONArray arry = new JSONArray(subnets);
470 for (int i = 0; i < arry.length(); i++) {
471 String subnetIpSlash = (String) arry.getJSONObject(i).get("subnetIp");
472 if (srManager.netMatch(subnetIpSlash, srcIp.toString())) {
473 gwIp = subnetIpSlash;
474 }
475 }
476 } catch (JSONException e) {
477 // TODO Auto-generated catch block
478 e.printStackTrace();
479 }
480 }
481
482 return gwIp;
483 }
484
Sangho Shin2f263692014-09-15 14:09:41 -0700485}