blob: 2f5576a8867903e78482caa1eca0c233968ec3c5 [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 Shin1aa93542014-09-22 09:49:44 -0700238 // If the destination host is not attached in the switch, add MPLS label
239 if (!sameSubnet) {
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700240
Sangho Shin1aa93542014-09-22 09:49:44 -0700241 IPv4Address targetAddress = IPv4Address.of(((IPv4)packet.getPayload()).getDestinationAddress());
242 int mplsLabel = getMplsLabelFromConfig(targetAddress);
243 if (mplsLabel > 0) {
244 OFAction pushlabel = factory.actions().pushMpls(EthType.MPLS_UNICAST);
245 OFOxmMplsLabel l = factory.oxms()
246 .mplsLabel(U32.of(mplsLabel));
247 OFAction setlabelid = factory.actions().buildSetField()
248 .setField(l).build();
249 OFAction copyTtlOut = factory.actions().copyTtlOut();
250 actions.add(pushlabel);
251 actions.add(setlabelid);
252 actions.add(copyTtlOut);
253 }
254 }
255
256 OFAction outport = factory.actions().output(OFPort.of(switchPort.getPortNumber().shortValue()), Short.MAX_VALUE);
257 actions.add(outport);
258 }
259 // If OFPP_TABLE action is supported, first set a rule to allow packet from CONTROLLER port.
260 // Then, send the packet to the table port
261 else {
262 if (!controllerPortAllowed) {
263 addControlPortInVlanTable(sw);
264 controllerPortAllowed = true;
265 }
266 OFAction outport = factory.actions().output(OFPort.TABLE, Short.MAX_VALUE);
267 actions.add(outport);
268 }
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700269
270 OFPacketOut po = factory.buildPacketOut()
271 .setData(packet.serialize())
272 .setActions(actions)
273 .build();
274
275 flowPusher.add(sw.getDpid(), po);
276 }
277
Sangho Shin2f263692014-09-15 14:09:41 -0700278 /**
Sangho Shin1aa93542014-09-22 09:49:44 -0700279 * Get MPLS label for the target address from the network config file
280 *
281 * @param targetAddress - IP address of the target host
282 * @return MPLS label of the switch to send packets to the target address
283 */
284 private int getMplsLabelFromConfig(IPv4Address targetAddress) {
285
286 int mplsLabel = -1;
287
288 for (Switch sw: mutableTopology.getSwitches()) {
289
290 String subnets = sw.getStringAttribute("subnets");
291 try {
292 JSONArray arry = new JSONArray(subnets);
293 for (int i = 0; i < arry.length(); i++) {
294 String subnetIp = (String) arry.getJSONObject(i).get("subnetIp");
295 if (srManager.netMatch(subnetIp, targetAddress.toString())) {
296 String mplsLabelStr = sw.getStringAttribute("nodeSid");
297 if (mplsLabelStr != null)
298 mplsLabel = Integer.parseInt(mplsLabelStr);
299 }
300 }
301 } catch (JSONException e) {
302 // TODO Auto-generated catch block
303 e.printStackTrace();
304 }
305 }
306
307 return mplsLabel;
308 }
309
Sangho Shin2f263692014-09-15 14:09:41 -0700310
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700311
312 /**
313 * Add a new rule to VLAN table to forward packets from any port to the next table
314 * It is required to forward packets from controller to pipeline
315 *
316 * @param sw Switch the packet came from
317 */
318 private void addControlPortInVlanTable(Switch sw) {
319
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700320 IOFSwitch ofSwitch = floodlightProvider.getMasterSwitch(sw.getDpid().value());
321 OFFactory factory = ofSwitch.getFactory();
322
Sangho Shin1aa93542014-09-22 09:49:44 -0700323 OFOxmInPort oxp = factory.oxms().inPort(OFPort.CONTROLLER);
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700324 OFOxmVlanVid oxv = factory.oxms()
325 .vlanVid(OFVlanVidMatch.UNTAGGED);
326 OFOxmList oxmList = OFOxmList.of(oxv);
327
Sangho Shin1aa93542014-09-22 09:49:44 -0700328 /* Cqpd switch does not seems to support CONTROLLER port as in_port match rule */
329 //OFOxmList oxmList = OFOxmList.of(oxp, oxv);
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700330
331 OFMatchV3 match = factory.buildMatchV3()
332 .setOxmList(oxmList)
333 .build();
334
335 OFInstruction gotoTbl = factory.instructions().buildGotoTable()
336 .setTableId(TableId.of(TABLE_TMAC)).build();
337 List<OFInstruction> instructions = new ArrayList<OFInstruction>();
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700338 instructions.add(gotoTbl);
339 OFMessage flowEntry = factory.buildFlowAdd()
340 .setTableId(TableId.of(TABLE_VLAN))
341 .setMatch(match)
342 .setInstructions(instructions)
343 .setPriority(1000) // does not matter - all rules
344 // exclusive
345 .setBufferId(OFBufferId.NO_BUFFER)
346 .setIdleTimeout(0)
347 .setHardTimeout(0)
348 //.setXid(getNextTransactionId())
349 .build();
350
351 flowPusher.add(sw.getDpid(), flowEntry);;
Sangho Shin1aa93542014-09-22 09:49:44 -0700352 log.debug("Adding a new vlan-rules in sw {}", sw.getDpid());
Sangho Shin2f263692014-09-15 14:09:41 -0700353
354 }
355
356}