blob: b548e182174085873ddb16630fd3973e6582f0ba [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;
15import net.onrc.onos.core.topology.ITopologyService;
16import net.onrc.onos.core.topology.MutableTopology;
17import net.onrc.onos.core.topology.Port;
18import net.onrc.onos.core.topology.Switch;
Sangho Shin79c8d452014-09-18 09:50:21 -070019import net.onrc.onos.core.util.SwitchPort;
Sangho Shin2f263692014-09-15 14:09:41 -070020
Sangho Shin1aa93542014-09-22 09:49:44 -070021import org.json.JSONArray;
22import org.json.JSONException;
Sangho Shin2f263692014-09-15 14:09:41 -070023import org.projectfloodlight.openflow.protocol.OFFactory;
24import org.projectfloodlight.openflow.protocol.OFMatchV3;
25import org.projectfloodlight.openflow.protocol.OFMessage;
26import org.projectfloodlight.openflow.protocol.OFOxmList;
Sangho Shinf41ac0a2014-09-19 09:50:30 -070027import org.projectfloodlight.openflow.protocol.OFPacketOut;
Sangho Shin2f263692014-09-15 14:09:41 -070028import org.projectfloodlight.openflow.protocol.action.OFAction;
29import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
Sangho Shin1aa93542014-09-22 09:49:44 -070030import org.projectfloodlight.openflow.protocol.oxm.OFOxmInPort;
Sangho Shinf41ac0a2014-09-19 09:50:30 -070031import org.projectfloodlight.openflow.protocol.oxm.OFOxmMplsLabel;
32import org.projectfloodlight.openflow.protocol.oxm.OFOxmVlanVid;
Sangho Shin2f263692014-09-15 14:09:41 -070033import org.projectfloodlight.openflow.types.EthType;
34import org.projectfloodlight.openflow.types.IPv4Address;
Sangho Shin2f263692014-09-15 14:09:41 -070035import org.projectfloodlight.openflow.types.OFBufferId;
36import org.projectfloodlight.openflow.types.OFPort;
Sangho Shinf41ac0a2014-09-19 09:50:30 -070037import org.projectfloodlight.openflow.types.OFVlanVidMatch;
Sangho Shin2f263692014-09-15 14:09:41 -070038import org.projectfloodlight.openflow.types.TableId;
Sangho Shinf41ac0a2014-09-19 09:50:30 -070039import org.projectfloodlight.openflow.types.U32;
Sangho Shin2f263692014-09-15 14:09:41 -070040import org.slf4j.Logger;
41import org.slf4j.LoggerFactory;
42
43public class IcmpHandler implements IPacketListener {
44
45 private SegmentRoutingManager srManager;
46 private IFloodlightProviderService floodlightProvider;
47 private MutableTopology mutableTopology;
48 private IPacketService packetService;
49 private ITopologyService topologyService;
50 private static final Logger log = LoggerFactory
Sangho Shinf41ac0a2014-09-19 09:50:30 -070051 .getLogger(IcmpHandler.class);
Sangho Shin2f263692014-09-15 14:09:41 -070052
53 private IFlowPusherService flowPusher;
Sangho Shin1aa93542014-09-22 09:49:44 -070054 private boolean controllerPortAllowed = false;
Sangho Shin2f263692014-09-15 14:09:41 -070055
56 private static final int TABLE_VLAN = 0;
57 private static final int TABLE_TMAC = 1;
58 private static final int TABLE_IPv4_UNICAST = 2;
59 private static final int TABLE_MPLS = 3;
60 private static final int TABLE_META = 4;
61 private static final int TABLE_ACL = 5;
62
63 private static final short MAX_PRIORITY = (short) 0xffff;
64 private static final short SLASH_24_PRIORITY = (short) 0xfff0;
65 private static final short SLASH_16_PRIORITY = (short) 0xff00;
66 private static final short SLASH_8_PRIORITY = (short) 0xf000;
67 private static final short MIN_PRIORITY = 0x0;
68
Sangho Shin1aa93542014-09-22 09:49:44 -070069 private static final int ICMP_TYPE_ECHO = 0x08;
70 private static final int ICMP_TYPE_REPLY = 0x00;
71
Sangho Shin2f263692014-09-15 14:09:41 -070072
73 public IcmpHandler(FloodlightModuleContext context, SegmentRoutingManager manager) {
74
75 this.floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
76 this.flowPusher = context.getServiceImpl(IFlowPusherService.class);
77 this.packetService = context.getServiceImpl(IPacketService.class);
78 this.topologyService = context.getServiceImpl(ITopologyService.class);
79 this.mutableTopology = topologyService.getTopology();
80
81 this.srManager = manager;
82
83 packetService.registerPacketListener(this);
84
85 }
86
87 @Override
88 public void receive(Switch sw, Port inPort, Ethernet payload) {
89
90 if (payload.getEtherType() == Ethernet.TYPE_IPV4) {
91
92 IPv4 ipv4 = (IPv4)payload.getPayload();
Sangho Shin79c8d452014-09-18 09:50:21 -070093
Sangho Shin2f263692014-09-15 14:09:41 -070094 if (ipv4.getProtocol() == IPv4.PROTOCOL_ICMP) {
Sangho Shin79c8d452014-09-18 09:50:21 -070095 int destinationAddress = ipv4.getDestinationAddress();
Sangho Shineb083032014-09-22 16:11:34 -070096 String destAddressStr = IPv4Address.of(destinationAddress).toString();
Sangho Shin2f263692014-09-15 14:09:41 -070097
Sangho Shin79c8d452014-09-18 09:50:21 -070098 // Check if it is ICMP request to the switch
99 String switchIpAddressSlash = sw.getStringAttribute("routerIp");
100 if (switchIpAddressSlash != null) {
Sangho Shineb083032014-09-22 16:11:34 -0700101 String switchIpAddressStr
102 = switchIpAddressSlash.substring(0, switchIpAddressSlash.indexOf('/'));
Sangho Shin79c8d452014-09-18 09:50:21 -0700103 IPv4Address switchIpAddress = IPv4Address.of(switchIpAddressStr);
Sangho Shineb083032014-09-22 16:11:34 -0700104 List<String> gatewayIps = getSubnetGatewayIps(sw);
Sangho Shin1aa93542014-09-22 09:49:44 -0700105 if (((ICMP)ipv4.getPayload()).getIcmpType() == ICMP_TYPE_ECHO &&
Sangho Shineb083032014-09-22 16:11:34 -0700106 (destinationAddress == switchIpAddress.getInt() ||
107 gatewayIps.contains(destAddressStr))) {
Sangho Shin79c8d452014-09-18 09:50:21 -0700108 sendICMPResponse(sw, inPort, payload);
109 return;
110 }
111 }
Sangho Shin2f263692014-09-15 14:09:41 -0700112 }
113
114 }
Sangho Shineb083032014-09-22 16:11:34 -0700115 }
Sangho Shin2f263692014-09-15 14:09:41 -0700116
Sangho Shineb083032014-09-22 16:11:34 -0700117 /**
118 * Retrieve Gateway IP address of all subnets defined in net config file
119 *
120 * @param sw Switch to retrieve subnet GW IPs for
121 * @return list of GW IP addresses for all subnets
122 */
123 private List<String> getSubnetGatewayIps(Switch sw) {
124
125 List<String> gatewayIps = new ArrayList<String>();
126
127 String subnets = sw.getStringAttribute("subnets");
128 try {
129 JSONArray arry = new JSONArray(subnets);
130 for (int i = 0; i < arry.length(); i++) {
131 String subnetIpSlash = (String) arry.getJSONObject(i).get("subnetIp");
132 if (subnetIpSlash != null) {
133 String subnetIp = subnetIpSlash.substring(0, subnetIpSlash.indexOf('/'));
134 gatewayIps.add(subnetIp);
135 }
136 }
137 } catch (JSONException e) {
138 // TODO Auto-generated catch block
139 e.printStackTrace();
140 }
141
142 return gatewayIps;
Sangho Shin2f263692014-09-15 14:09:41 -0700143 }
144
Sangho Shin79c8d452014-09-18 09:50:21 -0700145
Sangho Shin79c8d452014-09-18 09:50:21 -0700146
147 /**
148 * Send ICMP reply back
149 *
150 * @param sw Switch
151 * @param inPort Port the ICMP packet is forwarded from
152 * @param icmpRequest the ICMP request to handle
153 * @param destinationAddress destination address to send ICMP response to
154 */
155 private void sendICMPResponse(Switch sw, Port inPort, Ethernet icmpRequest) {
156
157 Ethernet icmpReplyEth = new Ethernet();
158
159 IPv4 icmpRequestIpv4 = (IPv4) icmpRequest.getPayload();
160 IPv4 icmpReplyIpv4 = new IPv4();
161 int destAddress = icmpRequestIpv4.getDestinationAddress();
162 icmpReplyIpv4.setDestinationAddress(icmpRequestIpv4.getSourceAddress());
163 icmpReplyIpv4.setSourceAddress(destAddress);
164 icmpReplyIpv4.setTtl((byte)64);
165 icmpReplyIpv4.setChecksum((short)0);
166
167
168 ICMP icmpReply = (ICMP)icmpRequestIpv4.getPayload().clone();
169 icmpReply.setIcmpCode((byte)0x00);
Sangho Shin1aa93542014-09-22 09:49:44 -0700170 icmpReply.setIcmpType((byte) ICMP_TYPE_REPLY);
Sangho Shin79c8d452014-09-18 09:50:21 -0700171 icmpReply.setChecksum((short)0);
172
173 icmpReplyIpv4.setPayload(icmpReply);
174
175 icmpReplyEth.setPayload(icmpReplyIpv4);
176 icmpReplyEth.setEtherType(Ethernet.TYPE_IPV4);
177 icmpReplyEth.setDestinationMACAddress(icmpRequest.getSourceMACAddress());
178 icmpReplyEth.setSourceMACAddress(icmpRequest.getDestinationMACAddress());
179
Sangho Shin1aa93542014-09-22 09:49:44 -0700180 sendPacketOut(sw, icmpReplyEth, new SwitchPort(sw.getDpid(), inPort.getPortNumber()), false);
Sangho Shin79c8d452014-09-18 09:50:21 -0700181
182 log.debug("Send an ICMP response {}", icmpReplyIpv4.toString());
183
184 }
185
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700186 /**
Sangho Shin1aa93542014-09-22 09:49:44 -0700187 * Send PACKET_OUT message with actions
188 * If switches support OFPP_TABLE action, it sends out packet to TABLE port
189 * Otherwise, it sends the packet to the port the packet came from
190 * (in this case, MPLS label is added if the packet needs go through transit switches)
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700191 *
192 * @param sw Switch the packet came from
193 * @param packet Ethernet packet to send
194 * @param switchPort port to send the packet
195 */
Sangho Shin1aa93542014-09-22 09:49:44 -0700196 private void sendPacketOut(Switch sw, Ethernet packet, SwitchPort switchPort, boolean supportOfppTable) {
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700197
198 boolean sameSubnet = false;
199 IOFSwitch ofSwitch = floodlightProvider.getMasterSwitch(sw.getDpid().value());
200 OFFactory factory = ofSwitch.getFactory();
201
202 List<OFAction> actions = new ArrayList<>();
203
Sangho Shin1aa93542014-09-22 09:49:44 -0700204 // If OFPP_TABLE action is not supported in the switch, MPLS label needs to be set
205 // if the packet needs to be delivered crossing switches
206 if (!supportOfppTable) {
207 // Check if the destination is the host attached to the switch
208 int destinationAddress = ((IPv4)packet.getPayload()).getDestinationAddress();
209 for (net.onrc.onos.core.topology.Host host: mutableTopology.getHosts(switchPort)) {
210 IPv4Address hostIpAddress = IPv4Address.of(host.getIpAddress());
211 if (hostIpAddress != null && hostIpAddress.getInt() == destinationAddress) {
212 sameSubnet = true;
213 break;
214 }
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700215 }
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700216
Sangho Shin1aa93542014-09-22 09:49:44 -0700217 // If the destination host is not attached in the switch, add MPLS label
218 if (!sameSubnet) {
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700219
Sangho Shin1aa93542014-09-22 09:49:44 -0700220 IPv4Address targetAddress = IPv4Address.of(((IPv4)packet.getPayload()).getDestinationAddress());
221 int mplsLabel = getMplsLabelFromConfig(targetAddress);
222 if (mplsLabel > 0) {
223 OFAction pushlabel = factory.actions().pushMpls(EthType.MPLS_UNICAST);
224 OFOxmMplsLabel l = factory.oxms()
225 .mplsLabel(U32.of(mplsLabel));
226 OFAction setlabelid = factory.actions().buildSetField()
227 .setField(l).build();
228 OFAction copyTtlOut = factory.actions().copyTtlOut();
229 actions.add(pushlabel);
230 actions.add(setlabelid);
231 actions.add(copyTtlOut);
232 }
233 }
234
235 OFAction outport = factory.actions().output(OFPort.of(switchPort.getPortNumber().shortValue()), Short.MAX_VALUE);
236 actions.add(outport);
237 }
238 // If OFPP_TABLE action is supported, first set a rule to allow packet from CONTROLLER port.
239 // Then, send the packet to the table port
240 else {
241 if (!controllerPortAllowed) {
242 addControlPortInVlanTable(sw);
243 controllerPortAllowed = true;
244 }
245 OFAction outport = factory.actions().output(OFPort.TABLE, Short.MAX_VALUE);
246 actions.add(outport);
247 }
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700248
249 OFPacketOut po = factory.buildPacketOut()
250 .setData(packet.serialize())
251 .setActions(actions)
252 .build();
253
254 flowPusher.add(sw.getDpid(), po);
255 }
256
Sangho Shin2f263692014-09-15 14:09:41 -0700257 /**
Sangho Shin1aa93542014-09-22 09:49:44 -0700258 * Get MPLS label for the target address from the network config file
259 *
260 * @param targetAddress - IP address of the target host
261 * @return MPLS label of the switch to send packets to the target address
262 */
263 private int getMplsLabelFromConfig(IPv4Address targetAddress) {
264
265 int mplsLabel = -1;
266
267 for (Switch sw: mutableTopology.getSwitches()) {
268
269 String subnets = sw.getStringAttribute("subnets");
270 try {
271 JSONArray arry = new JSONArray(subnets);
272 for (int i = 0; i < arry.length(); i++) {
273 String subnetIp = (String) arry.getJSONObject(i).get("subnetIp");
274 if (srManager.netMatch(subnetIp, targetAddress.toString())) {
275 String mplsLabelStr = sw.getStringAttribute("nodeSid");
276 if (mplsLabelStr != null)
277 mplsLabel = Integer.parseInt(mplsLabelStr);
278 }
279 }
280 } catch (JSONException e) {
281 // TODO Auto-generated catch block
282 e.printStackTrace();
283 }
284 }
285
286 return mplsLabel;
287 }
288
Sangho Shin2f263692014-09-15 14:09:41 -0700289
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700290
291 /**
292 * Add a new rule to VLAN table to forward packets from any port to the next table
293 * It is required to forward packets from controller to pipeline
294 *
295 * @param sw Switch the packet came from
296 */
297 private void addControlPortInVlanTable(Switch sw) {
298
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700299 IOFSwitch ofSwitch = floodlightProvider.getMasterSwitch(sw.getDpid().value());
300 OFFactory factory = ofSwitch.getFactory();
301
Sangho Shin1aa93542014-09-22 09:49:44 -0700302 OFOxmInPort oxp = factory.oxms().inPort(OFPort.CONTROLLER);
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700303 OFOxmVlanVid oxv = factory.oxms()
304 .vlanVid(OFVlanVidMatch.UNTAGGED);
305 OFOxmList oxmList = OFOxmList.of(oxv);
306
Sangho Shin1aa93542014-09-22 09:49:44 -0700307 /* Cqpd switch does not seems to support CONTROLLER port as in_port match rule */
308 //OFOxmList oxmList = OFOxmList.of(oxp, oxv);
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700309
310 OFMatchV3 match = factory.buildMatchV3()
311 .setOxmList(oxmList)
312 .build();
313
314 OFInstruction gotoTbl = factory.instructions().buildGotoTable()
315 .setTableId(TableId.of(TABLE_TMAC)).build();
316 List<OFInstruction> instructions = new ArrayList<OFInstruction>();
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700317 instructions.add(gotoTbl);
318 OFMessage flowEntry = factory.buildFlowAdd()
319 .setTableId(TableId.of(TABLE_VLAN))
320 .setMatch(match)
321 .setInstructions(instructions)
322 .setPriority(1000) // does not matter - all rules
323 // exclusive
324 .setBufferId(OFBufferId.NO_BUFFER)
325 .setIdleTimeout(0)
326 .setHardTimeout(0)
327 //.setXid(getNextTransactionId())
328 .build();
329
330 flowPusher.add(sw.getDpid(), flowEntry);;
Sangho Shin1aa93542014-09-22 09:49:44 -0700331 log.debug("Adding a new vlan-rules in sw {}", sw.getDpid());
Sangho Shin2f263692014-09-15 14:09:41 -0700332
333 }
334
335}