blob: e32d7c1226d4adc51998833ebb5604390b130b1e [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)) {
Sangho Shin99918bd2014-10-08 15:52:35 -0700153 ipPacket.setTtl((byte)(ipPacket.getTtl()-1));
154 ipPacket.setChecksum((short)0);
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700155 Ethernet eth = new Ethernet();
Sangho Shinfbc572c2014-10-02 16:37:05 -0700156 eth.setDestinationMACAddress(destinationMacAddress);
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700157 eth.setSourceMACAddress(sw
158 .getStringAttribute("routerMac"));
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700159 eth.setEtherType(Ethernet.TYPE_IPV4);
160 eth.setPayload(ipPacket);
Sangho Shinfbc572c2014-10-02 16:37:05 -0700161 for (Port port: host.getAttachmentPoints())
162 sendPacketOut(sw, eth, new SwitchPort(
163 port.getDpid(),port.getNumber()), false);
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700164 }
165 }
166
Srikanth Vavilapalli5ddf8f02014-09-24 11:02:28 -0700167 return;
168 }
169 }
170 /* ICMP for an unknown host */
Sangho Shindafae192014-10-07 17:06:48 -0700171 log.debug("ICMPHandler: ICMP request for unknown host {}",
172 destinationAddress);
Sangho Shin463bee52014-09-29 15:14:43 -0700173 srManager.sendArpRequest(sw, destinationAddress.getInt(), inPort);
Sangho Shin2f263692014-09-15 14:09:41 -0700174 }
175
176 }
Sangho Shineb083032014-09-22 16:11:34 -0700177 }
Sangho Shin2f263692014-09-15 14:09:41 -0700178
Sangho Shineb083032014-09-22 16:11:34 -0700179 /**
180 * Retrieve Gateway IP address of all subnets defined in net config file
Sangho Shinfbc572c2014-10-02 16:37:05 -0700181 *
Sangho Shineb083032014-09-22 16:11:34 -0700182 * @param sw Switch to retrieve subnet GW IPs for
183 * @return list of GW IP addresses for all subnets
184 */
185 private List<String> getSubnetGatewayIps(Switch sw) {
186
187 List<String> gatewayIps = new ArrayList<String>();
188
189 String subnets = sw.getStringAttribute("subnets");
190 try {
191 JSONArray arry = new JSONArray(subnets);
192 for (int i = 0; i < arry.length(); i++) {
193 String subnetIpSlash = (String) arry.getJSONObject(i).get("subnetIp");
194 if (subnetIpSlash != null) {
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700195 String subnetIp = subnetIpSlash.substring(0,
196 subnetIpSlash.indexOf('/'));
Sangho Shineb083032014-09-22 16:11:34 -0700197 gatewayIps.add(subnetIp);
198 }
199 }
200 } catch (JSONException e) {
201 // TODO Auto-generated catch block
202 e.printStackTrace();
203 }
204
205 return gatewayIps;
Sangho Shin2f263692014-09-15 14:09:41 -0700206 }
207
Sangho Shin79c8d452014-09-18 09:50:21 -0700208 /**
209 * Send ICMP reply back
Sangho Shinfbc572c2014-10-02 16:37:05 -0700210 *
Sangho Shin79c8d452014-09-18 09:50:21 -0700211 * @param sw Switch
212 * @param inPort Port the ICMP packet is forwarded from
213 * @param icmpRequest the ICMP request to handle
214 * @param destinationAddress destination address to send ICMP response to
215 */
216 private void sendICMPResponse(Switch sw, Port inPort, Ethernet icmpRequest) {
217
218 Ethernet icmpReplyEth = new Ethernet();
219
220 IPv4 icmpRequestIpv4 = (IPv4) icmpRequest.getPayload();
221 IPv4 icmpReplyIpv4 = new IPv4();
222 int destAddress = icmpRequestIpv4.getDestinationAddress();
223 icmpReplyIpv4.setDestinationAddress(icmpRequestIpv4.getSourceAddress());
224 icmpReplyIpv4.setSourceAddress(destAddress);
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700225 icmpReplyIpv4.setTtl((byte) 64);
226 icmpReplyIpv4.setChecksum((short) 0);
Sangho Shin79c8d452014-09-18 09:50:21 -0700227
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700228 ICMP icmpReply = (ICMP) icmpRequestIpv4.getPayload().clone();
229 icmpReply.setIcmpCode((byte) 0x00);
Sangho Shin1aa93542014-09-22 09:49:44 -0700230 icmpReply.setIcmpType((byte) ICMP_TYPE_REPLY);
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700231 icmpReply.setChecksum((short) 0);
Sangho Shin79c8d452014-09-18 09:50:21 -0700232
233 icmpReplyIpv4.setPayload(icmpReply);
234
235 icmpReplyEth.setPayload(icmpReplyIpv4);
236 icmpReplyEth.setEtherType(Ethernet.TYPE_IPV4);
237 icmpReplyEth.setDestinationMACAddress(icmpRequest.getSourceMACAddress());
238 icmpReplyEth.setSourceMACAddress(icmpRequest.getDestinationMACAddress());
239
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700240 sendPacketOut(sw, icmpReplyEth,
241 new SwitchPort(sw.getDpid(), inPort.getPortNumber()), false);
Sangho Shin79c8d452014-09-18 09:50:21 -0700242
243 log.debug("Send an ICMP response {}", icmpReplyIpv4.toString());
244
245 }
246
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700247 /**
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700248 * Send PACKET_OUT message with actions If switches support OFPP_TABLE
249 * action, it sends out packet to TABLE port Otherwise, it sends the packet
250 * to the port the packet came from (in this case, MPLS label is added if
251 * the packet needs go through transit switches)
Sangho Shinfbc572c2014-10-02 16:37:05 -0700252 *
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700253 * @param sw Switch the packet came from
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700254 * @param packet Ethernet packet to send
255 * @param switchPort port to send the packet
256 */
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700257 private void sendPacketOut(Switch sw, Ethernet packet, SwitchPort switchPort,
258 boolean supportOfppTable) {
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700259
260 boolean sameSubnet = false;
261 IOFSwitch ofSwitch = floodlightProvider.getMasterSwitch(sw.getDpid().value());
262 OFFactory factory = ofSwitch.getFactory();
263
264 List<OFAction> actions = new ArrayList<>();
265
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700266 // If OFPP_TABLE action is not supported in the switch, MPLS label needs
267 // to be set
Sangho Shin1aa93542014-09-22 09:49:44 -0700268 // if the packet needs to be delivered crossing switches
269 if (!supportOfppTable) {
270 // Check if the destination is the host attached to the switch
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700271 int destinationAddress = ((IPv4) packet.getPayload()).getDestinationAddress();
272 for (net.onrc.onos.core.topology.Host host : mutableTopology
273 .getHosts(switchPort)) {
Sangho Shin1aa93542014-09-22 09:49:44 -0700274 IPv4Address hostIpAddress = IPv4Address.of(host.getIpAddress());
275 if (hostIpAddress != null && hostIpAddress.getInt() == destinationAddress) {
276 sameSubnet = true;
277 break;
278 }
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700279 }
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700280
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700281 IPv4Address targetAddress = IPv4Address.of(((IPv4) packet.getPayload())
282 .getDestinationAddress());
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700283 String destMacAddress = packet.getDestinationMAC().toString();
284 // If the destination host is not attached in the switch
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700285 // and the destination is not the neighbor switch, then add MPLS
286 // label
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700287 String targetMac = getRouterMACFromConfig(targetAddress);
Sangho Shindafae192014-10-07 17:06:48 -0700288 if (targetMac == null) {
289 return; // targetMac should be always available
290 }
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700291 if (!sameSubnet && !targetMac.equals(destMacAddress)) {
Sangho Shin1aa93542014-09-22 09:49:44 -0700292 int mplsLabel = getMplsLabelFromConfig(targetAddress);
293 if (mplsLabel > 0) {
294 OFAction pushlabel = factory.actions().pushMpls(EthType.MPLS_UNICAST);
295 OFOxmMplsLabel l = factory.oxms()
296 .mplsLabel(U32.of(mplsLabel));
297 OFAction setlabelid = factory.actions().buildSetField()
298 .setField(l).build();
299 OFAction copyTtlOut = factory.actions().copyTtlOut();
300 actions.add(pushlabel);
301 actions.add(setlabelid);
302 actions.add(copyTtlOut);
303 }
304 }
305
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700306 OFAction outport = factory.actions().output(
307 OFPort.of(switchPort.getPortNumber().shortValue()), Short.MAX_VALUE);
Sangho Shin1aa93542014-09-22 09:49:44 -0700308 actions.add(outport);
309 }
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700310 // If OFPP_TABLE action is supported, first set a rule to allow packet
311 // from CONTROLLER port.
Sangho Shin1aa93542014-09-22 09:49:44 -0700312 // Then, send the packet to the table port
313 else {
314 if (!controllerPortAllowed) {
315 addControlPortInVlanTable(sw);
316 controllerPortAllowed = true;
317 }
318 OFAction outport = factory.actions().output(OFPort.TABLE, Short.MAX_VALUE);
319 actions.add(outport);
320 }
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700321
322 OFPacketOut po = factory.buildPacketOut()
323 .setData(packet.serialize())
324 .setActions(actions)
325 .build();
326
327 flowPusher.add(sw.getDpid(), po);
328 }
329
Sangho Shin2f263692014-09-15 14:09:41 -0700330 /**
Sangho Shin1aa93542014-09-22 09:49:44 -0700331 * Get MPLS label for the target address from the network config file
Sangho Shinfbc572c2014-10-02 16:37:05 -0700332 *
Sangho Shin1aa93542014-09-22 09:49:44 -0700333 * @param targetAddress - IP address of the target host
334 * @return MPLS label of the switch to send packets to the target address
335 */
336 private int getMplsLabelFromConfig(IPv4Address targetAddress) {
337
338 int mplsLabel = -1;
339
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700340 for (Switch sw : mutableTopology.getSwitches()) {
Sangho Shin1aa93542014-09-22 09:49:44 -0700341
342 String subnets = sw.getStringAttribute("subnets");
343 try {
344 JSONArray arry = new JSONArray(subnets);
345 for (int i = 0; i < arry.length(); i++) {
346 String subnetIp = (String) arry.getJSONObject(i).get("subnetIp");
347 if (srManager.netMatch(subnetIp, targetAddress.toString())) {
348 String mplsLabelStr = sw.getStringAttribute("nodeSid");
349 if (mplsLabelStr != null)
350 mplsLabel = Integer.parseInt(mplsLabelStr);
351 }
352 }
353 } catch (JSONException e) {
354 // TODO Auto-generated catch block
355 e.printStackTrace();
356 }
357 }
358
359 return mplsLabel;
360 }
361
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700362 /**
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700363 * Get Router MAC Address for the target address from the network config
364 * file
Sangho Shinfbc572c2014-10-02 16:37:05 -0700365 *
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700366 * @param targetAddress - IP address of the target host
367 * @return Router MAC of the switch to send packets to the target address
368 */
369 private String getRouterMACFromConfig(IPv4Address targetAddress) {
370
371 String routerMac = null;
372
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700373 for (Switch sw : mutableTopology.getSwitches()) {
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700374
375 String subnets = sw.getStringAttribute("subnets");
376 try {
377 JSONArray arry = new JSONArray(subnets);
378 for (int i = 0; i < arry.length(); i++) {
379 String subnetIp = (String) arry.getJSONObject(i).get("subnetIp");
380 if (srManager.netMatch(subnetIp, targetAddress.toString())) {
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700381 routerMac = sw.getStringAttribute("routerMac");
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700382 }
383 }
384 } catch (JSONException e) {
385 // TODO Auto-generated catch block
386 e.printStackTrace();
387 }
388 }
389
390 return routerMac;
391 }
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700392
393 /**
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700394 * Add a new rule to VLAN table to forward packets from any port to the next
395 * table It is required to forward packets from controller to pipeline
Sangho Shinfbc572c2014-10-02 16:37:05 -0700396 *
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700397 * @param sw Switch the packet came from
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700398 */
399 private void addControlPortInVlanTable(Switch sw) {
400
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700401 IOFSwitch ofSwitch = floodlightProvider.getMasterSwitch(sw.getDpid().value());
402 OFFactory factory = ofSwitch.getFactory();
403
Sangho Shin1aa93542014-09-22 09:49:44 -0700404 OFOxmInPort oxp = factory.oxms().inPort(OFPort.CONTROLLER);
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700405 OFOxmVlanVid oxv = factory.oxms()
406 .vlanVid(OFVlanVidMatch.UNTAGGED);
407 OFOxmList oxmList = OFOxmList.of(oxv);
408
Sangho Shin1aa93542014-09-22 09:49:44 -0700409 /* Cqpd switch does not seems to support CONTROLLER port as in_port match rule */
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700410 // OFOxmList oxmList = OFOxmList.of(oxp, oxv);
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700411
412 OFMatchV3 match = factory.buildMatchV3()
413 .setOxmList(oxmList)
414 .build();
415
416 OFInstruction gotoTbl = factory.instructions().buildGotoTable()
417 .setTableId(TableId.of(TABLE_TMAC)).build();
418 List<OFInstruction> instructions = new ArrayList<OFInstruction>();
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700419 instructions.add(gotoTbl);
420 OFMessage flowEntry = factory.buildFlowAdd()
421 .setTableId(TableId.of(TABLE_VLAN))
422 .setMatch(match)
423 .setInstructions(instructions)
424 .setPriority(1000) // does not matter - all rules
425 // exclusive
426 .setBufferId(OFBufferId.NO_BUFFER)
427 .setIdleTimeout(0)
428 .setHardTimeout(0)
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700429 // .setXid(getNextTransactionId())
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700430 .build();
431
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700432 flowPusher.add(sw.getDpid(), flowEntry);
Sangho Shin1aa93542014-09-22 09:49:44 -0700433 log.debug("Adding a new vlan-rules in sw {}", sw.getDpid());
Sangho Shin2f263692014-09-15 14:09:41 -0700434
435 }
436
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700437 /**
438 * Check if the source IP and destination IP are in the same subnet
Sangho Shinfbc572c2014-10-02 16:37:05 -0700439 *
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700440 * @param sw Switch
441 * @param ipv4 IP address to check
442 * @return return true if the IP packet is within the same subnet
443 */
444 private boolean inSameSubnet(Switch sw, IPv4 ipv4) {
445
446 String gwIpSrc = getGwIpForSubnet(ipv4.getSourceAddress());
447 String gwIpDest = getGwIpForSubnet(ipv4.getDestinationAddress());
448
449 if (gwIpSrc.equals(gwIpDest)) {
450 return true;
451 }
452 else
453 return false;
454 }
455
456 /**
457 * Get router IP address for the given IP address
Sangho Shinfbc572c2014-10-02 16:37:05 -0700458 *
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700459 * @param sourceAddress
460 * @return
461 */
462 private String getGwIpForSubnet(int sourceAddress) {
463
464 String gwIp = null;
465 IPv4Address srcIp = IPv4Address.of(sourceAddress);
466
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700467 for (Switch sw : mutableTopology.getSwitches()) {
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700468
469 String subnets = sw.getStringAttribute("subnets");
470 try {
471 JSONArray arry = new JSONArray(subnets);
472 for (int i = 0; i < arry.length(); i++) {
473 String subnetIpSlash = (String) arry.getJSONObject(i).get("subnetIp");
474 if (srManager.netMatch(subnetIpSlash, srcIp.toString())) {
475 gwIp = subnetIpSlash;
476 }
477 }
478 } catch (JSONException e) {
479 // TODO Auto-generated catch block
480 e.printStackTrace();
481 }
482 }
483
484 return gwIp;
485 }
486
Sangho Shin2f263692014-09-15 14:09:41 -0700487}