blob: b8bbc994ad289cc12b3c655f19f1ab98a385839e [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.api.packet.IPacketListener;
10import net.onrc.onos.api.packet.IPacketService;
11import net.onrc.onos.core.flowprogrammer.IFlowPusherService;
12import net.onrc.onos.core.packet.Ethernet;
Sangho Shin79c8d452014-09-18 09:50:21 -070013import net.onrc.onos.core.packet.ICMP;
Sangho Shin2f263692014-09-15 14:09:41 -070014import net.onrc.onos.core.packet.IPv4;
Srikanth Vavilapalli5ddf8f02014-09-24 11:02:28 -070015import net.onrc.onos.core.topology.Host;
Sangho Shin2f263692014-09-15 14:09:41 -070016import net.onrc.onos.core.topology.ITopologyService;
17import net.onrc.onos.core.topology.MutableTopology;
18import net.onrc.onos.core.topology.Port;
19import net.onrc.onos.core.topology.Switch;
Sangho Shin79c8d452014-09-18 09:50:21 -070020import net.onrc.onos.core.util.SwitchPort;
Sangho Shin2f263692014-09-15 14:09:41 -070021
Sangho Shin1aa93542014-09-22 09:49:44 -070022import org.json.JSONArray;
23import org.json.JSONException;
Sangho Shin2f263692014-09-15 14:09:41 -070024import org.projectfloodlight.openflow.protocol.OFFactory;
25import org.projectfloodlight.openflow.protocol.OFMatchV3;
26import org.projectfloodlight.openflow.protocol.OFMessage;
27import org.projectfloodlight.openflow.protocol.OFOxmList;
Sangho Shinf41ac0a2014-09-19 09:50:30 -070028import org.projectfloodlight.openflow.protocol.OFPacketOut;
Sangho Shin2f263692014-09-15 14:09:41 -070029import org.projectfloodlight.openflow.protocol.action.OFAction;
30import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
Sangho Shin1aa93542014-09-22 09:49:44 -070031import org.projectfloodlight.openflow.protocol.oxm.OFOxmInPort;
Sangho Shinf41ac0a2014-09-19 09:50:30 -070032import org.projectfloodlight.openflow.protocol.oxm.OFOxmMplsLabel;
33import org.projectfloodlight.openflow.protocol.oxm.OFOxmVlanVid;
Sangho Shin2f263692014-09-15 14:09:41 -070034import org.projectfloodlight.openflow.types.EthType;
35import org.projectfloodlight.openflow.types.IPv4Address;
Sangho Shin2f263692014-09-15 14:09:41 -070036import org.projectfloodlight.openflow.types.OFBufferId;
37import org.projectfloodlight.openflow.types.OFPort;
Sangho Shinf41ac0a2014-09-19 09:50:30 -070038import org.projectfloodlight.openflow.types.OFVlanVidMatch;
Sangho Shin2f263692014-09-15 14:09:41 -070039import org.projectfloodlight.openflow.types.TableId;
Sangho Shinf41ac0a2014-09-19 09:50:30 -070040import org.projectfloodlight.openflow.types.U32;
Sangho Shin2f263692014-09-15 14:09:41 -070041import org.slf4j.Logger;
42import org.slf4j.LoggerFactory;
43
44public class IcmpHandler implements IPacketListener {
45
46 private SegmentRoutingManager srManager;
47 private IFloodlightProviderService floodlightProvider;
48 private MutableTopology mutableTopology;
49 private IPacketService packetService;
50 private ITopologyService topologyService;
51 private static final Logger log = LoggerFactory
Sangho Shinf41ac0a2014-09-19 09:50:30 -070052 .getLogger(IcmpHandler.class);
Sangho Shin2f263692014-09-15 14:09:41 -070053
54 private IFlowPusherService flowPusher;
Sangho Shin1aa93542014-09-22 09:49:44 -070055 private boolean controllerPortAllowed = false;
Sangho Shin2f263692014-09-15 14:09:41 -070056
57 private static final int TABLE_VLAN = 0;
58 private static final int TABLE_TMAC = 1;
59 private static final int TABLE_IPv4_UNICAST = 2;
60 private static final int TABLE_MPLS = 3;
61 private static final int TABLE_META = 4;
62 private static final int TABLE_ACL = 5;
63
64 private static final short MAX_PRIORITY = (short) 0xffff;
65 private static final short SLASH_24_PRIORITY = (short) 0xfff0;
66 private static final short SLASH_16_PRIORITY = (short) 0xff00;
67 private static final short SLASH_8_PRIORITY = (short) 0xf000;
68 private static final short MIN_PRIORITY = 0x0;
69
Sangho Shin1aa93542014-09-22 09:49:44 -070070 private static final int ICMP_TYPE_ECHO = 0x08;
71 private static final int ICMP_TYPE_REPLY = 0x00;
72
Sangho Shin2f263692014-09-15 14:09:41 -070073
74 public IcmpHandler(FloodlightModuleContext context, SegmentRoutingManager manager) {
75
76 this.floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
77 this.flowPusher = context.getServiceImpl(IFlowPusherService.class);
78 this.packetService = context.getServiceImpl(IPacketService.class);
79 this.topologyService = context.getServiceImpl(ITopologyService.class);
80 this.mutableTopology = topologyService.getTopology();
81
82 this.srManager = manager;
83
84 packetService.registerPacketListener(this);
85
86 }
87
88 @Override
89 public void receive(Switch sw, Port inPort, Ethernet payload) {
90
91 if (payload.getEtherType() == Ethernet.TYPE_IPV4) {
92
93 IPv4 ipv4 = (IPv4)payload.getPayload();
Sangho Shin79c8d452014-09-18 09:50:21 -070094
Sangho Shin2f263692014-09-15 14:09:41 -070095 if (ipv4.getProtocol() == IPv4.PROTOCOL_ICMP) {
Srikanth Vavilapalli5ddf8f02014-09-24 11:02:28 -070096
97 log.debug("ICMPHandler: Received a ICMP packet {} from sw {} ",
98 payload.toString(), sw.getDpid());
Sangho Shin79c8d452014-09-18 09:50:21 -070099 int destinationAddress = ipv4.getDestinationAddress();
Sangho Shineb083032014-09-22 16:11:34 -0700100 String destAddressStr = IPv4Address.of(destinationAddress).toString();
Sangho Shin2f263692014-09-15 14:09:41 -0700101
Sangho Shin79c8d452014-09-18 09:50:21 -0700102 // Check if it is ICMP request to the switch
103 String switchIpAddressSlash = sw.getStringAttribute("routerIp");
104 if (switchIpAddressSlash != null) {
Sangho Shineb083032014-09-22 16:11:34 -0700105 String switchIpAddressStr
106 = switchIpAddressSlash.substring(0, switchIpAddressSlash.indexOf('/'));
Sangho Shin79c8d452014-09-18 09:50:21 -0700107 IPv4Address switchIpAddress = IPv4Address.of(switchIpAddressStr);
Sangho Shineb083032014-09-22 16:11:34 -0700108 List<String> gatewayIps = getSubnetGatewayIps(sw);
Sangho Shin1aa93542014-09-22 09:49:44 -0700109 if (((ICMP)ipv4.getPayload()).getIcmpType() == ICMP_TYPE_ECHO &&
Sangho Shineb083032014-09-22 16:11:34 -0700110 (destinationAddress == switchIpAddress.getInt() ||
111 gatewayIps.contains(destAddressStr))) {
Srikanth Vavilapalli5ddf8f02014-09-24 11:02:28 -0700112 log.debug("ICMPHandler: ICMP packet for sw {} and "
113 + "sending ICMP response ", sw.getDpid());
Sangho Shin79c8d452014-09-18 09:50:21 -0700114 sendICMPResponse(sw, inPort, payload);
115 return;
116 }
117 }
Srikanth Vavilapalli5ddf8f02014-09-24 11:02:28 -0700118
119 /* Check if ICMP is for any switch known host */
120 for (Host host: sw.getHosts()) {
121 IPv4Address hostIpAddress =
122 IPv4Address.of(host.getIpAddress());
123 if (hostIpAddress != null &&
124 hostIpAddress.equals(destinationAddress)) {
125 /* TODO: We should not have come here as ARP itself
126 * would have installed a Route to the host. See if
127 * we can remove this code
128 */
129 log.debug("ICMPHandler: ICMP request for known host {}",
130 hostIpAddress);
131 byte[] destinationMacAddress = host.getMacAddress().toBytes();
132 srManager.addRouteToHost(sw,
133 destinationAddress, destinationMacAddress);
134 return;
135 }
136 }
137 /* ICMP for an unknown host */
138 log.debug("ICMPHandler: ICMP request for unknown host {}"
139 + " and sending ARP request", destinationAddress);
140 srManager.sendArpRequest(sw, destinationAddress, inPort);
Sangho Shin2f263692014-09-15 14:09:41 -0700141 }
142
143 }
Sangho Shineb083032014-09-22 16:11:34 -0700144 }
Sangho Shin2f263692014-09-15 14:09:41 -0700145
Sangho Shineb083032014-09-22 16:11:34 -0700146 /**
147 * Retrieve Gateway IP address of all subnets defined in net config file
148 *
149 * @param sw Switch to retrieve subnet GW IPs for
150 * @return list of GW IP addresses for all subnets
151 */
152 private List<String> getSubnetGatewayIps(Switch sw) {
153
154 List<String> gatewayIps = new ArrayList<String>();
155
156 String subnets = sw.getStringAttribute("subnets");
157 try {
158 JSONArray arry = new JSONArray(subnets);
159 for (int i = 0; i < arry.length(); i++) {
160 String subnetIpSlash = (String) arry.getJSONObject(i).get("subnetIp");
161 if (subnetIpSlash != null) {
162 String subnetIp = subnetIpSlash.substring(0, subnetIpSlash.indexOf('/'));
163 gatewayIps.add(subnetIp);
164 }
165 }
166 } catch (JSONException e) {
167 // TODO Auto-generated catch block
168 e.printStackTrace();
169 }
170
171 return gatewayIps;
Sangho Shin2f263692014-09-15 14:09:41 -0700172 }
173
Sangho Shin79c8d452014-09-18 09:50:21 -0700174
Sangho Shin79c8d452014-09-18 09:50:21 -0700175
176 /**
177 * Send ICMP reply back
178 *
179 * @param sw Switch
180 * @param inPort Port the ICMP packet is forwarded from
181 * @param icmpRequest the ICMP request to handle
182 * @param destinationAddress destination address to send ICMP response to
183 */
184 private void sendICMPResponse(Switch sw, Port inPort, Ethernet icmpRequest) {
185
186 Ethernet icmpReplyEth = new Ethernet();
187
188 IPv4 icmpRequestIpv4 = (IPv4) icmpRequest.getPayload();
189 IPv4 icmpReplyIpv4 = new IPv4();
190 int destAddress = icmpRequestIpv4.getDestinationAddress();
191 icmpReplyIpv4.setDestinationAddress(icmpRequestIpv4.getSourceAddress());
192 icmpReplyIpv4.setSourceAddress(destAddress);
193 icmpReplyIpv4.setTtl((byte)64);
194 icmpReplyIpv4.setChecksum((short)0);
195
196
197 ICMP icmpReply = (ICMP)icmpRequestIpv4.getPayload().clone();
198 icmpReply.setIcmpCode((byte)0x00);
Sangho Shin1aa93542014-09-22 09:49:44 -0700199 icmpReply.setIcmpType((byte) ICMP_TYPE_REPLY);
Sangho Shin79c8d452014-09-18 09:50:21 -0700200 icmpReply.setChecksum((short)0);
201
202 icmpReplyIpv4.setPayload(icmpReply);
203
204 icmpReplyEth.setPayload(icmpReplyIpv4);
205 icmpReplyEth.setEtherType(Ethernet.TYPE_IPV4);
206 icmpReplyEth.setDestinationMACAddress(icmpRequest.getSourceMACAddress());
207 icmpReplyEth.setSourceMACAddress(icmpRequest.getDestinationMACAddress());
208
Sangho Shin1aa93542014-09-22 09:49:44 -0700209 sendPacketOut(sw, icmpReplyEth, new SwitchPort(sw.getDpid(), inPort.getPortNumber()), false);
Sangho Shin79c8d452014-09-18 09:50:21 -0700210
211 log.debug("Send an ICMP response {}", icmpReplyIpv4.toString());
212
213 }
214
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700215 /**
Sangho Shin1aa93542014-09-22 09:49:44 -0700216 * Send PACKET_OUT message with actions
217 * If switches support OFPP_TABLE action, it sends out packet to TABLE port
218 * Otherwise, it sends the packet to the port the packet came from
219 * (in this case, MPLS label is added if the packet needs go through transit switches)
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700220 *
221 * @param sw Switch the packet came from
222 * @param packet Ethernet packet to send
223 * @param switchPort port to send the packet
224 */
Sangho Shin1aa93542014-09-22 09:49:44 -0700225 private void sendPacketOut(Switch sw, Ethernet packet, SwitchPort switchPort, boolean supportOfppTable) {
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700226
227 boolean sameSubnet = false;
228 IOFSwitch ofSwitch = floodlightProvider.getMasterSwitch(sw.getDpid().value());
229 OFFactory factory = ofSwitch.getFactory();
230
231 List<OFAction> actions = new ArrayList<>();
232
Sangho Shin1aa93542014-09-22 09:49:44 -0700233 // If OFPP_TABLE action is not supported in the switch, MPLS label needs to be set
234 // if the packet needs to be delivered crossing switches
235 if (!supportOfppTable) {
236 // Check if the destination is the host attached to the switch
237 int destinationAddress = ((IPv4)packet.getPayload()).getDestinationAddress();
238 for (net.onrc.onos.core.topology.Host host: mutableTopology.getHosts(switchPort)) {
239 IPv4Address hostIpAddress = IPv4Address.of(host.getIpAddress());
240 if (hostIpAddress != null && hostIpAddress.getInt() == destinationAddress) {
241 sameSubnet = true;
242 break;
243 }
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700244 }
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700245
Sangho Shin1aa93542014-09-22 09:49:44 -0700246 // If the destination host is not attached in the switch, add MPLS label
247 if (!sameSubnet) {
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700248
Sangho Shin1aa93542014-09-22 09:49:44 -0700249 IPv4Address targetAddress = IPv4Address.of(((IPv4)packet.getPayload()).getDestinationAddress());
250 int mplsLabel = getMplsLabelFromConfig(targetAddress);
251 if (mplsLabel > 0) {
252 OFAction pushlabel = factory.actions().pushMpls(EthType.MPLS_UNICAST);
253 OFOxmMplsLabel l = factory.oxms()
254 .mplsLabel(U32.of(mplsLabel));
255 OFAction setlabelid = factory.actions().buildSetField()
256 .setField(l).build();
257 OFAction copyTtlOut = factory.actions().copyTtlOut();
258 actions.add(pushlabel);
259 actions.add(setlabelid);
260 actions.add(copyTtlOut);
261 }
262 }
263
264 OFAction outport = factory.actions().output(OFPort.of(switchPort.getPortNumber().shortValue()), Short.MAX_VALUE);
265 actions.add(outport);
266 }
267 // If OFPP_TABLE action is supported, first set a rule to allow packet from CONTROLLER port.
268 // Then, send the packet to the table port
269 else {
270 if (!controllerPortAllowed) {
271 addControlPortInVlanTable(sw);
272 controllerPortAllowed = true;
273 }
274 OFAction outport = factory.actions().output(OFPort.TABLE, Short.MAX_VALUE);
275 actions.add(outport);
276 }
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700277
278 OFPacketOut po = factory.buildPacketOut()
279 .setData(packet.serialize())
280 .setActions(actions)
281 .build();
282
283 flowPusher.add(sw.getDpid(), po);
284 }
285
Sangho Shin2f263692014-09-15 14:09:41 -0700286 /**
Sangho Shin1aa93542014-09-22 09:49:44 -0700287 * Get MPLS label for the target address from the network config file
288 *
289 * @param targetAddress - IP address of the target host
290 * @return MPLS label of the switch to send packets to the target address
291 */
292 private int getMplsLabelFromConfig(IPv4Address targetAddress) {
293
294 int mplsLabel = -1;
295
296 for (Switch sw: mutableTopology.getSwitches()) {
297
298 String subnets = sw.getStringAttribute("subnets");
299 try {
300 JSONArray arry = new JSONArray(subnets);
301 for (int i = 0; i < arry.length(); i++) {
302 String subnetIp = (String) arry.getJSONObject(i).get("subnetIp");
303 if (srManager.netMatch(subnetIp, targetAddress.toString())) {
304 String mplsLabelStr = sw.getStringAttribute("nodeSid");
305 if (mplsLabelStr != null)
306 mplsLabel = Integer.parseInt(mplsLabelStr);
307 }
308 }
309 } catch (JSONException e) {
310 // TODO Auto-generated catch block
311 e.printStackTrace();
312 }
313 }
314
315 return mplsLabel;
316 }
317
Sangho Shin2f263692014-09-15 14:09:41 -0700318
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700319
320 /**
321 * Add a new rule to VLAN table to forward packets from any port to the next table
322 * It is required to forward packets from controller to pipeline
323 *
324 * @param sw Switch the packet came from
325 */
326 private void addControlPortInVlanTable(Switch sw) {
327
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700328 IOFSwitch ofSwitch = floodlightProvider.getMasterSwitch(sw.getDpid().value());
329 OFFactory factory = ofSwitch.getFactory();
330
Sangho Shin1aa93542014-09-22 09:49:44 -0700331 OFOxmInPort oxp = factory.oxms().inPort(OFPort.CONTROLLER);
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700332 OFOxmVlanVid oxv = factory.oxms()
333 .vlanVid(OFVlanVidMatch.UNTAGGED);
334 OFOxmList oxmList = OFOxmList.of(oxv);
335
Sangho Shin1aa93542014-09-22 09:49:44 -0700336 /* Cqpd switch does not seems to support CONTROLLER port as in_port match rule */
337 //OFOxmList oxmList = OFOxmList.of(oxp, oxv);
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700338
339 OFMatchV3 match = factory.buildMatchV3()
340 .setOxmList(oxmList)
341 .build();
342
343 OFInstruction gotoTbl = factory.instructions().buildGotoTable()
344 .setTableId(TableId.of(TABLE_TMAC)).build();
345 List<OFInstruction> instructions = new ArrayList<OFInstruction>();
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700346 instructions.add(gotoTbl);
347 OFMessage flowEntry = factory.buildFlowAdd()
348 .setTableId(TableId.of(TABLE_VLAN))
349 .setMatch(match)
350 .setInstructions(instructions)
351 .setPriority(1000) // does not matter - all rules
352 // exclusive
353 .setBufferId(OFBufferId.NO_BUFFER)
354 .setIdleTimeout(0)
355 .setHardTimeout(0)
356 //.setXid(getNextTransactionId())
357 .build();
358
359 flowPusher.add(sw.getDpid(), flowEntry);;
Sangho Shin1aa93542014-09-22 09:49:44 -0700360 log.debug("Adding a new vlan-rules in sw {}", sw.getDpid());
Sangho Shin2f263692014-09-15 14:09:41 -0700361
362 }
363
364}