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; |
| 11 | import java.util.List; |
| 12 | import java.util.Map; |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 13 | import java.util.concurrent.ConcurrentLinkedQueue; |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 14 | import java.util.concurrent.ExecutionException; |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 15 | import java.util.concurrent.ScheduledExecutorService; |
| 16 | import java.util.concurrent.TimeUnit; |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 17 | import java.util.concurrent.TimeoutException; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 18 | |
| 19 | import net.floodlightcontroller.core.IFloodlightProviderService; |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 20 | import net.floodlightcontroller.core.IOF13Switch; |
Sangho Shin | 0df0198 | 2014-09-25 17:11:18 -0700 | [diff] [blame] | 21 | import net.floodlightcontroller.core.IOF13Switch.NeighborSet; |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 22 | import net.floodlightcontroller.core.internal.OFBarrierReplyFuture; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 23 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 24 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 25 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 26 | import net.floodlightcontroller.core.module.IFloodlightService; |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 27 | import net.floodlightcontroller.core.util.SingletonTask; |
| 28 | import net.floodlightcontroller.threadpool.IThreadPoolService; |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 29 | import net.onrc.onos.api.packet.IPacketListener; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 30 | import net.onrc.onos.api.packet.IPacketService; |
| 31 | import net.onrc.onos.core.flowprogrammer.IFlowPusherService; |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 32 | import net.onrc.onos.core.intent.Path; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 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, |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -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 | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 80 | private ConcurrentLinkedQueue<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 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 90 | private HashMap<Switch, ECMPShortestPathGraph> graphs; |
| 91 | |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 92 | @Override |
| 93 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 94 | // TODO Auto-generated method stub |
| 95 | return null; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
| 100 | // TODO Auto-generated method stub |
| 101 | return null; |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 106 | Collection<Class<? extends IFloodlightService>> l = new ArrayList<Class<? extends IFloodlightService>>(); |
| 107 | |
| 108 | l.add(IFloodlightProviderService.class); |
| 109 | l.add(IConfigInfoService.class); |
| 110 | l.add(ITopologyService.class); |
| 111 | l.add(IPacketService.class); |
| 112 | l.add(IFlowPusherService.class); |
| 113 | l.add(ITopologyService.class); |
| 114 | |
| 115 | return l; |
| 116 | |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public void init(FloodlightModuleContext context) throws FloodlightModuleException { |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 121 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 122 | arpHandler = new ArpHandler(context, this); |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 123 | icmpHandler = new IcmpHandler(context, this); |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 124 | ipHandler = new GenericIpHandler(context, this); |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 125 | arpEntries = new ArrayList<ArpEntry>(); |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 126 | topologyService = context.getServiceImpl(ITopologyService.class); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 127 | threadPool = context.getServiceImpl(IThreadPoolService.class); |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 128 | mutableTopology = topologyService.getTopology(); |
| 129 | topologyService.addListener(this, false); |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 130 | ipPacketQueue = new ConcurrentLinkedQueue<IPv4>(); |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 131 | graphs = new HashMap<Switch, ECMPShortestPathGraph>(); |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 132 | |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 133 | this.packetService = context.getServiceImpl(IPacketService.class); |
| 134 | packetService.registerPacketListener(this); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 135 | |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public void startUp(FloodlightModuleContext context) throws FloodlightModuleException { |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 140 | ScheduledExecutorService ses = threadPool.getScheduledExecutor(); |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 141 | |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 142 | discoveryTask = new SingletonTask(ses, new Runnable() { |
| 143 | @Override |
| 144 | public void run() { |
| 145 | populateEcmpRoutingRules(); |
| 146 | } |
| 147 | }); |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 150 | @Override |
| 151 | public void receive(Switch sw, Port inPort, Ethernet payload) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 152 | if (payload.getEtherType() == Ethernet.TYPE_ARP) |
| 153 | arpHandler.processPacketIn(sw, inPort, payload); |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 154 | if (payload.getEtherType() == Ethernet.TYPE_IPV4) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 155 | addPacket((IPv4) payload.getPayload()); |
| 156 | if (((IPv4) payload.getPayload()).getProtocol() == IPv4.PROTOCOL_ICMP) |
| 157 | icmpHandler.processPacketIn(sw, inPort, payload); |
| 158 | else |
| 159 | ipHandler.processPacketIn(sw, inPort, payload); |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 160 | } |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 161 | else { |
| 162 | log.debug("{}", payload.toString()); |
| 163 | } |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 164 | } |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 165 | |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 166 | /** |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 167 | * Update ARP Cache using ARP packets It is used to set destination MAC |
| 168 | * address to forward packets to known hosts. But, it will be replace with |
| 169 | * Host information of Topology service later. |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 170 | * |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 171 | * @param arp APR packets to use for updating ARP entries |
| 172 | */ |
| 173 | public void updateArpCache(ARP arp) { |
| 174 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 175 | ArpEntry arpEntry = new ArpEntry(arp.getSenderHardwareAddress(), |
| 176 | arp.getSenderProtocolAddress()); |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 177 | // TODO: Need to check the duplication |
| 178 | arpEntries.add(arpEntry); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Get MAC address to known hosts |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 183 | * |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 184 | * @param destinationAddress IP address to get MAC address |
| 185 | * @return MAC Address to given IP address |
| 186 | */ |
| 187 | public byte[] getMacAddressFromIpAddress(int destinationAddress) { |
| 188 | |
| 189 | // Can't we get the host IP address from the TopologyService ?? |
| 190 | |
| 191 | Iterator<ArpEntry> iterator = arpEntries.iterator(); |
| 192 | |
| 193 | IPv4Address ipAddress = IPv4Address.of(destinationAddress); |
| 194 | byte[] ipAddressInByte = ipAddress.getBytes(); |
| 195 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 196 | while (iterator.hasNext()) { |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 197 | ArpEntry arpEntry = iterator.next(); |
| 198 | byte[] address = arpEntry.targetIpAddress; |
| 199 | |
| 200 | IPv4Address a = IPv4Address.of(address); |
| 201 | IPv4Address b = IPv4Address.of(ipAddressInByte); |
| 202 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 203 | if (a.equals(b)) { |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 204 | log.debug("Found an arp entry"); |
| 205 | return arpEntry.targetMacAddress; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | return null; |
| 210 | } |
| 211 | |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 212 | /** |
| 213 | * Send an ARP request via ArpHandler |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 214 | * |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 215 | * @param destinationAddress |
| 216 | * @param sw |
| 217 | * @param inPort |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 218 | * |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 219 | */ |
| 220 | public void sendArpRequest(Switch sw, int destinationAddress, Port inPort) { |
| 221 | arpHandler.sendArpRequest(sw, destinationAddress, inPort); |
| 222 | } |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 223 | |
| 224 | /** |
| 225 | * Temporary class to to keep ARP entry |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 226 | * |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 227 | */ |
| 228 | private class ArpEntry { |
| 229 | |
| 230 | byte[] targetMacAddress; |
| 231 | byte[] targetIpAddress; |
| 232 | |
| 233 | private ArpEntry(byte[] macAddress, byte[] ipAddress) { |
| 234 | this.targetMacAddress = macAddress; |
| 235 | this.targetIpAddress = ipAddress; |
| 236 | } |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 237 | } |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 238 | |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 239 | /** |
| 240 | * Topology events that have been generated. |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 241 | * |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 242 | * @param topologyEvents the generated Topology Events |
| 243 | * @see TopologyEvents |
| 244 | */ |
| 245 | public void topologyEvents(TopologyEvents topologyEvents) |
| 246 | { |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 247 | |
| 248 | Collection<LinkData> linkEntriesAdded = |
| 249 | topologyEvents.getAddedLinkDataEntries(); |
| 250 | if (!linkEntriesAdded.isEmpty()) { |
| 251 | processLinkAdd(linkEntriesAdded); |
| 252 | } |
| 253 | |
| 254 | Collection<PortData> PortEntriesAdded = |
| 255 | topologyEvents.getAddedPortDataEntries(); |
| 256 | if (linkEntriesAdded != null) { |
| 257 | processPortAdd(PortEntriesAdded); |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 258 | } |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 259 | |
| 260 | Collection<PortData> portEntries = |
| 261 | topologyEvents.getRemovedPortDataEntries(); |
| 262 | if (!portEntries.isEmpty()) { |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 263 | processPortRemoval(portEntries); |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 266 | Collection<LinkData> linkEntriesRemoved = |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 267 | topologyEvents.getRemovedLinkDataEntries(); |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 268 | if (!linkEntriesRemoved.isEmpty()) { |
| 269 | processLinkRemoval(linkEntriesRemoved); |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 270 | } |
| 271 | } |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 272 | |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 273 | /** |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 274 | * Report ports newly added to driver |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 275 | * |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 276 | * @param portEntries |
| 277 | */ |
| 278 | private void processPortAdd(Collection<PortData> portEntries) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 279 | // TODO: do we need to add ports slowly? |
| 280 | for (PortData port : portEntries) { |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 281 | Dpid dpid = port.getDpid(); |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 282 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 283 | IOF13Switch sw = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 284 | getSwId(port.getDpid().toString())); |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 285 | sw.addPortToGroups(port.getPortNumber()); |
| 286 | |
| 287 | log.debug("Add port {} to switch {}", port, dpid); |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Reports ports of new links to driver and recalculate ECMP SPG |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 293 | * |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 294 | * @param linkEntries |
| 295 | */ |
| 296 | private void processLinkAdd(Collection<LinkData> linkEntries) { |
| 297 | |
| 298 | // TODO: How to determine this link was broken before and back now |
| 299 | // If so, we need to ad the link slowly... |
| 300 | // Or, just add any new or restored link slowly ??? |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 301 | for (LinkData link : linkEntries) { |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 302 | SwitchPort srcPort = link.getSrc(); |
| 303 | SwitchPort dstPort = link.getDst(); |
| 304 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 305 | IOF13Switch srcSw = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 306 | getSwId(srcPort.getDpid().toString())); |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 307 | IOF13Switch dstSw = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 308 | getSwId(srcPort.getDpid().toString())); |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 309 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 310 | srcSw.addPortToGroups(srcPort.getPortNumber()); |
| 311 | dstSw.addPortToGroups(dstPort.getPortNumber()); |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 312 | } |
| 313 | discoveryTask.reschedule(1, TimeUnit.SECONDS); |
| 314 | } |
| 315 | |
| 316 | /** |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 317 | * Check if all links are gone b/w the two switches. If all links are gone, |
| 318 | * then we need to recalculate the path. Otherwise, just report link failure |
| 319 | * to the driver. |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 320 | * |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 321 | * @param linkEntries |
| 322 | */ |
| 323 | private void processLinkRemoval(Collection<LinkData> linkEntries) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 324 | for (LinkData link : linkEntries) { |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 325 | SwitchPort srcPort = link.getSrc(); |
| 326 | SwitchPort dstPort = link.getDst(); |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 327 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 328 | IOF13Switch srcSw = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 329 | getSwId(srcPort.getDpid().toString())); |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 330 | IOF13Switch dstSw = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 331 | getSwId(srcPort.getDpid().toString())); |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 332 | |
| 333 | // TODO: Why do we get null for the master switch?? |
| 334 | if (srcSw != null) |
| 335 | srcSw.removePortFromGroups(srcPort.getPortNumber()); |
| 336 | if (dstSw != null) |
| 337 | dstSw.removePortFromGroups(dstPort.getPortNumber()); |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 338 | |
| 339 | Switch srcSwitch = mutableTopology.getSwitch(srcPort.getDpid()); |
| 340 | if (srcSwitch.getLinkToNeighbor(dstPort.getDpid()) == null) { |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 341 | //discoveryTask.reschedule(1, TimeUnit.SECONDS); |
| 342 | modifyEcmpRoutingRules(link); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 343 | log.debug("All links are gone b/w {} and {}", srcPort.getDpid(), |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 344 | srcPort.getDpid()); |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 345 | } |
| 346 | } |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 347 | } |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 348 | |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 349 | /** |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 350 | * report ports removed to the driver immediately |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 351 | * |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 352 | * @param portEntries |
| 353 | */ |
| 354 | private void processPortRemoval(Collection<PortData> portEntries) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 355 | for (PortData port : portEntries) { |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 356 | Dpid dpid = port.getDpid(); |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 357 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 358 | IOF13Switch sw = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 359 | getSwId(port.getDpid().toString())); |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 360 | if (sw != null) |
| 361 | sw.removePortFromGroups(port.getPortNumber()); |
| 362 | log.debug("Remove port {} from switch {}", port, dpid); |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 363 | } |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 364 | } |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 365 | |
| 366 | /** |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 367 | * Populate routing rules walking through the ECMP shortest paths |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 368 | * |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 369 | */ |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 370 | private void populateEcmpRoutingRules() { |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 371 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 372 | Iterable<Switch> switches = mutableTopology.getSwitches(); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 373 | for (Switch sw : switches) { |
| 374 | ECMPShortestPathGraph ecmpSPG = new ECMPShortestPathGraph(sw); |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 375 | graphs.put(sw, ecmpSPG); |
| 376 | //log.debug("ECMPShortestPathGraph is computed for switch {}", |
| 377 | // HexString.toHexString(sw.getDpid().value())); |
| 378 | populateEcmpRoutingRulesForPath(sw, ecmpSPG); |
| 379 | } |
| 380 | } |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 381 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 382 | private void populateEcmpRoutingRulesForPath(Switch sw, |
| 383 | ECMPShortestPathGraph ecmpSPG) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 384 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 385 | HashMap<Integer, HashMap<Switch, ArrayList<ArrayList<Dpid>>>> switchVia = |
| 386 | ecmpSPG.getAllLearnedSwitchesAndVia(); |
| 387 | for (Integer itrIdx : switchVia.keySet()) { |
| 388 | //log.debug("ECMPShortestPathGraph:Switches learned in " |
| 389 | // + "Iteration{} from switch {}:", |
| 390 | // itrIdx, |
| 391 | // HexString.toHexString(sw.getDpid().value())); |
| 392 | HashMap<Switch, ArrayList<ArrayList<Dpid>>> swViaMap = |
| 393 | switchVia.get(itrIdx); |
| 394 | for (Switch targetSw : swViaMap.keySet()) { |
| 395 | log.debug("ECMPShortestPathGraph:****switch {} via:", |
| 396 | HexString.toHexString(targetSw.getDpid().value())); |
| 397 | String destSw = sw.getDpid().toString(); |
| 398 | List<String> fwdToSw = new ArrayList<String>(); |
| 399 | |
| 400 | int i = 0; |
| 401 | for (ArrayList<Dpid> via : swViaMap.get(targetSw)) { |
| 402 | log.debug("ECMPShortestPathGraph:******{}) {}", ++i, via); |
| 403 | if (via.isEmpty()) { |
| 404 | fwdToSw.add(destSw); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 405 | } |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 406 | else { |
| 407 | fwdToSw.add(via.get(0).toString()); |
| 408 | } |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 409 | } |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 410 | setRoutingRule(targetSw, destSw, fwdToSw); |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 411 | } |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 412 | |
| 413 | // Send Barrier Message and make sure all rules are set |
| 414 | // before we set the rules to next routers |
| 415 | IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch( |
| 416 | getSwId(sw.getDpid().toString())); |
| 417 | try { |
| 418 | OFBarrierReplyFuture replyFuture = sw13.sendBarrier(); |
| 419 | replyFuture.get(10, TimeUnit.SECONDS); |
| 420 | } catch (IOException e) { |
| 421 | e.printStackTrace(); |
| 422 | } catch (InterruptedException | ExecutionException | TimeoutException e) { |
| 423 | log.error("Barrier message not received for sw: {}", sw.getDpid()); |
| 424 | e.printStackTrace(); |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | } |
| 429 | |
| 430 | |
| 431 | private class SwitchPair { |
| 432 | private Switch src; |
| 433 | private Switch dst; |
| 434 | |
| 435 | public SwitchPair(Switch src, Switch dst) { |
| 436 | this.src = src; |
| 437 | this.dst = dst; |
| 438 | } |
| 439 | |
| 440 | public Switch getSource() { |
| 441 | return src; |
| 442 | } |
| 443 | |
| 444 | public Switch getDestination() { |
| 445 | return dst; |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 446 | } |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | /** |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 450 | * Modify the routing rules for the lost links |
| 451 | * - Recompute the path if the link failed is included in the path |
| 452 | * (including src and dest). |
| 453 | * |
| 454 | * @param newLink |
| 455 | */ |
| 456 | private void modifyEcmpRoutingRules(LinkData linkRemoved) { |
| 457 | |
| 458 | //HashMap<Switch, SwitchPair> linksToRecompute = new HashMap<Switch, SwitchPair>(); |
| 459 | List<SwitchPair> linksToRecompute = new ArrayList<SwitchPair>(); |
| 460 | |
| 461 | for (ECMPShortestPathGraph ecmpSPG : graphs.values()) { |
| 462 | Switch rootSw = ecmpSPG.getRootSwitch(); |
| 463 | HashMap<Integer, HashMap<Switch, ArrayList<Path>>> paths = |
| 464 | ecmpSPG.getCompleteLearnedSwitchesAndPaths(); |
| 465 | for (HashMap<Switch, ArrayList<Path>> p: paths.values()) { |
| 466 | for (Switch destSw: p.keySet()) { |
| 467 | ArrayList<Path> path = p.get(destSw); |
| 468 | if (checkPath(path, linkRemoved)) { |
| 469 | linksToRecompute.add(new SwitchPair(rootSw, destSw)); |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | // Recompute the path for the specific route |
| 476 | for (SwitchPair pair: linksToRecompute) { |
| 477 | |
| 478 | log.debug("Recompute path from {} to {}", pair.getSource(), pair.getDestination()); |
| 479 | // TODO : we need the following function |
| 480 | //ECMPShortestPathGraph ecmpSPG = |
| 481 | // new ECMPShortestPathGraph(pair.getSource(), pair.getDestination()); |
| 482 | ECMPShortestPathGraph ecmpSPG = |
| 483 | new ECMPShortestPathGraph(pair.getSource()); |
| 484 | // TODO: we need to use different function to MODIFY the rules |
| 485 | populateEcmpRoutingRulesForPath(pair.getSource(), ecmpSPG); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | /** |
| 490 | * Check if the path is affected from the link removed |
| 491 | * |
| 492 | * @param path Path to check |
| 493 | * @param linkRemoved link removed |
| 494 | * @return true if the path contains the link removed |
| 495 | */ |
| 496 | private boolean checkPath(ArrayList<Path> path, LinkData linkRemoved) { |
| 497 | |
| 498 | for (Path ppp: path) { |
| 499 | // TODO: need to check if this is a bidirectional or |
| 500 | // unidirectional |
| 501 | if (ppp.contains(linkRemoved)) { |
| 502 | return true; |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | return false; |
| 507 | } |
| 508 | |
| 509 | /** |
| 510 | * |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 511 | * Set routing rules in targetSw {forward packets to fwdToSw switches in |
| 512 | * order to send packets to destSw} - If the target switch is an edge router |
| 513 | * and final destnation switch is also an edge router, then set IP |
| 514 | * forwarding rules to subnets - If only the target switch is an edge |
| 515 | * router, then set IP forwarding rule to the transit router loopback IP |
| 516 | * address - If the target is a transit router, then just set the MPLS |
| 517 | * forwarding rule |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 518 | * |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 519 | * @param targetSw Switch to set the rules |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 520 | * @param destSw Final destination switches |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 521 | * @param fwdToSw next hop switches |
| 522 | */ |
| 523 | private void setRoutingRule(Switch targetSw, String destSw, List<String> fwdToSw) { |
| 524 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 525 | if (fwdToSw.isEmpty()) { |
| 526 | fwdToSw.add(destSw); |
| 527 | } |
| 528 | |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 529 | // 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] | 530 | if (IsEdgeRouter(targetSw.getDpid().toString()) && |
| 531 | IsEdgeRouter(destSw)) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 532 | // We assume that there is at least one transit router b/w edge |
| 533 | // routers |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 534 | Switch destSwitch = mutableTopology.getSwitch(new Dpid(destSw)); |
| 535 | String subnets = destSwitch.getStringAttribute("subnets"); |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 536 | setIpTableRouterSubnet(targetSw, subnets, getMplsLabel(destSw) |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 537 | , fwdToSw); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 538 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 539 | String routerIp = destSwitch.getStringAttribute("routerIp"); |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 540 | setIpTableRouter(targetSw, routerIp, getMplsLabel(destSw), fwdToSw, null); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 541 | } |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 542 | // 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] | 543 | else if (IsEdgeRouter(targetSw.getDpid().toString())) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 544 | // We assume that there is at least one transit router b/w edge |
| 545 | // routers |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 546 | Switch destSwitch = mutableTopology.getSwitch(new Dpid(destSw)); |
| 547 | String routerIp = destSwitch.getStringAttribute("routerIp"); |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 548 | setIpTableRouter(targetSw, routerIp, getMplsLabel(destSw), fwdToSw, null); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 549 | } |
| 550 | // if it is a transit router, then set rules in the MPLS table |
| 551 | else { |
| 552 | setMplsTable(targetSw, getMplsLabel(destSw), fwdToSw); |
| 553 | } |
| 554 | |
| 555 | } |
| 556 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 557 | /** |
| 558 | * Set IP forwarding rule to the gateway of each subnet of switches |
| 559 | * |
| 560 | * @param targetSw Switch to set rules |
| 561 | * @param subnets subnet information |
| 562 | * @param mplsLabel destination MPLS label |
| 563 | * @param fwdToSw router to forward packets to |
| 564 | */ |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 565 | private void setIpTableRouterSubnet(Switch targetSw, String subnets, |
| 566 | String mplsLabel, List<String> fwdToSw) { |
| 567 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 568 | Collection<MatchActionOperationEntry> entries = |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 569 | new ArrayList<MatchActionOperationEntry>(); |
| 570 | |
| 571 | try { |
| 572 | JSONArray arry = new JSONArray(subnets); |
| 573 | for (int i = 0; i < arry.length(); i++) { |
| 574 | String subnetIp = (String) arry.getJSONObject(i).get("subnetIp"); |
| 575 | setIpTableRouter(targetSw, subnetIp, mplsLabel, fwdToSw, entries); |
| 576 | } |
| 577 | } catch (JSONException e) { |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 578 | e.printStackTrace(); |
| 579 | } |
| 580 | |
| 581 | if (!entries.isEmpty()) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 582 | IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 583 | getSwId(targetSw.getDpid().toString())); |
| 584 | |
| 585 | try { |
| 586 | sw13.pushFlows(entries); |
| 587 | } catch (IOException e) { |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 588 | e.printStackTrace(); |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | } |
| 593 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 594 | /** |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 595 | * Check if the switch is the edge router or not If any subnet information |
| 596 | * is defined in the config file, the we assume it is an edge router |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 597 | * |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 598 | * @param dpid Dpid of the switch to check |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 599 | * @return true if it is an edge router, otherwise false |
| 600 | */ |
| 601 | private boolean IsEdgeRouter(String dpid) { |
| 602 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 603 | for (Switch sw : mutableTopology.getSwitches()) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 604 | String dpidStr = sw.getDpid().toString(); |
| 605 | if (dpid.equals(dpidStr)) { |
| 606 | String subnetInfo = sw.getStringAttribute("subnets"); |
| 607 | if (subnetInfo == null || subnetInfo.equals("[]")) { |
| 608 | return false; |
| 609 | } |
| 610 | else |
| 611 | return true; |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | return false; |
| 616 | } |
| 617 | |
| 618 | /** |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 619 | * Set IP forwarding rule - If the destination is the next hop, then do not |
| 620 | * push MPLS, just decrease the NW TTL - Otherwise, push MPLS label and set |
| 621 | * the MPLS ID |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 622 | * |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 623 | * @param sw target switch to set rules |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 624 | * @param subnetIp Match IP address |
| 625 | * @param mplsLabel MPLS label of final destination router |
| 626 | * @param fwdToSws next hop routers |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 627 | * @param entries |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 628 | */ |
| 629 | private void setIpTableRouter(Switch sw, String subnetIp, String mplsLabel, |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 630 | List<String> fwdToSws, Collection<MatchActionOperationEntry> entries) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 631 | |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 632 | Ipv4Match ipMatch = new Ipv4Match(subnetIp); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 633 | List<Action> actions = new ArrayList<>(); |
| 634 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 635 | // If destination SW is the same as the fwd SW, then do not push MPLS |
| 636 | // label |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 637 | |
| 638 | if (fwdToSws.size() > 1) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 639 | PushMplsAction pushMplsAction = new PushMplsAction(); |
| 640 | SetMplsIdAction setIdAction = new SetMplsIdAction(Integer.parseInt(mplsLabel)); |
| 641 | CopyTtlOutAction copyTtlOutAction = new CopyTtlOutAction(); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 642 | DecMplsTtlAction decMplsTtlAction = new DecMplsTtlAction(1); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 643 | |
| 644 | actions.add(pushMplsAction); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 645 | actions.add(copyTtlOutAction); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 646 | actions.add(decMplsTtlAction); |
| 647 | actions.add(setIdAction); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 648 | } |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 649 | else { |
| 650 | String fwdToSw = fwdToSws.get(0); |
| 651 | if (getMplsLabel(fwdToSw).equals(mplsLabel)) { |
| 652 | DecNwTtlAction decTtlAction = new DecNwTtlAction(1); |
| 653 | actions.add(decTtlAction); |
| 654 | } |
| 655 | else { |
| 656 | PushMplsAction pushMplsAction = new PushMplsAction(); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 657 | SetMplsIdAction setIdAction = new SetMplsIdAction( |
| 658 | Integer.parseInt(mplsLabel)); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 659 | CopyTtlOutAction copyTtlOutAction = new CopyTtlOutAction(); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 660 | DecMplsTtlAction decMplsTtlAction = new DecMplsTtlAction(1); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 661 | |
| 662 | actions.add(pushMplsAction); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 663 | actions.add(copyTtlOutAction); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 664 | actions.add(decMplsTtlAction); |
| 665 | actions.add(setIdAction); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 666 | } |
| 667 | } |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 668 | |
| 669 | GroupAction groupAction = new GroupAction(); |
| 670 | |
| 671 | for (String fwdSw : fwdToSws) { |
| 672 | groupAction.addSwitch(new Dpid(fwdSw)); |
| 673 | } |
| 674 | actions.add(groupAction); |
| 675 | |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 676 | // TODO: Mactch Action Id should be set correctly |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 677 | MatchAction matchAction = new MatchAction(new MatchActionId(0), |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 678 | new SwitchPort((long) 0, (short) 0), ipMatch, actions); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 679 | |
| 680 | MatchActionOperationEntry maEntry = |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 681 | new MatchActionOperationEntry( |
| 682 | net.onrc.onos.core.matchaction.MatchActionOperations.Operator.ADD, |
| 683 | matchAction); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 684 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 685 | IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 686 | getSwId(sw.getDpid().toString())); |
| 687 | |
| 688 | try { |
| 689 | printMatchActionOperationEntry(sw, maEntry); |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 690 | if (entries != null) |
| 691 | entries.add(maEntry); |
| 692 | else |
| 693 | sw13.pushFlow(maEntry); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 694 | } catch (IOException e) { |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 695 | e.printStackTrace(); |
| 696 | } |
| 697 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 698 | } |
| 699 | |
Sangho Shin | ac5ee2b | 2014-09-28 21:27:20 -0700 | [diff] [blame] | 700 | /** |
| 701 | * Convert a string DPID to its Switch Id (integer) |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 702 | * |
Sangho Shin | ac5ee2b | 2014-09-28 21:27:20 -0700 | [diff] [blame] | 703 | * @param dpid |
| 704 | * @return |
| 705 | */ |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 706 | private long getSwId(String dpid) { |
| 707 | |
| 708 | long swId = 0; |
| 709 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 710 | String swIdStr = dpid.substring(dpid.lastIndexOf(":") + 1); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 711 | if (swIdStr != null) |
| 712 | swId = Integer.parseInt(swIdStr); |
| 713 | |
| 714 | return swId; |
| 715 | } |
| 716 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 717 | /** |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 718 | * Set MPLS forwarding rules to MPLS table - If the destination is the same |
| 719 | * as the next hop to forward packets then, pop the MPLS label according to |
| 720 | * PHP rule - Otherwise, just forward packets to next hops using Group |
| 721 | * action |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 722 | * |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 723 | * @param sw Switch to set the rules |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 724 | * @param mplsLabel destination MPLS label |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 725 | * @param fwdSws next hop switches |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 726 | */ |
| 727 | private void setMplsTable(Switch sw, String mplsLabel, List<String> fwdSws) { |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 728 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 729 | MplsMatch mplsMatch = new MplsMatch(Integer.parseInt(mplsLabel)); |
| 730 | |
| 731 | List<Action> actions = new ArrayList<Action>(); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 732 | |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 733 | // 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] | 734 | // Otherwise, just decrease the MPLS TTL. |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 735 | if (fwdSws.size() == 1) { |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 736 | String fwdMplsId = getMplsLabel(fwdSws.get(0)); |
| 737 | if (fwdMplsId.equals(mplsLabel)) { |
| 738 | String fwdSw = fwdSws.get(0); |
| 739 | if (mplsLabel.equals(getMplsLabel(fwdSw))) { |
| 740 | PopMplsAction popAction = new PopMplsAction(EthType.IPv4); |
| 741 | CopyTtlInAction copyTtlInAction = new CopyTtlInAction(); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 742 | DecNwTtlAction decNwTtlAction = new DecNwTtlAction(1); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 743 | |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 744 | actions.add(copyTtlInAction); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 745 | actions.add(popAction); |
| 746 | actions.add(decNwTtlAction); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 747 | } |
| 748 | } |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 749 | else { |
| 750 | DecMplsTtlAction decMplsTtlAction = new DecMplsTtlAction(1); |
| 751 | actions.add(decMplsTtlAction); |
| 752 | } |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 753 | } |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 754 | GroupAction groupAction = new GroupAction(); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 755 | for (String fwdSw : fwdSws) |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 756 | groupAction.addSwitch(new Dpid(fwdSw)); |
| 757 | actions.add(groupAction); |
| 758 | |
| 759 | MatchAction matchAction = new MatchAction(new MatchActionId(0), |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 760 | new SwitchPort((long) 0, (short) 0), mplsMatch, actions); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 761 | |
| 762 | MatchActionOperationEntry maEntry = |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 763 | new MatchActionOperationEntry( |
| 764 | net.onrc.onos.core.matchaction.MatchActionOperations.Operator.ADD, |
| 765 | matchAction); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 766 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 767 | IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 768 | getSwId(sw.getDpid().toString())); |
| 769 | |
| 770 | try { |
| 771 | printMatchActionOperationEntry(sw, maEntry); |
| 772 | sw13.pushFlow(maEntry); |
| 773 | } catch (IOException e) { |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 774 | e.printStackTrace(); |
| 775 | } |
| 776 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 777 | } |
| 778 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 779 | /** |
| 780 | * Debugging function to print out the Match Action Entry |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 781 | * |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 782 | * @param maEntry |
| 783 | */ |
Sangho Shin | ac5ee2b | 2014-09-28 21:27:20 -0700 | [diff] [blame] | 784 | private void printMatchActionOperationEntry(Switch sw, |
| 785 | MatchActionOperationEntry maEntry) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 786 | |
Sangho Shin | 0df0198 | 2014-09-25 17:11:18 -0700 | [diff] [blame] | 787 | StringBuilder logStr = new StringBuilder("In switch " + sw.getDpid() + ", "); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 788 | |
| 789 | MatchAction ma = maEntry.getTarget(); |
| 790 | Match m = ma.getMatch(); |
| 791 | List<Action> actions = ma.getActions(); |
| 792 | |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 793 | if (m instanceof Ipv4Match) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 794 | logStr.append("If the IP matches with "); |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 795 | IPv4Net ip = ((Ipv4Match) m).getDestination(); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 796 | logStr.append(ip.toString()); |
| 797 | logStr.append(" then "); |
| 798 | } |
| 799 | else if (m instanceof MplsMatch) { |
| 800 | logStr.append("If the MPLS label matches with "); |
| 801 | int mplsLabel = ((MplsMatch) m).getMplsLabel(); |
| 802 | logStr.append(mplsLabel); |
| 803 | logStr.append(" then "); |
| 804 | } |
| 805 | |
| 806 | logStr.append(" do { "); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 807 | for (Action action : actions) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 808 | if (action instanceof CopyTtlInAction) { |
| 809 | logStr.append("copy ttl In, "); |
| 810 | } |
| 811 | else if (action instanceof CopyTtlOutAction) { |
| 812 | logStr.append("copy ttl Out, "); |
| 813 | } |
| 814 | else if (action instanceof DecMplsTtlAction) { |
| 815 | logStr.append("Dec MPLS TTL , "); |
| 816 | } |
| 817 | else if (action instanceof GroupAction) { |
| 818 | logStr.append("Forward packet to < "); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 819 | NeighborSet dpids = ((GroupAction) action).getDpids(); |
Sangho Shin | 0df0198 | 2014-09-25 17:11:18 -0700 | [diff] [blame] | 820 | logStr.append(dpids.toString() + ","); |
| 821 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 822 | } |
| 823 | else if (action instanceof PopMplsAction) { |
| 824 | logStr.append("Pop MPLS label, "); |
| 825 | } |
| 826 | else if (action instanceof PushMplsAction) { |
| 827 | logStr.append("Push MPLS label, "); |
| 828 | } |
| 829 | else if (action instanceof SetMplsIdAction) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 830 | int id = ((SetMplsIdAction) action).getMplsId(); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 831 | logStr.append("Set MPLS ID as " + id + ", "); |
| 832 | |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | log.debug(logStr.toString()); |
| 837 | |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | /** |
| 841 | * Get MPLS label reading the config file |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 842 | * |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 843 | * @param dipid DPID of the switch |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 844 | * @return MPLS label for the switch |
| 845 | */ |
| 846 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 847 | private String getMplsLabel(String dpid) { |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 848 | |
| 849 | String mplsLabel = null; |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 850 | for (Switch sw : mutableTopology.getSwitches()) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 851 | String dpidStr = sw.getDpid().toString(); |
| 852 | if (dpid.equals(dpidStr)) { |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 853 | mplsLabel = sw.getStringAttribute("nodeSid"); |
| 854 | break; |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 855 | } |
| 856 | } |
| 857 | |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 858 | return mplsLabel; |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 859 | } |
| 860 | |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 861 | /** |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 862 | * The function checks if given IP matches to the given subnet mask |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 863 | * |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 864 | * @param addr - subnet address to match |
| 865 | * @param addr1 - IP address to check |
| 866 | * @return true if the IP address matches to the subnet, otherwise false |
| 867 | */ |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 868 | public boolean netMatch(String addr, String addr1) { // addr is subnet |
| 869 | // address and addr1 is |
| 870 | // ip address. Function |
| 871 | // will return true, if |
| 872 | // addr1 is within |
| 873 | // addr(subnet) |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 874 | |
| 875 | String[] parts = addr.split("/"); |
| 876 | String ip = parts[0]; |
| 877 | int prefix; |
| 878 | |
| 879 | if (parts.length < 2) { |
| 880 | prefix = 0; |
| 881 | } else { |
| 882 | prefix = Integer.parseInt(parts[1]); |
| 883 | } |
| 884 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 885 | Inet4Address a = null; |
| 886 | Inet4Address a1 = null; |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 887 | try { |
| 888 | a = (Inet4Address) InetAddress.getByName(ip); |
| 889 | a1 = (Inet4Address) InetAddress.getByName(addr1); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 890 | } catch (UnknownHostException e) { |
| 891 | } |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 892 | |
| 893 | byte[] b = a.getAddress(); |
| 894 | int ipInt = ((b[0] & 0xFF) << 24) | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 895 | ((b[1] & 0xFF) << 16) | |
| 896 | ((b[2] & 0xFF) << 8) | |
| 897 | ((b[3] & 0xFF) << 0); |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 898 | |
| 899 | byte[] b1 = a1.getAddress(); |
| 900 | int ipInt1 = ((b1[0] & 0xFF) << 24) | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 901 | ((b1[1] & 0xFF) << 16) | |
| 902 | ((b1[2] & 0xFF) << 8) | |
| 903 | ((b1[3] & 0xFF) << 0); |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 904 | |
| 905 | int mask = ~((1 << (32 - prefix)) - 1); |
| 906 | |
| 907 | if ((ipInt & mask) == (ipInt1 & mask)) { |
| 908 | return true; |
| 909 | } |
| 910 | else { |
| 911 | return false; |
| 912 | } |
| 913 | } |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 914 | |
Sangho Shin | ac5ee2b | 2014-09-28 21:27:20 -0700 | [diff] [blame] | 915 | /** |
| 916 | * Add a routing rule for the host |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 917 | * |
Sangho Shin | ac5ee2b | 2014-09-28 21:27:20 -0700 | [diff] [blame] | 918 | * @param sw - Switch to add the rule |
| 919 | * @param hostIpAddress Destination host IP address |
| 920 | * @param hostMacAddress Destination host MAC address |
| 921 | */ |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 922 | public void addRouteToHost(Switch sw, int hostIpAddress, byte[] hostMacAddress) { |
| 923 | ipHandler.addRouteToHost(sw, hostIpAddress, hostMacAddress); |
| 924 | |
| 925 | } |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 926 | |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 927 | /** |
| 928 | * Add IP packet to a buffer queue |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 929 | * |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 930 | * @param ipv4 |
| 931 | */ |
| 932 | public void addPacket(IPv4 ipv4) { |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 933 | ipPacketQueue.add(ipv4); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | /** |
| 937 | * Retrieve all packets whose destination is the given address. |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 938 | * |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 939 | * @param destIp Destination address of packets to retrieve |
| 940 | */ |
| 941 | public List<IPv4> getIpPacketFromQueue(byte[] destIp) { |
| 942 | |
| 943 | List<IPv4> bufferedPackets = new ArrayList<IPv4>(); |
| 944 | |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 945 | if (!ipPacketQueue.isEmpty()) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 946 | for (IPv4 ip : ipPacketQueue) { |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 947 | int dest = ip.getDestinationAddress(); |
| 948 | IPv4Address ip1 = IPv4Address.of(dest); |
| 949 | IPv4Address ip2 = IPv4Address.of(destIp); |
| 950 | if (ip1.equals(ip2)) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 951 | bufferedPackets.add((IPv4) (ipPacketQueue.poll()).clone()); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 952 | } |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | return bufferedPackets; |
| 957 | } |
| 958 | |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 959 | } |