Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 1 | package net.onrc.onos.apps.segmentrouting; |
| 2 | |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 3 | import java.io.IOException; |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 4 | import java.net.Inet4Address; |
| 5 | import java.net.InetAddress; |
| 6 | import java.net.UnknownHostException; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 7 | import java.util.ArrayList; |
| 8 | import java.util.Collection; |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 9 | import java.util.HashMap; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 10 | import java.util.Iterator; |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 11 | import java.util.LinkedList; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 12 | import java.util.List; |
| 13 | import java.util.Map; |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 14 | import java.util.Queue; |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 15 | import java.util.concurrent.ExecutionException; |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 16 | import java.util.concurrent.ScheduledExecutorService; |
| 17 | import java.util.concurrent.TimeUnit; |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 18 | import java.util.concurrent.TimeoutException; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 19 | |
| 20 | import net.floodlightcontroller.core.IFloodlightProviderService; |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 21 | import net.floodlightcontroller.core.IOF13Switch; |
Sangho Shin | 0df0198 | 2014-09-25 17:11:18 -0700 | [diff] [blame] | 22 | import net.floodlightcontroller.core.IOF13Switch.NeighborSet; |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 23 | import net.floodlightcontroller.core.internal.OFBarrierReplyFuture; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 24 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 25 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 26 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 27 | import net.floodlightcontroller.core.module.IFloodlightService; |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 28 | import net.floodlightcontroller.core.util.SingletonTask; |
| 29 | import net.floodlightcontroller.threadpool.IThreadPoolService; |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 30 | import net.onrc.onos.api.packet.IPacketListener; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 31 | import net.onrc.onos.api.packet.IPacketService; |
| 32 | import net.onrc.onos.core.flowprogrammer.IFlowPusherService; |
| 33 | import net.onrc.onos.core.main.config.IConfigInfoService; |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 34 | import net.onrc.onos.core.matchaction.MatchAction; |
| 35 | import net.onrc.onos.core.matchaction.MatchActionId; |
| 36 | import net.onrc.onos.core.matchaction.MatchActionOperationEntry; |
| 37 | import net.onrc.onos.core.matchaction.action.Action; |
| 38 | import net.onrc.onos.core.matchaction.action.CopyTtlInAction; |
| 39 | import net.onrc.onos.core.matchaction.action.CopyTtlOutAction; |
| 40 | import net.onrc.onos.core.matchaction.action.DecMplsTtlAction; |
| 41 | import net.onrc.onos.core.matchaction.action.DecNwTtlAction; |
| 42 | import net.onrc.onos.core.matchaction.action.GroupAction; |
| 43 | import net.onrc.onos.core.matchaction.action.PopMplsAction; |
| 44 | import net.onrc.onos.core.matchaction.action.PushMplsAction; |
| 45 | import net.onrc.onos.core.matchaction.action.SetMplsIdAction; |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 46 | import net.onrc.onos.core.matchaction.match.Ipv4Match; |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 47 | import net.onrc.onos.core.matchaction.match.Match; |
| 48 | import net.onrc.onos.core.matchaction.match.MplsMatch; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 49 | import net.onrc.onos.core.packet.ARP; |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 50 | import net.onrc.onos.core.packet.Ethernet; |
| 51 | import net.onrc.onos.core.packet.IPv4; |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 52 | import net.onrc.onos.core.topology.ITopologyListener; |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 53 | import net.onrc.onos.core.topology.ITopologyService; |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 54 | import net.onrc.onos.core.topology.LinkData; |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 55 | import net.onrc.onos.core.topology.MutableTopology; |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 56 | import net.onrc.onos.core.topology.Port; |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 57 | import net.onrc.onos.core.topology.PortData; |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 58 | import net.onrc.onos.core.topology.Switch; |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 59 | import net.onrc.onos.core.topology.TopologyEvents; |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 60 | import net.onrc.onos.core.util.Dpid; |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 61 | import net.onrc.onos.core.util.IPv4Net; |
| 62 | import net.onrc.onos.core.util.SwitchPort; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 63 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 64 | import org.json.JSONArray; |
| 65 | import org.json.JSONException; |
Saurav Das | bc594a4 | 2014-09-25 20:13:50 -0700 | [diff] [blame] | 66 | import org.projectfloodlight.openflow.types.EthType; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 67 | import org.projectfloodlight.openflow.types.IPv4Address; |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 68 | import org.projectfloodlight.openflow.util.HexString; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 69 | import org.slf4j.Logger; |
| 70 | import org.slf4j.LoggerFactory; |
| 71 | |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 72 | public class SegmentRoutingManager implements IFloodlightModule, |
Sangho Shin | ac5ee2b | 2014-09-28 21:27:20 -0700 | [diff] [blame] | 73 | ITopologyListener, IPacketListener { |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 74 | |
| 75 | private static final Logger log = LoggerFactory |
| 76 | .getLogger(SegmentRoutingManager.class); |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 77 | private ITopologyService topologyService; |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 78 | private IPacketService packetService; |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 79 | private MutableTopology mutableTopology; |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 80 | private Queue<IPv4> ipPacketQueue; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 81 | |
| 82 | private List<ArpEntry> arpEntries; |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 83 | private ArpHandler arpHandler; |
| 84 | private GenericIpHandler ipHandler; |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 85 | private IcmpHandler icmpHandler; |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 86 | private IThreadPoolService threadPool; |
| 87 | private SingletonTask discoveryTask; |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 88 | private IFloodlightProviderService floodlightProvider; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 89 | |
| 90 | @Override |
| 91 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 92 | // TODO Auto-generated method stub |
| 93 | return null; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
| 98 | // TODO Auto-generated method stub |
| 99 | return null; |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 104 | Collection<Class<? extends IFloodlightService>> l = new ArrayList<Class<? extends IFloodlightService>>(); |
| 105 | |
| 106 | l.add(IFloodlightProviderService.class); |
| 107 | l.add(IConfigInfoService.class); |
| 108 | l.add(ITopologyService.class); |
| 109 | l.add(IPacketService.class); |
| 110 | l.add(IFlowPusherService.class); |
| 111 | l.add(ITopologyService.class); |
| 112 | |
| 113 | return l; |
| 114 | |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public void init(FloodlightModuleContext context) throws FloodlightModuleException { |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 119 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 120 | arpHandler = new ArpHandler(context, this); |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 121 | icmpHandler = new IcmpHandler(context, this); |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 122 | ipHandler = new GenericIpHandler(context, this); |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 123 | arpEntries = new ArrayList<ArpEntry>(); |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 124 | topologyService = context.getServiceImpl(ITopologyService.class); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 125 | threadPool = context.getServiceImpl(IThreadPoolService.class); |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 126 | mutableTopology = topologyService.getTopology(); |
| 127 | topologyService.addListener(this, false); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 128 | ipPacketQueue = new LinkedList<IPv4>(); |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 129 | |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 130 | this.packetService = context.getServiceImpl(IPacketService.class); |
| 131 | packetService.registerPacketListener(this); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 132 | |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public void startUp(FloodlightModuleContext context) throws FloodlightModuleException { |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 137 | ScheduledExecutorService ses = threadPool.getScheduledExecutor(); |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 138 | |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 139 | discoveryTask = new SingletonTask(ses, new Runnable() { |
| 140 | @Override |
| 141 | public void run() { |
| 142 | populateEcmpRoutingRules(); |
| 143 | } |
| 144 | }); |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 147 | @Override |
| 148 | public void receive(Switch sw, Port inPort, Ethernet payload) { |
| 149 | if (payload.getEtherType() == Ethernet.TYPE_ARP) |
| 150 | arpHandler.processPacketIn(sw, inPort, payload); |
| 151 | if (payload.getEtherType() == Ethernet.TYPE_IPV4) { |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 152 | addPacket((IPv4)payload.getPayload()); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 153 | if (((IPv4)payload.getPayload()).getProtocol() == IPv4.PROTOCOL_ICMP) |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 154 | icmpHandler.processPacketIn(sw, inPort, payload); |
| 155 | else |
| 156 | ipHandler.processPacketIn(sw, inPort, payload); |
| 157 | } |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 158 | else { |
| 159 | log.debug("{}", payload.toString()); |
| 160 | } |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 161 | } |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 162 | /** |
| 163 | * Update ARP Cache using ARP packets |
| 164 | * It is used to set destination MAC address to forward packets to known hosts. |
| 165 | * But, it will be replace with Host information of Topology service later. |
| 166 | * |
| 167 | * @param arp APR packets to use for updating ARP entries |
| 168 | */ |
| 169 | public void updateArpCache(ARP arp) { |
| 170 | |
| 171 | ArpEntry arpEntry = new ArpEntry(arp.getSenderHardwareAddress(), arp.getSenderProtocolAddress()); |
| 172 | // TODO: Need to check the duplication |
| 173 | arpEntries.add(arpEntry); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Get MAC address to known hosts |
| 178 | * |
| 179 | * @param destinationAddress IP address to get MAC address |
| 180 | * @return MAC Address to given IP address |
| 181 | */ |
| 182 | public byte[] getMacAddressFromIpAddress(int destinationAddress) { |
| 183 | |
| 184 | // Can't we get the host IP address from the TopologyService ?? |
| 185 | |
| 186 | Iterator<ArpEntry> iterator = arpEntries.iterator(); |
| 187 | |
| 188 | IPv4Address ipAddress = IPv4Address.of(destinationAddress); |
| 189 | byte[] ipAddressInByte = ipAddress.getBytes(); |
| 190 | |
| 191 | while (iterator.hasNext() ) { |
| 192 | ArpEntry arpEntry = iterator.next(); |
| 193 | byte[] address = arpEntry.targetIpAddress; |
| 194 | |
| 195 | IPv4Address a = IPv4Address.of(address); |
| 196 | IPv4Address b = IPv4Address.of(ipAddressInByte); |
| 197 | |
| 198 | if ( a.equals(b)) { |
| 199 | log.debug("Found an arp entry"); |
| 200 | return arpEntry.targetMacAddress; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | return null; |
| 205 | } |
| 206 | |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 207 | /** |
| 208 | * Send an ARP request via ArpHandler |
| 209 | * @param destinationAddress |
| 210 | * @param sw |
| 211 | * @param inPort |
| 212 | * |
| 213 | */ |
| 214 | public void sendArpRequest(Switch sw, int destinationAddress, Port inPort) { |
| 215 | arpHandler.sendArpRequest(sw, destinationAddress, inPort); |
| 216 | } |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 217 | |
| 218 | /** |
| 219 | * Temporary class to to keep ARP entry |
| 220 | * |
| 221 | */ |
| 222 | private class ArpEntry { |
| 223 | |
| 224 | byte[] targetMacAddress; |
| 225 | byte[] targetIpAddress; |
| 226 | |
| 227 | private ArpEntry(byte[] macAddress, byte[] ipAddress) { |
| 228 | this.targetMacAddress = macAddress; |
| 229 | this.targetIpAddress = ipAddress; |
| 230 | } |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 231 | } |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 232 | |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 233 | /** |
| 234 | * Topology events that have been generated. |
| 235 | * |
| 236 | * @param topologyEvents the generated Topology Events |
| 237 | * @see TopologyEvents |
| 238 | */ |
| 239 | public void topologyEvents(TopologyEvents topologyEvents) |
| 240 | { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 241 | if ((topologyEvents.getAddedLinkDataEntries() != null) || |
| 242 | (topologyEvents.getRemovedLinkDataEntries() != null)) |
| 243 | { |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 244 | discoveryTask.reschedule(1, TimeUnit.SECONDS); |
| 245 | } |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 246 | |
| 247 | Collection<PortData> portEntries = |
| 248 | topologyEvents.getRemovedPortDataEntries(); |
| 249 | if (!portEntries.isEmpty()) { |
| 250 | // report port removal to the driver |
| 251 | for (PortData port: portEntries) { |
| 252 | Dpid dpid = port.getDpid(); |
| 253 | int portNo = (int) port.getPortNumber().value(); |
| 254 | log.debug("Remove port {} from switch {}", portNo, dpid.toString()); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | Collection<LinkData> linkEntries = |
| 259 | topologyEvents.getRemovedLinkDataEntries(); |
| 260 | if (!linkEntries.isEmpty()) { |
| 261 | for (LinkData link: linkEntries) { |
| 262 | Dpid srcSwDpid = link.getSrc().getDpid(); |
| 263 | Dpid dstSwDpid = link.getDst().getDpid(); |
| 264 | |
| 265 | Switch srcSwitch = mutableTopology.getSwitch(srcSwDpid); |
| 266 | Switch dstSwitch = mutableTopology.getSwitch(dstSwDpid); |
| 267 | |
| 268 | |
| 269 | } |
| 270 | } |
| 271 | |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 272 | } |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 273 | |
| 274 | /** |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 275 | * Populate routing rules walking through the ECMP shortest paths |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 276 | * |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 277 | */ |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 278 | private void populateEcmpRoutingRules() { |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 279 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 280 | Iterable<Switch> switches= mutableTopology.getSwitches(); |
| 281 | for (Switch sw : switches) { |
| 282 | ECMPShortestPathGraph ecmpSPG = new ECMPShortestPathGraph(sw); |
| 283 | log.debug("ECMPShortestPathGraph is computed for switch {}", |
| 284 | HexString.toHexString(sw.getDpid().value())); |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 285 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 286 | HashMap<Integer, HashMap<Switch,ArrayList<ArrayList<Dpid>>>> switchVia = |
| 287 | ecmpSPG.getAllLearnedSwitchesAndVia(); |
| 288 | for (Integer itrIdx: switchVia.keySet()){ |
| 289 | log.debug("ECMPShortestPathGraph:Switches learned in " |
| 290 | + "Iteration{} from switch {}:", |
| 291 | itrIdx, |
| 292 | HexString.toHexString(sw.getDpid().value())); |
| 293 | HashMap<Switch, ArrayList<ArrayList<Dpid>>> swViaMap = |
| 294 | switchVia.get(itrIdx); |
| 295 | for (Switch targetSw: swViaMap.keySet()){ |
| 296 | log.debug("ECMPShortestPathGraph:****switch {} via:", |
| 297 | HexString.toHexString(targetSw.getDpid().value())); |
| 298 | String destSw = sw.getDpid().toString(); |
| 299 | List<String> fwdToSw = new ArrayList<String>(); |
| 300 | |
| 301 | int i=0; |
| 302 | for (ArrayList<Dpid> via:swViaMap.get(targetSw)){ |
| 303 | log.debug("ECMPShortestPathGraph:******{}) {}",++i,via); |
| 304 | if (via.isEmpty()) { |
| 305 | fwdToSw.add(destSw); |
| 306 | } |
| 307 | else { |
| 308 | fwdToSw.add(via.get(0).toString()); |
| 309 | } |
| 310 | } |
| 311 | setRoutingRule(targetSw, destSw, fwdToSw); |
| 312 | } |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 313 | |
| 314 | // Send Barrier Message and make sure all rules are set |
| 315 | // before we set the rules to next routers |
| 316 | IOF13Switch sw13 = (IOF13Switch)floodlightProvider.getMasterSwitch( |
| 317 | getSwId(sw.getDpid().toString())); |
| 318 | try { |
| 319 | OFBarrierReplyFuture replyFuture = sw13.sendBarrier(); |
| 320 | replyFuture.get(10, TimeUnit.SECONDS); |
| 321 | } catch (IOException e) { |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 322 | e.printStackTrace(); |
| 323 | } |
| 324 | catch (InterruptedException | ExecutionException | TimeoutException e) { |
| 325 | log.error("Barrier message not received for sw: {}", sw.getDpid()); |
| 326 | e.printStackTrace(); |
| 327 | } |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 328 | } |
| 329 | } |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | /** |
| 333 | * |
| 334 | * Set routing rules in targetSw |
| 335 | * {forward packets to fwdToSw switches in order to send packets to destSw} |
| 336 | * - If the target switch is an edge router and final destnation switch is also |
| 337 | * an edge router, then set IP forwarding rules to subnets |
| 338 | * - If only the target switch is an edge router, then set IP forwarding rule to |
| 339 | * the transit router loopback IP address |
| 340 | * - If the target is a transit router, then just set the MPLS forwarding rule |
| 341 | * |
| 342 | * @param targetSw Switch to set the rules |
| 343 | * @param destSw Final destination switches |
| 344 | * @param fwdToSw next hop switches |
| 345 | */ |
| 346 | private void setRoutingRule(Switch targetSw, String destSw, List<String> fwdToSw) { |
| 347 | |
| 348 | |
| 349 | if (fwdToSw.isEmpty()) { |
| 350 | fwdToSw.add(destSw); |
| 351 | } |
| 352 | |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 353 | // if both target SW and dest SW are an edge router, then set IP table |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 354 | if (IsEdgeRouter(targetSw.getDpid().toString()) && |
| 355 | IsEdgeRouter(destSw)) { |
| 356 | // We assume that there is at least one transit router b/w edge routers |
| 357 | Switch destSwitch = mutableTopology.getSwitch(new Dpid(destSw)); |
| 358 | String subnets = destSwitch.getStringAttribute("subnets"); |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 359 | setIpTableRouterSubnet(targetSw, subnets, getMplsLabel(destSw) |
| 360 | ,fwdToSw); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 361 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 362 | String routerIp = destSwitch.getStringAttribute("routerIp"); |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 363 | setIpTableRouter(targetSw, routerIp, getMplsLabel(destSw), fwdToSw, null); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 364 | } |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 365 | // Only if the target switch is the edge router, then set the IP rules |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 366 | else if (IsEdgeRouter(targetSw.getDpid().toString())) { |
| 367 | // We assume that there is at least one transit router b/w edge routers |
| 368 | Switch destSwitch = mutableTopology.getSwitch(new Dpid(destSw)); |
| 369 | String routerIp = destSwitch.getStringAttribute("routerIp"); |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 370 | setIpTableRouter(targetSw, routerIp, getMplsLabel(destSw), fwdToSw, null); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 371 | } |
| 372 | // if it is a transit router, then set rules in the MPLS table |
| 373 | else { |
| 374 | setMplsTable(targetSw, getMplsLabel(destSw), fwdToSw); |
| 375 | } |
| 376 | |
| 377 | } |
| 378 | |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 379 | private void setIpTableRouterSubnet(Switch targetSw, String subnets, |
| 380 | String mplsLabel, List<String> fwdToSw) { |
| 381 | |
| 382 | Collection <MatchActionOperationEntry> entries = |
| 383 | new ArrayList<MatchActionOperationEntry>(); |
| 384 | |
| 385 | try { |
| 386 | JSONArray arry = new JSONArray(subnets); |
| 387 | for (int i = 0; i < arry.length(); i++) { |
| 388 | String subnetIp = (String) arry.getJSONObject(i).get("subnetIp"); |
| 389 | setIpTableRouter(targetSw, subnetIp, mplsLabel, fwdToSw, entries); |
| 390 | } |
| 391 | } catch (JSONException e) { |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 392 | e.printStackTrace(); |
| 393 | } |
| 394 | |
| 395 | if (!entries.isEmpty()) { |
| 396 | IOF13Switch sw13 = (IOF13Switch)floodlightProvider.getMasterSwitch( |
| 397 | getSwId(targetSw.getDpid().toString())); |
| 398 | |
| 399 | try { |
| 400 | sw13.pushFlows(entries); |
| 401 | } catch (IOException e) { |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 402 | e.printStackTrace(); |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | } |
| 407 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 408 | /** |
| 409 | * Check if the switch is the edge router or not |
| 410 | * If any subnet information is defined in the config file, the we assume |
| 411 | * it is an edge router |
| 412 | * |
| 413 | * @param dpid Dpid of the switch to check |
| 414 | * @return true if it is an edge router, otherwise false |
| 415 | */ |
| 416 | private boolean IsEdgeRouter(String dpid) { |
| 417 | |
| 418 | for (Switch sw: mutableTopology.getSwitches()) { |
| 419 | String dpidStr = sw.getDpid().toString(); |
| 420 | if (dpid.equals(dpidStr)) { |
| 421 | String subnetInfo = sw.getStringAttribute("subnets"); |
| 422 | if (subnetInfo == null || subnetInfo.equals("[]")) { |
| 423 | return false; |
| 424 | } |
| 425 | else |
| 426 | return true; |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | return false; |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * Set IP forwarding rule |
| 435 | * - If the destination is the next hop, then do not push MPLS, |
| 436 | * just decrease the NW TTL |
| 437 | * - Otherwise, push MPLS label and set the MPLS ID |
| 438 | * |
| 439 | * @param sw target switch to set rules |
| 440 | * @param subnetIp Match IP address |
| 441 | * @param mplsLabel MPLS label of final destination router |
| 442 | * @param fwdToSws next hop routers |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 443 | * @param entries |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 444 | */ |
| 445 | private void setIpTableRouter(Switch sw, String subnetIp, String mplsLabel, |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 446 | List<String> fwdToSws, Collection<MatchActionOperationEntry> entries) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 447 | |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 448 | Ipv4Match ipMatch = new Ipv4Match(subnetIp); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 449 | List<Action> actions = new ArrayList<>(); |
| 450 | |
| 451 | // If destination SW is the same as the fwd SW, then do not push MPLS label |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 452 | |
| 453 | if (fwdToSws.size() > 1) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 454 | PushMplsAction pushMplsAction = new PushMplsAction(); |
| 455 | SetMplsIdAction setIdAction = new SetMplsIdAction(Integer.parseInt(mplsLabel)); |
| 456 | CopyTtlOutAction copyTtlOutAction = new CopyTtlOutAction(); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 457 | DecMplsTtlAction decMplsTtlAction = new DecMplsTtlAction(1); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 458 | |
| 459 | actions.add(pushMplsAction); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 460 | actions.add(copyTtlOutAction); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 461 | actions.add(decMplsTtlAction); |
| 462 | actions.add(setIdAction); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 463 | } |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 464 | else { |
| 465 | String fwdToSw = fwdToSws.get(0); |
| 466 | if (getMplsLabel(fwdToSw).equals(mplsLabel)) { |
| 467 | DecNwTtlAction decTtlAction = new DecNwTtlAction(1); |
| 468 | actions.add(decTtlAction); |
| 469 | } |
| 470 | else { |
| 471 | PushMplsAction pushMplsAction = new PushMplsAction(); |
| 472 | SetMplsIdAction setIdAction = new SetMplsIdAction(Integer.parseInt(mplsLabel)); |
| 473 | CopyTtlOutAction copyTtlOutAction = new CopyTtlOutAction(); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 474 | DecMplsTtlAction decMplsTtlAction = new DecMplsTtlAction(1); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 475 | |
| 476 | actions.add(pushMplsAction); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 477 | actions.add(copyTtlOutAction); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 478 | actions.add(decMplsTtlAction); |
| 479 | actions.add(setIdAction); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 480 | } |
| 481 | } |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 482 | |
| 483 | GroupAction groupAction = new GroupAction(); |
| 484 | |
| 485 | for (String fwdSw : fwdToSws) { |
| 486 | groupAction.addSwitch(new Dpid(fwdSw)); |
| 487 | } |
| 488 | actions.add(groupAction); |
| 489 | |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 490 | // TODO: Mactch Action Id should be set correctly |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 491 | MatchAction matchAction = new MatchAction(new MatchActionId(0), |
| 492 | new SwitchPort((long)0,(short)0), ipMatch, actions); |
| 493 | |
| 494 | MatchActionOperationEntry maEntry = |
| 495 | new MatchActionOperationEntry( |
| 496 | net.onrc.onos.core.matchaction.MatchActionOperations.Operator.ADD, |
| 497 | matchAction); |
| 498 | |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 499 | IOF13Switch sw13 = (IOF13Switch)floodlightProvider.getMasterSwitch( |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 500 | getSwId(sw.getDpid().toString())); |
| 501 | |
| 502 | try { |
| 503 | printMatchActionOperationEntry(sw, maEntry); |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 504 | if (entries != null) |
| 505 | entries.add(maEntry); |
| 506 | else |
| 507 | sw13.pushFlow(maEntry); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 508 | } catch (IOException e) { |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 509 | e.printStackTrace(); |
| 510 | } |
| 511 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | |
Sangho Shin | ac5ee2b | 2014-09-28 21:27:20 -0700 | [diff] [blame] | 515 | /** |
| 516 | * Convert a string DPID to its Switch Id (integer) |
| 517 | * |
| 518 | * @param dpid |
| 519 | * @return |
| 520 | */ |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 521 | private long getSwId(String dpid) { |
| 522 | |
| 523 | long swId = 0; |
| 524 | |
| 525 | String swIdStr = dpid.substring(dpid.lastIndexOf(":")+1); |
| 526 | if (swIdStr != null) |
| 527 | swId = Integer.parseInt(swIdStr); |
| 528 | |
| 529 | return swId; |
| 530 | } |
| 531 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 532 | /** |
| 533 | * Set MPLS forwarding rules to MPLS table |
| 534 | * - If the destination is the same as the next hop to forward packets |
| 535 | * then, pop the MPLS label according to PHP rule |
| 536 | * - Otherwise, just forward packets to next hops using Group action |
| 537 | * |
| 538 | * @param sw Switch to set the rules |
| 539 | * @param mplsLabel destination MPLS label |
| 540 | * @param fwdSws next hop switches |
| 541 | */ |
| 542 | private void setMplsTable(Switch sw, String mplsLabel, List<String> fwdSws) { |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 543 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 544 | MplsMatch mplsMatch = new MplsMatch(Integer.parseInt(mplsLabel)); |
| 545 | |
| 546 | List<Action> actions = new ArrayList<Action>(); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 547 | |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 548 | // If the destination is the same as the next hop, then pop MPLS |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 549 | // Otherwise, just decrease the MPLS TTL. |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 550 | if (fwdSws.size() == 1) { |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 551 | String fwdMplsId = getMplsLabel(fwdSws.get(0)); |
| 552 | if (fwdMplsId.equals(mplsLabel)) { |
| 553 | String fwdSw = fwdSws.get(0); |
| 554 | if (mplsLabel.equals(getMplsLabel(fwdSw))) { |
| 555 | PopMplsAction popAction = new PopMplsAction(EthType.IPv4); |
| 556 | CopyTtlInAction copyTtlInAction = new CopyTtlInAction(); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 557 | DecNwTtlAction decNwTtlAction = new DecNwTtlAction(1); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 558 | |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 559 | actions.add(copyTtlInAction); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 560 | actions.add(popAction); |
| 561 | actions.add(decNwTtlAction); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 562 | } |
| 563 | } |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 564 | else { |
| 565 | DecMplsTtlAction decMplsTtlAction = new DecMplsTtlAction(1); |
| 566 | actions.add(decMplsTtlAction); |
| 567 | } |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 568 | } |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 569 | GroupAction groupAction = new GroupAction(); |
| 570 | for (String fwdSw: fwdSws) |
| 571 | groupAction.addSwitch(new Dpid(fwdSw)); |
| 572 | actions.add(groupAction); |
| 573 | |
| 574 | MatchAction matchAction = new MatchAction(new MatchActionId(0), |
| 575 | new SwitchPort((long)0,(short)0), mplsMatch, actions); |
| 576 | |
| 577 | MatchActionOperationEntry maEntry = |
| 578 | new MatchActionOperationEntry( |
| 579 | net.onrc.onos.core.matchaction.MatchActionOperations.Operator.ADD, |
| 580 | matchAction); |
| 581 | |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 582 | IOF13Switch sw13 = (IOF13Switch)floodlightProvider.getMasterSwitch( |
| 583 | getSwId(sw.getDpid().toString())); |
| 584 | |
| 585 | try { |
| 586 | printMatchActionOperationEntry(sw, maEntry); |
| 587 | sw13.pushFlow(maEntry); |
| 588 | } catch (IOException e) { |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 589 | e.printStackTrace(); |
| 590 | } |
| 591 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | |
| 595 | /** |
| 596 | * Debugging function to print out the Match Action Entry |
| 597 | * |
| 598 | * @param maEntry |
| 599 | */ |
Sangho Shin | ac5ee2b | 2014-09-28 21:27:20 -0700 | [diff] [blame] | 600 | private void printMatchActionOperationEntry(Switch sw, |
| 601 | MatchActionOperationEntry maEntry) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 602 | |
Sangho Shin | 0df0198 | 2014-09-25 17:11:18 -0700 | [diff] [blame] | 603 | StringBuilder logStr = new StringBuilder("In switch " + sw.getDpid() + ", "); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 604 | |
| 605 | MatchAction ma = maEntry.getTarget(); |
| 606 | Match m = ma.getMatch(); |
| 607 | List<Action> actions = ma.getActions(); |
| 608 | |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 609 | if (m instanceof Ipv4Match) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 610 | logStr.append("If the IP matches with "); |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 611 | IPv4Net ip = ((Ipv4Match) m).getDestination(); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 612 | logStr.append(ip.toString()); |
| 613 | logStr.append(" then "); |
| 614 | } |
| 615 | else if (m instanceof MplsMatch) { |
| 616 | logStr.append("If the MPLS label matches with "); |
| 617 | int mplsLabel = ((MplsMatch) m).getMplsLabel(); |
| 618 | logStr.append(mplsLabel); |
| 619 | logStr.append(" then "); |
| 620 | } |
| 621 | |
| 622 | logStr.append(" do { "); |
| 623 | for (Action action: actions) { |
| 624 | if (action instanceof CopyTtlInAction) { |
| 625 | logStr.append("copy ttl In, "); |
| 626 | } |
| 627 | else if (action instanceof CopyTtlOutAction) { |
| 628 | logStr.append("copy ttl Out, "); |
| 629 | } |
| 630 | else if (action instanceof DecMplsTtlAction) { |
| 631 | logStr.append("Dec MPLS TTL , "); |
| 632 | } |
| 633 | else if (action instanceof GroupAction) { |
| 634 | logStr.append("Forward packet to < "); |
Sangho Shin | 0df0198 | 2014-09-25 17:11:18 -0700 | [diff] [blame] | 635 | NeighborSet dpids = ((GroupAction)action).getDpids(); |
| 636 | logStr.append(dpids.toString() + ","); |
| 637 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 638 | } |
| 639 | else if (action instanceof PopMplsAction) { |
| 640 | logStr.append("Pop MPLS label, "); |
| 641 | } |
| 642 | else if (action instanceof PushMplsAction) { |
| 643 | logStr.append("Push MPLS label, "); |
| 644 | } |
| 645 | else if (action instanceof SetMplsIdAction) { |
| 646 | int id = ((SetMplsIdAction)action).getMplsId(); |
| 647 | logStr.append("Set MPLS ID as " + id + ", "); |
| 648 | |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | log.debug(logStr.toString()); |
| 653 | |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | /** |
| 657 | * Get MPLS label reading the config file |
| 658 | * |
| 659 | * @param dipid DPID of the switch |
| 660 | * @return MPLS label for the switch |
| 661 | */ |
| 662 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 663 | private String getMplsLabel(String dpid) { |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 664 | |
| 665 | String mplsLabel = null; |
| 666 | for (Switch sw: mutableTopology.getSwitches()) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 667 | String dpidStr = sw.getDpid().toString(); |
| 668 | if (dpid.equals(dpidStr)) { |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 669 | mplsLabel = sw.getStringAttribute("nodeSid"); |
| 670 | break; |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 674 | return mplsLabel; |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 675 | } |
| 676 | |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 677 | |
| 678 | |
| 679 | /** |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 680 | * The function checks if given IP matches to the given subnet mask |
| 681 | * |
| 682 | * @param addr - subnet address to match |
| 683 | * @param addr1 - IP address to check |
| 684 | * @return true if the IP address matches to the subnet, otherwise false |
| 685 | */ |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 686 | public boolean netMatch(String addr, String addr1){ //addr is subnet address and addr1 is ip address. Function will return true, if addr1 is within addr(subnet) |
| 687 | |
| 688 | String[] parts = addr.split("/"); |
| 689 | String ip = parts[0]; |
| 690 | int prefix; |
| 691 | |
| 692 | if (parts.length < 2) { |
| 693 | prefix = 0; |
| 694 | } else { |
| 695 | prefix = Integer.parseInt(parts[1]); |
| 696 | } |
| 697 | |
| 698 | Inet4Address a =null; |
| 699 | Inet4Address a1 =null; |
| 700 | try { |
| 701 | a = (Inet4Address) InetAddress.getByName(ip); |
| 702 | a1 = (Inet4Address) InetAddress.getByName(addr1); |
| 703 | } catch (UnknownHostException e){} |
| 704 | |
| 705 | byte[] b = a.getAddress(); |
| 706 | int ipInt = ((b[0] & 0xFF) << 24) | |
| 707 | ((b[1] & 0xFF) << 16) | |
| 708 | ((b[2] & 0xFF) << 8) | |
| 709 | ((b[3] & 0xFF) << 0); |
| 710 | |
| 711 | byte[] b1 = a1.getAddress(); |
| 712 | int ipInt1 = ((b1[0] & 0xFF) << 24) | |
| 713 | ((b1[1] & 0xFF) << 16) | |
| 714 | ((b1[2] & 0xFF) << 8) | |
| 715 | ((b1[3] & 0xFF) << 0); |
| 716 | |
| 717 | int mask = ~((1 << (32 - prefix)) - 1); |
| 718 | |
| 719 | if ((ipInt & mask) == (ipInt1 & mask)) { |
| 720 | return true; |
| 721 | } |
| 722 | else { |
| 723 | return false; |
| 724 | } |
| 725 | } |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 726 | |
Sangho Shin | ac5ee2b | 2014-09-28 21:27:20 -0700 | [diff] [blame] | 727 | /** |
| 728 | * Add a routing rule for the host |
| 729 | * |
| 730 | * @param sw - Switch to add the rule |
| 731 | * @param hostIpAddress Destination host IP address |
| 732 | * @param hostMacAddress Destination host MAC address |
| 733 | */ |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 734 | public void addRouteToHost(Switch sw, int hostIpAddress, byte[] hostMacAddress) { |
| 735 | ipHandler.addRouteToHost(sw, hostIpAddress, hostMacAddress); |
| 736 | |
| 737 | } |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 738 | |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 739 | /** |
| 740 | * Add IP packet to a buffer queue |
| 741 | * |
| 742 | * @param ipv4 |
| 743 | */ |
| 744 | public void addPacket(IPv4 ipv4) { |
| 745 | synchronized (ipPacketQueue) { |
| 746 | ipPacketQueue.add(ipv4); |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | /** |
| 751 | * Retrieve all packets whose destination is the given address. |
| 752 | * |
| 753 | * @param destIp Destination address of packets to retrieve |
| 754 | */ |
| 755 | public List<IPv4> getIpPacketFromQueue(byte[] destIp) { |
| 756 | |
| 757 | List<IPv4> bufferedPackets = new ArrayList<IPv4>(); |
| 758 | |
| 759 | synchronized (ipPacketQueue) { |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 760 | if (!ipPacketQueue.isEmpty()) { |
| 761 | for (IPv4 ip: ipPacketQueue) { |
| 762 | int dest = ip.getDestinationAddress(); |
| 763 | IPv4Address ip1 = IPv4Address.of(dest); |
| 764 | IPv4Address ip2 = IPv4Address.of(destIp); |
| 765 | if (ip1.equals(ip2)) { |
| 766 | bufferedPackets.add((IPv4)(ipPacketQueue.poll()).clone()); |
| 767 | } |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 768 | } |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | return bufferedPackets; |
| 773 | } |
| 774 | |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 775 | } |