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