blob: 676e68a95bed1e4552027490b60ec9dd6af0379f [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;
9import net.floodlightcontroller.util.MACAddress;
10import net.onrc.onos.api.packet.IPacketListener;
11import net.onrc.onos.api.packet.IPacketService;
12import net.onrc.onos.core.flowprogrammer.IFlowPusherService;
13import net.onrc.onos.core.packet.Ethernet;
Sangho Shin79c8d452014-09-18 09:50:21 -070014import net.onrc.onos.core.packet.ICMP;
Sangho Shin2f263692014-09-15 14:09:41 -070015import net.onrc.onos.core.packet.IPv4;
16import 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
22import 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;
29import org.projectfloodlight.openflow.protocol.oxm.OFOxmEthDst;
30import org.projectfloodlight.openflow.protocol.oxm.OFOxmEthSrc;
31import org.projectfloodlight.openflow.protocol.oxm.OFOxmEthType;
32import org.projectfloodlight.openflow.protocol.oxm.OFOxmIpv4DstMasked;
Sangho Shinf41ac0a2014-09-19 09:50:30 -070033import org.projectfloodlight.openflow.protocol.oxm.OFOxmMplsLabel;
34import org.projectfloodlight.openflow.protocol.oxm.OFOxmVlanVid;
Sangho Shin2f263692014-09-15 14:09:41 -070035import org.projectfloodlight.openflow.types.EthType;
36import org.projectfloodlight.openflow.types.IPv4Address;
37import org.projectfloodlight.openflow.types.MacAddress;
38import org.projectfloodlight.openflow.types.OFBufferId;
39import org.projectfloodlight.openflow.types.OFPort;
Sangho Shinf41ac0a2014-09-19 09:50:30 -070040import org.projectfloodlight.openflow.types.OFVlanVidMatch;
Sangho Shin2f263692014-09-15 14:09:41 -070041import org.projectfloodlight.openflow.types.TableId;
Sangho Shinf41ac0a2014-09-19 09:50:30 -070042import org.projectfloodlight.openflow.types.U32;
Sangho Shin2f263692014-09-15 14:09:41 -070043import org.slf4j.Logger;
44import org.slf4j.LoggerFactory;
45
46public class IcmpHandler implements IPacketListener {
47
48 private SegmentRoutingManager srManager;
49 private IFloodlightProviderService floodlightProvider;
50 private MutableTopology mutableTopology;
51 private IPacketService packetService;
52 private ITopologyService topologyService;
53 private static final Logger log = LoggerFactory
Sangho Shinf41ac0a2014-09-19 09:50:30 -070054 .getLogger(IcmpHandler.class);
Sangho Shin2f263692014-09-15 14:09:41 -070055
56 private IFlowPusherService flowPusher;
Sangho Shinf41ac0a2014-09-19 09:50:30 -070057 private boolean added;
Sangho Shin2f263692014-09-15 14:09:41 -070058
59 private static final int TABLE_VLAN = 0;
60 private static final int TABLE_TMAC = 1;
61 private static final int TABLE_IPv4_UNICAST = 2;
62 private static final int TABLE_MPLS = 3;
63 private static final int TABLE_META = 4;
64 private static final int TABLE_ACL = 5;
65
66 private static final short MAX_PRIORITY = (short) 0xffff;
67 private static final short SLASH_24_PRIORITY = (short) 0xfff0;
68 private static final short SLASH_16_PRIORITY = (short) 0xff00;
69 private static final short SLASH_8_PRIORITY = (short) 0xf000;
70 private static final short MIN_PRIORITY = 0x0;
71
72
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;
Sangho Shinf41ac0a2014-09-19 09:50:30 -070082 this.added = false;
Sangho Shin2f263692014-09-15 14:09:41 -070083
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) {
Sangho Shin79c8d452014-09-18 09:50:21 -070096 int destinationAddress = ipv4.getDestinationAddress();
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) {
101 String switchIpAddressStr = switchIpAddressSlash.substring(0, switchIpAddressSlash.indexOf('/'));
102 IPv4Address switchIpAddress = IPv4Address.of(switchIpAddressStr);
Sangho Shin2f263692014-09-15 14:09:41 -0700103
Sangho Shin79c8d452014-09-18 09:50:21 -0700104 if (((ICMP)ipv4.getPayload()).getIcmpType() == 0x08 &&
105 destinationAddress == switchIpAddress.getInt()) {
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700106 //addControlPortInVlanTable(sw);
Sangho Shin79c8d452014-09-18 09:50:21 -0700107 sendICMPResponse(sw, inPort, payload);
108 return;
109 }
110 }
111
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700112
Sangho Shin79c8d452014-09-18 09:50:21 -0700113 // Check if the destination is any host known to TopologyService
114 for (net.onrc.onos.core.topology.Host host: mutableTopology.getHosts()) {
115 IPv4Address hostIpAddress = IPv4Address.of(host.getIpAddress());
116 if (hostIpAddress != null && hostIpAddress.getInt() == destinationAddress) {
117 byte[] destinationMacAddress = host.getMacAddress().toBytes();
118 addRouteToHost(sw, destinationAddress, destinationMacAddress);
119 return;
120 }
121 }
Sangho Shin2f263692014-09-15 14:09:41 -0700122 }
123
124 }
125
126 }
127
Sangho Shin79c8d452014-09-18 09:50:21 -0700128
Sangho Shin79c8d452014-09-18 09:50:21 -0700129
130 /**
131 * Send ICMP reply back
132 *
133 * @param sw Switch
134 * @param inPort Port the ICMP packet is forwarded from
135 * @param icmpRequest the ICMP request to handle
136 * @param destinationAddress destination address to send ICMP response to
137 */
138 private void sendICMPResponse(Switch sw, Port inPort, Ethernet icmpRequest) {
139
140 Ethernet icmpReplyEth = new Ethernet();
141
142 IPv4 icmpRequestIpv4 = (IPv4) icmpRequest.getPayload();
143 IPv4 icmpReplyIpv4 = new IPv4();
144 int destAddress = icmpRequestIpv4.getDestinationAddress();
145 icmpReplyIpv4.setDestinationAddress(icmpRequestIpv4.getSourceAddress());
146 icmpReplyIpv4.setSourceAddress(destAddress);
147 icmpReplyIpv4.setTtl((byte)64);
148 icmpReplyIpv4.setChecksum((short)0);
149
150
151 ICMP icmpReply = (ICMP)icmpRequestIpv4.getPayload().clone();
152 icmpReply.setIcmpCode((byte)0x00);
153 icmpReply.setIcmpType((byte) 0x00);
154 icmpReply.setChecksum((short)0);
155
156 icmpReplyIpv4.setPayload(icmpReply);
157
158 icmpReplyEth.setPayload(icmpReplyIpv4);
159 icmpReplyEth.setEtherType(Ethernet.TYPE_IPV4);
160 icmpReplyEth.setDestinationMACAddress(icmpRequest.getSourceMACAddress());
161 icmpReplyEth.setSourceMACAddress(icmpRequest.getDestinationMACAddress());
162
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700163 sendPacketOut(sw, icmpReplyEth, new SwitchPort(sw.getDpid(), inPort.getPortNumber()));
Sangho Shin79c8d452014-09-18 09:50:21 -0700164
165 log.debug("Send an ICMP response {}", icmpReplyIpv4.toString());
166
167 }
168
169
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700170
171 /**
172 * Send PACKET_OUT message with some actions
173 *
174 * @param sw Switch the packet came from
175 * @param packet Ethernet packet to send
176 * @param switchPort port to send the packet
177 */
178 private void sendPacketOut(Switch sw, Ethernet packet, SwitchPort switchPort) {
179
180 boolean sameSubnet = false;
181 IOFSwitch ofSwitch = floodlightProvider.getMasterSwitch(sw.getDpid().value());
182 OFFactory factory = ofSwitch.getFactory();
183
184 List<OFAction> actions = new ArrayList<>();
185
186 // Check if the destination is the host attached to the switch
187 int destinationAddress = ((IPv4)packet.getPayload()).getDestinationAddress();
188 for (net.onrc.onos.core.topology.Host host: mutableTopology.getHosts(switchPort)) {
189 IPv4Address hostIpAddress = IPv4Address.of(host.getIpAddress());
190 if (hostIpAddress != null && hostIpAddress.getInt() == destinationAddress) {
191 sameSubnet = true;
192 break;
193 }
194 }
195
196 // If the destination host is not attached in the switch, add MPLS label
197 if (!sameSubnet) {
198 OFAction pushlabel = factory.actions().pushMpls(EthType.MPLS_UNICAST);
199 OFOxmMplsLabel l = factory.oxms()
200 .mplsLabel(U32.of(103));
201 OFAction setlabelid = factory.actions().buildSetField()
202 .setField(l).build();
203 OFAction copyTtlOut = factory.actions().copyTtlOut();
204 actions.add(pushlabel);
205 actions.add(setlabelid);
206 actions.add(copyTtlOut);
207 }
208
209 OFAction outport = factory.actions().output(OFPort.of((int) switchPort.getPortNumber().value()), Short.MAX_VALUE);
210 actions.add(outport);
211
212 OFPacketOut po = factory.buildPacketOut()
213 .setData(packet.serialize())
214 .setActions(actions)
215 .build();
216
217 flowPusher.add(sw.getDpid(), po);
218 }
219
Sangho Shin2f263692014-09-15 14:09:41 -0700220 /**
221 * Add routing rules to forward packets to known hosts
222 *
223 * @param sw Switch
224 * @param hostIp Host IP address to forwards packets to
225 */
Sangho Shin79c8d452014-09-18 09:50:21 -0700226 private void addRouteToHost(Switch sw, int destinationAddress, byte[] destinationMacAddress) {
Sangho Shin2f263692014-09-15 14:09:41 -0700227
228 IOFSwitch ofSwitch = floodlightProvider.getMasterSwitch(sw.getDpid().value());
229 OFFactory factory = ofSwitch.getFactory();
Sangho Shin2f263692014-09-15 14:09:41 -0700230
Sangho Shin2f263692014-09-15 14:09:41 -0700231
232 OFOxmEthType ethTypeIp = factory.oxms()
233 .ethType(EthType.IPv4);
234 OFOxmIpv4DstMasked ipPrefix = factory.oxms()
235 .ipv4DstMasked(
236 IPv4Address.of(destinationAddress),
237 IPv4Address.NO_MASK); // host addr should be /32
238 OFOxmList oxmListSlash32 = OFOxmList.of(ethTypeIp, ipPrefix);
239 OFMatchV3 match = factory.buildMatchV3()
240 .setOxmList(oxmListSlash32).build();
241 OFAction setDmac = null;
242 OFOxmEthDst dmac = factory.oxms()
243 .ethDst(MacAddress.of(destinationMacAddress));
244 setDmac = factory.actions().buildSetField()
245 .setField(dmac).build();
246
247 OFAction decTtl = factory.actions().decNwTtl();
248
249 // Set the source MAC address with the switch MAC address
250 String switchMacAddress = sw.getStringAttribute("routerMac");
251 OFOxmEthSrc srcAddr = factory.oxms().ethSrc(MacAddress.of(switchMacAddress));
252 OFAction setSA = factory.actions().buildSetField()
253 .setField(srcAddr).build();
254
255 List<OFAction> actionList = new ArrayList<OFAction>();
256 actionList.add(setDmac);
257 actionList.add(decTtl);
258 actionList.add(setSA);
259
260
261 /* TODO : need to check the config file for all packets
262 String subnets = sw.getStringAttribute("subnets");
263 try {
264 JSONArray arry = new JSONArray(subnets);
265 for (int i = 0; i < arry.length(); i++) {
266 String subnetIp = (String) arry.getJSONObject(i).get("subnetIp");
267 int portNo = (int) arry.getJSONObject(i).get("portNo");
268
269 if (netMatch(subnetIp, IPv4Address.of(hostIp.getDestinationAddress()).toString())) {
270 OFAction out = factory.actions().buildOutput()
271 .setPort(OFPort.of(portNo)).build();
272 actionList.add(out);
273 }
274 }
275 } catch (JSONException e) {
276 // TODO Auto-generated catch block
277 e.printStackTrace();
278 }
279 */
280
281 // Set output port
282 net.onrc.onos.core.topology.Host host = mutableTopology.getHostByMac(MACAddress.valueOf(destinationMacAddress));
283 if (host != null) {
284 for (Port port: host.getAttachmentPoints()) {
285 OFAction out = factory.actions().buildOutput()
286 .setPort(OFPort.of(port.getPortNumber().shortValue())).build();
287 actionList.add(out);
288 }
289 }
290
291 OFInstruction writeInstr = factory.instructions().buildWriteActions()
292 .setActions(actionList).build();
293
294 List<OFInstruction> instructions = new ArrayList<OFInstruction>();
295 instructions.add(writeInstr);
296
297 OFMessage myIpEntry = factory.buildFlowAdd()
298 .setTableId(TableId.of(TABLE_IPv4_UNICAST))
299 .setMatch(match)
300 .setInstructions(instructions)
301 .setPriority(MAX_PRIORITY)
302 .setBufferId(OFBufferId.NO_BUFFER)
303 .setIdleTimeout(0)
304 .setHardTimeout(0)
305 //.setXid(getNextTransactionId())
306 .build();
307
308 log.debug("Sending 'Routing information' OF message to the switch {}.", sw.getDpid().toString());
309
310 flowPusher.add(sw.getDpid(), myIpEntry);
311
Sangho Shinf41ac0a2014-09-19 09:50:30 -0700312 }
313
314 /**
315 * Add a new rule to VLAN table to forward packets from any port to the next table
316 * It is required to forward packets from controller to pipeline
317 *
318 * @param sw Switch the packet came from
319 */
320 private void addControlPortInVlanTable(Switch sw) {
321
322 if (added)
323 return;
324 else
325 added = true;
326
327 IOFSwitch ofSwitch = floodlightProvider.getMasterSwitch(sw.getDpid().value());
328 OFFactory factory = ofSwitch.getFactory();
329
330 //OFOxmInPort oxp = factory.oxms().inPort(p.getPortNo());
331 OFOxmVlanVid oxv = factory.oxms()
332 .vlanVid(OFVlanVidMatch.UNTAGGED);
333 OFOxmList oxmList = OFOxmList.of(oxv);
334
335
336 OFMatchV3 match = factory.buildMatchV3()
337 .setOxmList(oxmList)
338 .build();
339
340 OFInstruction gotoTbl = factory.instructions().buildGotoTable()
341 .setTableId(TableId.of(TABLE_TMAC)).build();
342 List<OFInstruction> instructions = new ArrayList<OFInstruction>();
343 // instructions.add(appAction);
344 instructions.add(gotoTbl);
345 OFMessage flowEntry = factory.buildFlowAdd()
346 .setTableId(TableId.of(TABLE_VLAN))
347 .setMatch(match)
348 .setInstructions(instructions)
349 .setPriority(1000) // does not matter - all rules
350 // exclusive
351 .setBufferId(OFBufferId.NO_BUFFER)
352 .setIdleTimeout(0)
353 .setHardTimeout(0)
354 //.setXid(getNextTransactionId())
355 .build();
356
357 flowPusher.add(sw.getDpid(), flowEntry);;
358 //log.debug("Adding {} vlan-rules in sw {}", msglist.size(), getStringId());
Sangho Shin2f263692014-09-15 14:09:41 -0700359
360 }
361
362}