blob: fe4e6e3e122362bc90843389d11faec4b879a88f [file] [log] [blame]
Srikanth Vavilapalli7f479d92014-09-24 13:36:46 -07001/*******************************************************************************
2 * Copyright (c) 2014 Open Networking Laboratory.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Apache License v2.0
5 * which accompanies this distribution, and is available at
6 * http://www.apache.org/licenses/LICENSE-2.0
7 ******************************************************************************/
8
9package net.onrc.onos.apps.segmentrouting;
10
11import java.util.ArrayList;
12import java.util.Iterator;
13import java.util.List;
14
15import net.floodlightcontroller.core.IFloodlightProviderService;
16import net.floodlightcontroller.core.IOFSwitch;
17import net.floodlightcontroller.core.module.FloodlightModuleContext;
18import net.floodlightcontroller.util.MACAddress;
19import net.onrc.onos.api.packet.IPacketListener;
20import net.onrc.onos.api.packet.IPacketService;
21import net.onrc.onos.core.flowprogrammer.IFlowPusherService;
22import net.onrc.onos.core.packet.ARP;
23import net.onrc.onos.core.packet.Ethernet;
24import net.onrc.onos.core.packet.IPv4;
25import net.onrc.onos.core.topology.ITopologyService;
26import net.onrc.onos.core.topology.MutableTopology;
27import net.onrc.onos.core.topology.Port;
28import net.onrc.onos.core.topology.Switch;
29
30import org.json.JSONArray;
31import org.json.JSONException;
32import org.projectfloodlight.openflow.protocol.OFFactory;
33import org.projectfloodlight.openflow.protocol.OFPacketOut;
34import org.projectfloodlight.openflow.protocol.action.OFAction;
35import org.projectfloodlight.openflow.types.IPv4Address;
36import org.projectfloodlight.openflow.types.MacAddress;
37import org.projectfloodlight.openflow.types.OFPort;
38import org.projectfloodlight.openflow.types.U32;
39import org.slf4j.Logger;
40import org.slf4j.LoggerFactory;
41
42import com.esotericsoftware.minlog.Log;
43
44/**
45 * Handling ARP requests to switches for Segment Routing.
46 * <p/>
47 * The module is for handling ARP requests to switches. It sends ARP response for any known
48 * hosts to the controllers.
49 * TODO: need to check the network config file for all hosts and packets
50 */
51public class ArpHandler implements IPacketListener {
52
53 private static final Logger log = LoggerFactory
54 .getLogger(ArpHandler.class);
55
56 private IFloodlightProviderService floodlightProvider;
57 private IPacketService packetService;
58 private IFlowPusherService flowPusher;
59 private ITopologyService topologyService;
60 private MutableTopology mutableTopology;
61 //private List<ArpEntry> arpEntries;
62 private SegmentRoutingManager srManager;
63
64 private static final short IDLE_TIMEOUT = 0;
65 private static final short HARD_TIMEOUT = 0;
66
67 private static final int TABLE_VLAN = 0;
68 private static final int TABLE_TMAC = 1;
69 private static final int TABLE_IPv4_UNICAST = 2;
70 private static final int TABLE_MPLS = 3;
71 private static final int TABLE_META = 4;
72 private static final int TABLE_ACL = 5;
73
74 private static final short MAX_PRIORITY = (short) 0xffff;
75 private static final short SLASH_24_PRIORITY = (short) 0xfff0;
76 private static final short SLASH_16_PRIORITY = (short) 0xff00;
77 private static final short SLASH_8_PRIORITY = (short) 0xf000;
78 private static final short MIN_PRIORITY = 0x0;
79
80
81 /*
82 * Default Constructor
83 */
84 public ArpHandler(FloodlightModuleContext context, SegmentRoutingManager segmentRoutingManager) {
85
86 this.floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
87 this.packetService = context.getServiceImpl(IPacketService.class);
88 this.flowPusher = context.getServiceImpl(IFlowPusherService.class);
89 this.topologyService = context.getServiceImpl(ITopologyService.class);
90 this.srManager = segmentRoutingManager;
91 this.mutableTopology = topologyService.getTopology();
92
93 packetService.registerPacketListener(this);
94 //arpEntries = new ArrayList<ArpEntry>();
95
96 Log.debug("Arp Handler is initialized");
97
98 }
99
100 @Override
101 public void receive(Switch sw, Port inPort, Ethernet payload) {
102 log.debug("Received a packet {} from sw {} ", payload.toString(), sw.getDpid());
103
104 if (payload.getEtherType() == Ethernet.TYPE_ARP) {
105
106 ARP arp = (ARP)payload.getPayload();
107 srManager.updateArpCache(arp);
108
109 if (arp.getOpCode() == ARP.OP_REQUEST) {
110 handleArpRequest(sw, inPort, arp);
111 }
112 else {
113 byte[] senderMacAddressByte = arp.getSenderHardwareAddress();
114 String targetMacAddressStr = MacAddress.of(senderMacAddressByte).toString();
115 if (targetMacAddressStr.equals(sw.getStringAttribute("routerMac"))) {
116 IPv4Address hostIpAddress = IPv4Address.of(arp.getSenderProtocolAddress());
117 srManager.addRouteToHost(sw,hostIpAddress.getInt(), senderMacAddressByte);
118 }
119 }
120
121 }
122
123 }
124
125
126 /**
127 * Send an ARP response for the ARP request to the known switches
128 *
129 * @param sw Switch
130 * @param inPort port to send ARP response to
131 * @param arpRequest ARP request packet to handle
132 */
133 private void handleArpRequest(Switch sw, Port inPort, ARP arpRequest) {
134
135 List<String> subnetGatewayIPs = getSubnetGatewayIps(sw);
136 String switchMacAddressStr = sw.getStringAttribute("routerMac");
137 if (!subnetGatewayIPs.isEmpty()) {
138 IPv4Address targetProtocolAddress = IPv4Address.of(arpRequest.getTargetProtocolAddress());
139 // Do we have to check port also ??
140 if (subnetGatewayIPs.contains(targetProtocolAddress.toString())) {
141 MACAddress targetMac = MACAddress.valueOf(switchMacAddressStr);
142
143 ARP arpReply = new ARP();
144 arpReply.setHardwareType(ARP.HW_TYPE_ETHERNET)
145 .setProtocolType(ARP.PROTO_TYPE_IP)
146 .setHardwareAddressLength(
147 (byte) Ethernet.DATALAYER_ADDRESS_LENGTH)
148 .setProtocolAddressLength((byte) IPv4.ADDRESS_LENGTH)
149 .setOpCode(ARP.OP_REPLY)
150 .setSenderHardwareAddress(targetMac.toBytes())
151 .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress())
152 .setTargetHardwareAddress(arpRequest.getSenderHardwareAddress())
153 .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress());
154
155 Ethernet eth = new Ethernet();
156 eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress())
157 .setSourceMACAddress(targetMac.toBytes())
158 .setEtherType(Ethernet.TYPE_ARP).setPayload(arpReply);
159
160 sendPacketOut(sw, eth, inPort.getPortNumber().shortValue());
161 }
162 }
163 }
164
165 /**
166 * Retrieve Gateway IP address of all subnets defined in net config file
167 *
168 * @param sw Switch to retrieve subnet GW IPs for
169 * @return list of GW IP addresses for all subnets
170 */
171 private List<String> getSubnetGatewayIps(Switch sw) {
172
173 List<String> gatewayIps = new ArrayList<String>();
174
175 String subnets = sw.getStringAttribute("subnets");
176 try {
177 JSONArray arry = new JSONArray(subnets);
178 for (int i = 0; i < arry.length(); i++) {
179 String subnetIpSlash = (String) arry.getJSONObject(i).get("subnetIp");
180 if (subnetIpSlash != null) {
181 String subnetIp = subnetIpSlash.substring(0, subnetIpSlash.indexOf('/'));
182 gatewayIps.add(subnetIp);
183 }
184 }
185 } catch (JSONException e) {
186 // TODO Auto-generated catch block
187 e.printStackTrace();
188 }
189
190 return gatewayIps;
191 }
192
193 /**
194 * Send an ARP request
195 *
196 * @param sw Switch
197 * @param targetAddress Target IP address
198 * @param inPort Port to send the ARP request
199 *
200 */
201 public void sendArpRequest(Switch sw, int targetAddressInt, Port inPort) {
202
203 IPv4Address targetAddress = IPv4Address.of(targetAddressInt);
204 String senderMacAddressStr = sw.getStringAttribute("routerMac");
205 String senderIpAddressSlash = sw.getStringAttribute("routerIp");
206 if (senderMacAddressStr == null || senderIpAddressSlash == null)
207 return;
208 String senderIpAddressStr =
209 senderIpAddressSlash.substring(0, senderIpAddressSlash.indexOf('/'));
210 byte[] senderMacAddress = MacAddress.of(senderMacAddressStr).getBytes();
211 byte[] senderIpAddress = IPv4Address.of(senderIpAddressStr).getBytes();
212
213 ARP arpRequest = new ARP();
214 arpRequest.setHardwareType(ARP.HW_TYPE_ETHERNET)
215 .setProtocolType(ARP.PROTO_TYPE_IP)
216 .setHardwareAddressLength(
217 (byte) Ethernet.DATALAYER_ADDRESS_LENGTH)
218 .setProtocolAddressLength((byte) IPv4.ADDRESS_LENGTH)
219 .setOpCode(ARP.OP_REQUEST)
220 .setSenderHardwareAddress(senderMacAddress)
221 .setTargetHardwareAddress(MacAddress.NONE.getBytes())
222 .setSenderProtocolAddress(senderIpAddress)
223 .setTargetProtocolAddress(targetAddress.getBytes());
224
225 Ethernet eth = new Ethernet();
226 eth.setDestinationMACAddress(MacAddress.BROADCAST.getBytes())
227 .setSourceMACAddress(senderMacAddress)
228 .setEtherType(Ethernet.TYPE_ARP).setPayload(arpRequest);
229
230 sendPacketOut(sw, eth, (short)-1);
231
232 }
233
234 /**
235 * Send PACKET_OUT packet to switch
236 *
237 * @param sw Switch to send the packet to
238 * @param packet Packet to send
239 * @param switchPort port to send (if -1, broadcast)
240 */
241 private void sendPacketOut(Switch sw, Ethernet packet, short port) {
242
243 IOFSwitch ofSwitch = floodlightProvider.getMasterSwitch(sw.getDpid().value());
244 OFFactory factory = ofSwitch.getFactory();
245
246 List<OFAction> actions = new ArrayList<>();
247
248 if (port > 0) {
249 OFAction outport = factory.actions().output(OFPort.of(port), Short.MAX_VALUE);
250 actions.add(outport);
251 }
252 else {
253 Iterator<Port> iter = sw.getPorts().iterator();
254 while (iter.hasNext()) {
255 Port p = iter.next();
256 int pnum = p.getPortNumber().shortValue();
257 if (U32.of(pnum).compareTo(U32.of(OFPort.MAX.getPortNumber())) < 1) {
258 OFAction outport = factory.actions().output(OFPort.of(p.getNumber().shortValue()),
259 Short.MAX_VALUE);
260 actions.add(outport);
261 }
262 }
263 }
264
265 OFPacketOut po = factory.buildPacketOut()
266 .setData(packet.serialize())
267 .setActions(actions)
268 .build();
269
270 flowPusher.add(sw.getDpid(), po);
271 }
272
273}
274