blob: fc6c02e67bb64407e5efa60f85eefd6afa7ef1bd [file] [log] [blame]
Sangho Shin01bca862014-09-12 11:18:59 -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
Sangho Shin01bca862014-09-12 11:18:59 -070011import net.floodlightcontroller.core.IFloodlightProviderService;
Sangho Shin01bca862014-09-12 11:18:59 -070012import net.floodlightcontroller.core.module.FloodlightModuleContext;
Sangho Shin01bca862014-09-12 11:18:59 -070013import net.floodlightcontroller.util.MACAddress;
14import net.onrc.onos.api.packet.IPacketListener;
15import net.onrc.onos.api.packet.IPacketService;
16import net.onrc.onos.core.flowprogrammer.IFlowPusherService;
Sangho Shin01bca862014-09-12 11:18:59 -070017import net.onrc.onos.core.packet.ARP;
18import net.onrc.onos.core.packet.Ethernet;
19import net.onrc.onos.core.packet.IPv4;
Sangho Shin01bca862014-09-12 11:18:59 -070020import net.onrc.onos.core.topology.ITopologyService;
21import net.onrc.onos.core.topology.MutableTopology;
22import net.onrc.onos.core.topology.Port;
23import net.onrc.onos.core.topology.Switch;
24import net.onrc.onos.core.util.SwitchPort;
25
Sangho Shin01bca862014-09-12 11:18:59 -070026import org.projectfloodlight.openflow.types.IPv4Address;
Sangho Shin01bca862014-09-12 11:18:59 -070027import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
30import com.esotericsoftware.minlog.Log;
31
32/**
33 * Handling ARP requests to switches for Segment Routing.
34 * <p/>
35 * The module is for handling ARP requests to switches. It sends ARP response for any known
36 * hosts to the controllers.
37 * TODO: need to check the network config file for all hosts and packets
38 */
Sangho Shin2f263692014-09-15 14:09:41 -070039public class ArpHandler implements IPacketListener {
Sangho Shin01bca862014-09-12 11:18:59 -070040
41 private static final Logger log = LoggerFactory
42 .getLogger(ArpHandler.class);
43
44 private IFloodlightProviderService floodlightProvider;
45 private IPacketService packetService;
46 private IFlowPusherService flowPusher;
47 private ITopologyService topologyService;
48 private MutableTopology mutableTopology;
Sangho Shin2f263692014-09-15 14:09:41 -070049 //private List<ArpEntry> arpEntries;
50 private SegmentRoutingManager srManager;
Sangho Shin01bca862014-09-12 11:18:59 -070051
52 private static final short IDLE_TIMEOUT = 0;
53 private static final short HARD_TIMEOUT = 0;
54
55 private static final int TABLE_VLAN = 0;
56 private static final int TABLE_TMAC = 1;
57 private static final int TABLE_IPv4_UNICAST = 2;
58 private static final int TABLE_MPLS = 3;
59 private static final int TABLE_META = 4;
60 private static final int TABLE_ACL = 5;
61
62 private static final short MAX_PRIORITY = (short) 0xffff;
63 private static final short SLASH_24_PRIORITY = (short) 0xfff0;
64 private static final short SLASH_16_PRIORITY = (short) 0xff00;
65 private static final short SLASH_8_PRIORITY = (short) 0xf000;
66 private static final short MIN_PRIORITY = 0x0;
67
68
Sangho Shin2f263692014-09-15 14:09:41 -070069 /*
70 * Default Constructor
71 */
72 public ArpHandler(FloodlightModuleContext context, SegmentRoutingManager segmentRoutingManager) {
Sangho Shin01bca862014-09-12 11:18:59 -070073
Sangho Shin01bca862014-09-12 11:18:59 -070074 this.floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
75 this.packetService = context.getServiceImpl(IPacketService.class);
76 this.flowPusher = context.getServiceImpl(IFlowPusherService.class);
77 this.topologyService = context.getServiceImpl(ITopologyService.class);
Sangho Shin2f263692014-09-15 14:09:41 -070078 this.srManager = segmentRoutingManager;
79 this.mutableTopology = topologyService.getTopology();
80
81 packetService.registerPacketListener(this);
82 //arpEntries = new ArrayList<ArpEntry>();
Sangho Shin01bca862014-09-12 11:18:59 -070083
84 Log.debug("Arp Handler is initialized");
85
86 }
87
88 @Override
Sangho Shin01bca862014-09-12 11:18:59 -070089 public void receive(Switch sw, Port inPort, Ethernet payload) {
90 log.debug("Received a packet {} from sw {} ", payload.toString(), sw.getDpid());
91
92 if (payload.getEtherType() == Ethernet.TYPE_ARP) {
93
94 ARP arp = (ARP)payload.getPayload();
Sangho Shin2f263692014-09-15 14:09:41 -070095 srManager.updateArpCache(arp);
Sangho Shin01bca862014-09-12 11:18:59 -070096
97 if (arp.getOpCode() == ARP.OP_REQUEST) {
98
99 handleArpRequest(sw, inPort, arp);
100 }
101
102 }
Sangho Shin01bca862014-09-12 11:18:59 -0700103
104 }
105
Sangho Shin01bca862014-09-12 11:18:59 -0700106
107 /**
108 * Send an ARP response for the ARP request to the known switches
109 *
110 * @param sw Switch
111 * @param inPort port to send ARP response to
112 * @param arpRequest ARP request packet to handle
113 */
114 private void handleArpRequest(Switch sw, Port inPort, ARP arpRequest) {
115
116 String switchIpAddressSlash = sw.getStringAttribute("routerIp");
117 String switchMacAddressStr = sw.getStringAttribute("routerMac");
118 if (switchIpAddressSlash != null && switchMacAddressStr != null) {
119
120 String switchIpAddressStr = switchIpAddressSlash.substring(0, switchIpAddressSlash.indexOf('/'));
121 IPv4Address switchIpAddress = IPv4Address.of(switchIpAddressStr);
122 IPv4Address targetProtocolAddress = IPv4Address.of(arpRequest.getTargetProtocolAddress());
123 if (targetProtocolAddress.equals(switchIpAddress)) {
124 MACAddress targetMac = MACAddress.valueOf(switchMacAddressStr);
125
126 ARP arpReply = new ARP();
127 arpReply.setHardwareType(ARP.HW_TYPE_ETHERNET)
128 .setProtocolType(ARP.PROTO_TYPE_IP)
129 .setHardwareAddressLength(
130 (byte) Ethernet.DATALAYER_ADDRESS_LENGTH)
131 .setProtocolAddressLength((byte) IPv4.ADDRESS_LENGTH)
132 .setOpCode(ARP.OP_REPLY)
133 .setSenderHardwareAddress(targetMac.toBytes())
134 .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress())
135 .setTargetHardwareAddress(arpRequest.getSenderHardwareAddress())
136 .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress());
137
138 Ethernet eth = new Ethernet();
139 eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress())
140 .setSourceMACAddress(targetMac.toBytes())
141 .setEtherType(Ethernet.TYPE_ARP).setPayload(arpReply);
142
143 packetService.sendPacket(eth, new SwitchPort(sw.getDpid(), inPort.getPortNumber()));
144 }
145 }
146 }
147
Sangho Shin01bca862014-09-12 11:18:59 -0700148
Sangho Shin01bca862014-09-12 11:18:59 -0700149
150
151
152
153}