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