blob: 21b197bf654d49a951120b1fa0b65af4756bddd3 [file] [log] [blame]
Sangho Shin2f263692014-09-15 14:09:41 -07001package net.onrc.onos.apps.segmentrouting;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import net.floodlightcontroller.core.IFloodlightProviderService;
7import net.floodlightcontroller.core.IOFSwitch;
8import net.floodlightcontroller.core.module.FloodlightModuleContext;
Sangho Shin2f263692014-09-15 14:09:41 -07009import net.onrc.onos.core.flowprogrammer.IFlowPusherService;
10import net.onrc.onos.core.packet.Ethernet;
Sangho Shin79c8d452014-09-18 09:50:21 -070011import net.onrc.onos.core.packet.ICMP;
Sangho Shin2f263692014-09-15 14:09:41 -070012import net.onrc.onos.core.packet.IPv4;
Srikanth Vavilapalli5ddf8f02014-09-24 11:02:28 -070013import net.onrc.onos.core.topology.Host;
Sangho Shin2f263692014-09-15 14:09:41 -070014import net.onrc.onos.core.topology.ITopologyService;
15import net.onrc.onos.core.topology.MutableTopology;
16import net.onrc.onos.core.topology.Port;
17import net.onrc.onos.core.topology.Switch;
Sangho Shin79c8d452014-09-18 09:50:21 -070018import net.onrc.onos.core.util.SwitchPort;
Sangho Shin2f263692014-09-15 14:09:41 -070019
Sangho Shin1aa93542014-09-22 09:49:44 -070020import org.json.JSONArray;
21import org.json.JSONException;
Sangho Shin2f263692014-09-15 14:09:41 -070022import org.projectfloodlight.openflow.protocol.OFFactory;
23import org.projectfloodlight.openflow.protocol.OFMatchV3;
24import org.projectfloodlight.openflow.protocol.OFMessage;
25import org.projectfloodlight.openflow.protocol.OFOxmList;
Sangho Shinf41ac0a2014-09-19 09:50:30 -070026import org.projectfloodlight.openflow.protocol.OFPacketOut;
Sangho Shin2f263692014-09-15 14:09:41 -070027import org.projectfloodlight.openflow.protocol.action.OFAction;
28import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
Sangho Shin1aa93542014-09-22 09:49:44 -070029import org.projectfloodlight.openflow.protocol.oxm.OFOxmInPort;
Sangho Shinf41ac0a2014-09-19 09:50:30 -070030import org.projectfloodlight.openflow.protocol.oxm.OFOxmMplsLabel;
31import org.projectfloodlight.openflow.protocol.oxm.OFOxmVlanVid;
Sangho Shin2f263692014-09-15 14:09:41 -070032import org.projectfloodlight.openflow.types.EthType;
33import org.projectfloodlight.openflow.types.IPv4Address;
Sangho Shin2f263692014-09-15 14:09:41 -070034import org.projectfloodlight.openflow.types.OFBufferId;
35import org.projectfloodlight.openflow.types.OFPort;
Sangho Shinf41ac0a2014-09-19 09:50:30 -070036import org.projectfloodlight.openflow.types.OFVlanVidMatch;
Sangho Shin2f263692014-09-15 14:09:41 -070037import org.projectfloodlight.openflow.types.TableId;
Sangho Shinf41ac0a2014-09-19 09:50:30 -070038import org.projectfloodlight.openflow.types.U32;
Sangho Shin2f263692014-09-15 14:09:41 -070039import org.slf4j.Logger;
40import org.slf4j.LoggerFactory;
41
Srikanth Vavilapallib1fce732014-09-24 14:09:50 -070042public class IcmpHandler {
Sangho Shin2f263692014-09-15 14:09:41 -070043
44 private SegmentRoutingManager srManager;
45 private IFloodlightProviderService floodlightProvider;
46 private MutableTopology mutableTopology;
Sangho Shin2f263692014-09-15 14:09:41 -070047 private ITopologyService topologyService;
48 private static final Logger log = LoggerFactory
Sangho Shinf41ac0a2014-09-19 09:50:30 -070049 .getLogger(IcmpHandler.class);
Sangho Shin2f263692014-09-15 14:09:41 -070050
51 private IFlowPusherService flowPusher;
Sangho Shin1aa93542014-09-22 09:49:44 -070052 private boolean controllerPortAllowed = false;
Sangho Shin2f263692014-09-15 14:09:41 -070053
54 private static final int TABLE_VLAN = 0;
55 private static final int TABLE_TMAC = 1;
56 private static final int TABLE_IPv4_UNICAST = 2;
57 private static final int TABLE_MPLS = 3;
58 private static final int TABLE_META = 4;
59 private static final int TABLE_ACL = 5;
60
61 private static final short MAX_PRIORITY = (short) 0xffff;
62 private static final short SLASH_24_PRIORITY = (short) 0xfff0;
63 private static final short SLASH_16_PRIORITY = (short) 0xff00;
64 private static final short SLASH_8_PRIORITY = (short) 0xf000;
65 private static final short MIN_PRIORITY = 0x0;
66
Sangho Shin1aa93542014-09-22 09:49:44 -070067 private static final int ICMP_TYPE_ECHO = 0x08;
68 private static final int ICMP_TYPE_REPLY = 0x00;
69
Sangho Shin2f263692014-09-15 14:09:41 -070070
71 public IcmpHandler(FloodlightModuleContext context, SegmentRoutingManager manager) {
72
73 this.floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
74 this.flowPusher = context.getServiceImpl(IFlowPusherService.class);
Sangho Shin2f263692014-09-15 14:09:41 -070075 this.topologyService = context.getServiceImpl(ITopologyService.class);
76 this.mutableTopology = topologyService.getTopology();
77
78 this.srManager = manager;
Sangho Shin2f263692014-09-15 14:09:41 -070079 }
80
Srikanth Vavilapallib1fce732014-09-24 14:09:50 -070081 public void processPacketIn(Switch sw, Port inPort, Ethernet payload) {
Sangho Shin2f263692014-09-15 14:09:41 -070082
83 if (payload.getEtherType() == Ethernet.TYPE_IPV4) {
84
85 IPv4 ipv4 = (IPv4)payload.getPayload();
Sangho Shin79c8d452014-09-18 09:50:21 -070086
Sangho Shin2f263692014-09-15 14:09:41 -070087 if (ipv4.getProtocol() == IPv4.PROTOCOL_ICMP) {
Srikanth Vavilapalli5ddf8f02014-09-24 11:02:28 -070088
89 log.debug("ICMPHandler: Received a ICMP packet {} from sw {} ",
90 payload.toString(), sw.getDpid());
Sangho Shin79c8d452014-09-18 09:50:21 -070091 int destinationAddress = ipv4.getDestinationAddress();
Sangho Shineb083032014-09-22 16:11:34 -070092 String destAddressStr = IPv4Address.of(destinationAddress).toString();
Sangho Shin2f263692014-09-15 14:09:41 -070093
Sangho Shin79c8d452014-09-18 09:50:21 -070094 // Check if it is ICMP request to the switch
95 String switchIpAddressSlash = sw.getStringAttribute("routerIp");
96 if (switchIpAddressSlash != null) {
Sangho Shineb083032014-09-22 16:11:34 -070097 String switchIpAddressStr
98 = switchIpAddressSlash.substring(0, switchIpAddressSlash.indexOf('/'));
Sangho Shin79c8d452014-09-18 09:50:21 -070099 IPv4Address switchIpAddress = IPv4Address.of(switchIpAddressStr);
Sangho Shineb083032014-09-22 16:11:34 -0700100 List<String> gatewayIps = getSubnetGatewayIps(sw);
Sangho Shin1aa93542014-09-22 09:49:44 -0700101 if (((ICMP)ipv4.getPayload()).getIcmpType() == ICMP_TYPE_ECHO &&
Sangho Shineb083032014-09-22 16:11:34 -0700102 (destinationAddress == switchIpAddress.getInt() ||
103 gatewayIps.contains(destAddressStr))) {
Srikanth Vavilapalli5ddf8f02014-09-24 11:02:28 -0700104 log.debug("ICMPHandler: ICMP packet for sw {} and "
105 + "sending ICMP response ", sw.getDpid());
Sangho Shin79c8d452014-09-18 09:50:21 -0700106 sendICMPResponse(sw, inPort, payload);
107 return;
108 }
109 }
Srikanth Vavilapalli5ddf8f02014-09-24 11:02:28 -0700110
111 /* Check if ICMP is for any switch known host */
112 for (Host host: sw.getHosts()) {
113 IPv4Address hostIpAddress =
114 IPv4Address.of(host.getIpAddress());
115 if (hostIpAddress != null &&
116 hostIpAddress.equals(destinationAddress)) {
117 /* TODO: We should not have come here as ARP itself
118 * would have installed a Route to the host. See if
119 * we can remove this code
120 */
121 log.debug("ICMPHandler: ICMP request for known host {}",
122 hostIpAddress);
123 byte[] destinationMacAddress = host.getMacAddress().toBytes();
124 srManager.addRouteToHost(sw,
125 destinationAddress, destinationMacAddress);
126 return;
127 }
128 }
129 /* ICMP for an unknown host */
130 log.debug("ICMPHandler: ICMP request for unknown host {}"
131 + " and sending ARP request", destinationAddress);
132 srManager.sendArpRequest(sw, destinationAddress, inPort);
Sangho Shin2f263692014-09-15 14:09:41 -0700133 }
134
135 }
Sangho Shineb083032014-09-22 16:11:34 -0700136 }
Sangho Shin2f263692014-09-15 14:09:41 -0700137
Sangho Shineb083032014-09-22 16:11:34 -0700138 /**
139 * Retrieve Gateway IP address of all subnets defined in net config file
140 *
141 * @param sw Switch to retrieve subnet GW IPs for
142 * @return list of GW IP addresses for all subnets
143 */
144 private List<String> getSubnetGatewayIps(Switch sw) {
145
146 List<String> gatewayIps = new ArrayList<String>();
147
148 String subnets = sw.getStringAttribute("subnets");
149 try {
150 JSONArray arry = new JSONArray(subnets);
151 for (int i = 0; i < arry.length(); i++) {
152 String subnetIpSlash = (String) arry.getJSONObject(i).get("subnetIp");
153 if (subnetIpSlash != null) {
154 String subnetIp = subnetIpSlash.substring(0, subnetIpSlash.indexOf('/'));
155 gatewayIps.add(subnetIp);
156 }
157 }
158 } catch (JSONException e) {
159 // TODO Auto-generated catch block
160 e.printStackTrace();
161 }
162
163 return gatewayIps;
Sangho Shin2f263692014-09-15 14:09:41 -0700164 }
165
Sangho Shin79c8d452014-09-18 09:50:21 -0700166
Sangho Shin79c8d452014-09-18 09:50:21 -0700167
168 /**
169 * Send ICMP reply back
170 *
171 * @param sw Switch
172 * @param inPort Port the ICMP packet is forwarded from
173 * @param icmpRequest the ICMP request to handle
174 * @param destinationAddress destination address to send ICMP response to
175 */
176 private void sendICMPResponse(Switch sw, Port inPort, Ethernet icmpRequest) {
177
178 Ethernet icmpReplyEth = new Ethernet();
179
180 IPv4 icmpRequestIpv4 = (IPv4) icmpRequest.getPayload();
181 IPv4 icmpReplyIpv4 = new IPv4();
182 int destAddress = icmpRequestIpv4.getDestinationAddress();
183 icmpReplyIpv4.setDestinationAddress(icmpRequestIpv4.getSourceAddress());
184 icmpReplyIpv4.setSourceAddress(destAddress);
185 icmpReplyIpv4.setTtl((byte)64);
186 icmpReplyIpv4.setChecksum((short)0);
187
188
189 ICMP icmpReply = (ICMP)icmpRequestIpv4.getPayload().clone();
190 icmpReply.setIcmpCode((byte)0x00);
Sangho Shin1aa93542014-09-22 09:49:44 -0700191 icmpReply.setIcmpType((byte) ICMP_TYPE_REPLY);
Sangho Shin79c8d452014-09-18 09:50:21 -0700192 icmpReply.setChecksum((short)0);
193
194 icmpReplyIpv4.setPayload(icmpReply);
195
196 icmpReplyEth.setPayload(icmpReplyIpv4);
197 icmpReplyEth.setEtherType(Ethernet.TYPE_IPV4);
198 icmpReplyEth.setDestinationMACAddress(icmpRequest.getSourceMACAddress());
199 icmpReplyEth.setSourceMACAddress(icmpRequest.getDestinationMACAddress());
200
Sangho Shin1aa93542014-09-22 09:49:44 -0700201 sendPacketOut(sw, icmpReplyEth, new SwitchPort(sw.getDpid(), inPort.getPortNumber()), false);
Sangho Shin79c8d452014-09-18 09:50:21 -0700202
203 log.debug("Send an ICMP response {}", icmpReplyIpv4.toString());
204
205 }
206
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700207 /**
Sangho Shin1aa93542014-09-22 09:49:44 -0700208 * Send PACKET_OUT message with actions
209 * If switches support OFPP_TABLE action, it sends out packet to TABLE port
210 * Otherwise, it sends the packet to the port the packet came from
211 * (in this case, MPLS label is added if the packet needs go through transit switches)
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700212 *
213 * @param sw Switch the packet came from
214 * @param packet Ethernet packet to send
215 * @param switchPort port to send the packet
216 */
Sangho Shin1aa93542014-09-22 09:49:44 -0700217 private void sendPacketOut(Switch sw, Ethernet packet, SwitchPort switchPort, boolean supportOfppTable) {
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700218
219 boolean sameSubnet = false;
220 IOFSwitch ofSwitch = floodlightProvider.getMasterSwitch(sw.getDpid().value());
221 OFFactory factory = ofSwitch.getFactory();
222
223 List<OFAction> actions = new ArrayList<>();
224
Sangho Shin1aa93542014-09-22 09:49:44 -0700225 // If OFPP_TABLE action is not supported in the switch, MPLS label needs to be set
226 // if the packet needs to be delivered crossing switches
227 if (!supportOfppTable) {
228 // Check if the destination is the host attached to the switch
229 int destinationAddress = ((IPv4)packet.getPayload()).getDestinationAddress();
230 for (net.onrc.onos.core.topology.Host host: mutableTopology.getHosts(switchPort)) {
231 IPv4Address hostIpAddress = IPv4Address.of(host.getIpAddress());
232 if (hostIpAddress != null && hostIpAddress.getInt() == destinationAddress) {
233 sameSubnet = true;
234 break;
235 }
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700236 }
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700237
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700238 IPv4Address targetAddress = IPv4Address.of(((IPv4)packet.getPayload()).getDestinationAddress());
239 String destMacAddress = packet.getDestinationMAC().toString();
240 // If the destination host is not attached in the switch
241 // and the destination is not the neighbor switch, then add MPLS label
242 String targetMac = getRouterMACFromConfig(targetAddress);
243 if (!sameSubnet && !targetMac.equals(destMacAddress)) {
Sangho Shin1aa93542014-09-22 09:49:44 -0700244 int mplsLabel = getMplsLabelFromConfig(targetAddress);
245 if (mplsLabel > 0) {
246 OFAction pushlabel = factory.actions().pushMpls(EthType.MPLS_UNICAST);
247 OFOxmMplsLabel l = factory.oxms()
248 .mplsLabel(U32.of(mplsLabel));
249 OFAction setlabelid = factory.actions().buildSetField()
250 .setField(l).build();
251 OFAction copyTtlOut = factory.actions().copyTtlOut();
252 actions.add(pushlabel);
253 actions.add(setlabelid);
254 actions.add(copyTtlOut);
255 }
256 }
257
258 OFAction outport = factory.actions().output(OFPort.of(switchPort.getPortNumber().shortValue()), Short.MAX_VALUE);
259 actions.add(outport);
260 }
261 // If OFPP_TABLE action is supported, first set a rule to allow packet from CONTROLLER port.
262 // Then, send the packet to the table port
263 else {
264 if (!controllerPortAllowed) {
265 addControlPortInVlanTable(sw);
266 controllerPortAllowed = true;
267 }
268 OFAction outport = factory.actions().output(OFPort.TABLE, Short.MAX_VALUE);
269 actions.add(outport);
270 }
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700271
272 OFPacketOut po = factory.buildPacketOut()
273 .setData(packet.serialize())
274 .setActions(actions)
275 .build();
276
277 flowPusher.add(sw.getDpid(), po);
278 }
279
Sangho Shin2f263692014-09-15 14:09:41 -0700280 /**
Sangho Shin1aa93542014-09-22 09:49:44 -0700281 * Get MPLS label for the target address from the network config file
282 *
283 * @param targetAddress - IP address of the target host
284 * @return MPLS label of the switch to send packets to the target address
285 */
286 private int getMplsLabelFromConfig(IPv4Address targetAddress) {
287
288 int mplsLabel = -1;
289
290 for (Switch sw: mutableTopology.getSwitches()) {
291
292 String subnets = sw.getStringAttribute("subnets");
293 try {
294 JSONArray arry = new JSONArray(subnets);
295 for (int i = 0; i < arry.length(); i++) {
296 String subnetIp = (String) arry.getJSONObject(i).get("subnetIp");
297 if (srManager.netMatch(subnetIp, targetAddress.toString())) {
298 String mplsLabelStr = sw.getStringAttribute("nodeSid");
299 if (mplsLabelStr != null)
300 mplsLabel = Integer.parseInt(mplsLabelStr);
301 }
302 }
303 } catch (JSONException e) {
304 // TODO Auto-generated catch block
305 e.printStackTrace();
306 }
307 }
308
309 return mplsLabel;
310 }
311
Sangho Shin2f263692014-09-15 14:09:41 -0700312
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700313 /**
314 * Get Router MAC Address for the target address from the network config file
315 *
316 * @param targetAddress - IP address of the target host
317 * @return Router MAC of the switch to send packets to the target address
318 */
319 private String getRouterMACFromConfig(IPv4Address targetAddress) {
320
321 String routerMac = null;
322
323 for (Switch sw: mutableTopology.getSwitches()) {
324
325 String subnets = sw.getStringAttribute("subnets");
326 try {
327 JSONArray arry = new JSONArray(subnets);
328 for (int i = 0; i < arry.length(); i++) {
329 String subnetIp = (String) arry.getJSONObject(i).get("subnetIp");
330 if (srManager.netMatch(subnetIp, targetAddress.toString())) {
331 routerMac = sw.getStringAttribute("routerMac");
332 }
333 }
334 } catch (JSONException e) {
335 // TODO Auto-generated catch block
336 e.printStackTrace();
337 }
338 }
339
340 return routerMac;
341 }
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700342
343 /**
344 * Add a new rule to VLAN table to forward packets from any port to the next table
345 * It is required to forward packets from controller to pipeline
346 *
347 * @param sw Switch the packet came from
348 */
349 private void addControlPortInVlanTable(Switch sw) {
350
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700351 IOFSwitch ofSwitch = floodlightProvider.getMasterSwitch(sw.getDpid().value());
352 OFFactory factory = ofSwitch.getFactory();
353
Sangho Shin1aa93542014-09-22 09:49:44 -0700354 OFOxmInPort oxp = factory.oxms().inPort(OFPort.CONTROLLER);
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700355 OFOxmVlanVid oxv = factory.oxms()
356 .vlanVid(OFVlanVidMatch.UNTAGGED);
357 OFOxmList oxmList = OFOxmList.of(oxv);
358
Sangho Shin1aa93542014-09-22 09:49:44 -0700359 /* Cqpd switch does not seems to support CONTROLLER port as in_port match rule */
360 //OFOxmList oxmList = OFOxmList.of(oxp, oxv);
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700361
362 OFMatchV3 match = factory.buildMatchV3()
363 .setOxmList(oxmList)
364 .build();
365
366 OFInstruction gotoTbl = factory.instructions().buildGotoTable()
367 .setTableId(TableId.of(TABLE_TMAC)).build();
368 List<OFInstruction> instructions = new ArrayList<OFInstruction>();
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700369 instructions.add(gotoTbl);
370 OFMessage flowEntry = factory.buildFlowAdd()
371 .setTableId(TableId.of(TABLE_VLAN))
372 .setMatch(match)
373 .setInstructions(instructions)
374 .setPriority(1000) // does not matter - all rules
375 // exclusive
376 .setBufferId(OFBufferId.NO_BUFFER)
377 .setIdleTimeout(0)
378 .setHardTimeout(0)
379 //.setXid(getNextTransactionId())
380 .build();
381
382 flowPusher.add(sw.getDpid(), flowEntry);;
Sangho Shin1aa93542014-09-22 09:49:44 -0700383 log.debug("Adding a new vlan-rules in sw {}", sw.getDpid());
Sangho Shin2f263692014-09-15 14:09:41 -0700384
385 }
386
387}