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 | 4ae146e | 2014-10-09 14:35:26 -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 | 4ae146e | 2014-10-09 14:35:26 -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 | 4ae146e | 2014-10-09 14:35:26 -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()); |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 467 | |
| 468 | Switch srcSwitch = mutableTopology.getSwitch(srcPort.getDpid()); |
| 469 | if (srcSwitch.getLinkToNeighbor(dstPort.getDpid()) == null) { |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 470 | // TODO: it is only for debugging purpose. |
Sangho Shin | 99918bd | 2014-10-08 15:52:35 -0700 | [diff] [blame] | 471 | // We just need to call populateEcmpRoutingRules() and return; |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 472 | recomputationRequired = true; |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 473 | log.debug("All links are gone b/w {} and {}", srcPort.getDpid(), |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 474 | dstPort.getDpid()); |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 475 | } |
| 476 | } |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 477 | |
| 478 | if (recomputationRequired) |
| 479 | populateEcmpRoutingRules(false); |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 480 | } |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 481 | |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 482 | /** |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 483 | * report ports removed to the driver immediately |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 484 | * |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 485 | * @param portEntries |
| 486 | */ |
| 487 | private void processPortRemoval(Collection<PortData> portEntries) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 488 | for (PortData port : portEntries) { |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 489 | Dpid dpid = port.getDpid(); |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 490 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 491 | IOF13Switch sw = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 492 | getSwId(port.getDpid().toString())); |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 493 | if (sw != null) |
| 494 | sw.removePortFromGroups(port.getPortNumber()); |
| 495 | log.debug("Remove port {} from switch {}", port, dpid); |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 496 | } |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 497 | } |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 498 | |
| 499 | /** |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 500 | * Populate routing rules walking through the ECMP shortest paths |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 501 | * |
Sangho Shin | 99918bd | 2014-10-08 15:52:35 -0700 | [diff] [blame] | 502 | * @param modified if true, it "modifies" the rules |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 503 | */ |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 504 | private void populateEcmpRoutingRules(boolean modified) { |
| 505 | graphs.clear(); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 506 | Iterable<Switch> switches = mutableTopology.getSwitches(); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 507 | for (Switch sw : switches) { |
| 508 | ECMPShortestPathGraph ecmpSPG = new ECMPShortestPathGraph(sw); |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 509 | graphs.put(sw, ecmpSPG); |
| 510 | //log.debug("ECMPShortestPathGraph is computed for switch {}", |
| 511 | // HexString.toHexString(sw.getDpid().value())); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 512 | populateEcmpRoutingRulesForPath(sw, ecmpSPG, modified); |
| 513 | if (modified) { |
| 514 | log.debug("Modify the rules for {}" , sw.getDpid()); |
| 515 | } |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 516 | } |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 517 | numOfPopulation++; |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 518 | } |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 519 | |
Sangho Shin | 99918bd | 2014-10-08 15:52:35 -0700 | [diff] [blame] | 520 | /** |
| 521 | * populate routing rules to forward packets from the switch given to |
| 522 | * all other switches. |
| 523 | * |
| 524 | * @param sw source switch |
| 525 | * @param ecmpSPG shortest path from the the source switch to all others |
| 526 | * @param modified modification flag |
| 527 | */ |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 528 | private void populateEcmpRoutingRulesForPath(Switch sw, |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 529 | ECMPShortestPathGraph ecmpSPG, boolean modified) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 530 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 531 | HashMap<Integer, HashMap<Switch, ArrayList<ArrayList<Dpid>>>> switchVia = |
| 532 | ecmpSPG.getAllLearnedSwitchesAndVia(); |
| 533 | for (Integer itrIdx : switchVia.keySet()) { |
| 534 | //log.debug("ECMPShortestPathGraph:Switches learned in " |
| 535 | // + "Iteration{} from switch {}:", |
| 536 | // itrIdx, |
| 537 | // HexString.toHexString(sw.getDpid().value())); |
| 538 | HashMap<Switch, ArrayList<ArrayList<Dpid>>> swViaMap = |
| 539 | switchVia.get(itrIdx); |
| 540 | for (Switch targetSw : swViaMap.keySet()) { |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 541 | //log.debug("ECMPShortestPathGraph:****switch {} via:", |
| 542 | // HexString.toHexString(targetSw.getDpid().value())); |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 543 | String destSw = sw.getDpid().toString(); |
| 544 | List<String> fwdToSw = new ArrayList<String>(); |
| 545 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 546 | for (ArrayList<Dpid> via : swViaMap.get(targetSw)) { |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 547 | //log.debug("ECMPShortestPathGraph:******{}) {}", ++i, via); |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 548 | if (via.isEmpty()) { |
| 549 | fwdToSw.add(destSw); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 550 | } |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 551 | else { |
| 552 | fwdToSw.add(via.get(0).toString()); |
| 553 | } |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 554 | } |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 555 | setRoutingRule(targetSw, destSw, fwdToSw, modified); |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 556 | } |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 557 | |
| 558 | // Send Barrier Message and make sure all rules are set |
| 559 | // before we set the rules to next routers |
| 560 | IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch( |
| 561 | getSwId(sw.getDpid().toString())); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 562 | if (sw13 != null) { |
| 563 | try { |
| 564 | OFBarrierReplyFuture replyFuture = sw13.sendBarrier(); |
| 565 | replyFuture.get(10, TimeUnit.SECONDS); |
| 566 | } catch (IOException e) { |
| 567 | e.printStackTrace(); |
| 568 | } catch (InterruptedException | ExecutionException | TimeoutException e) { |
| 569 | log.error("Barrier message not received for sw: {}", sw.getDpid()); |
| 570 | e.printStackTrace(); |
| 571 | } |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 572 | } |
| 573 | } |
| 574 | |
| 575 | } |
| 576 | |
| 577 | |
Sangho Shin | 99918bd | 2014-10-08 15:52:35 -0700 | [diff] [blame] | 578 | /** |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 579 | * This class is used only for link recovery optimization in |
| 580 | * modifyEcmpRoutingRules() function. |
Sangho Shin | 99918bd | 2014-10-08 15:52:35 -0700 | [diff] [blame] | 581 | * TODO: please remove if the optimization is not used at all |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 582 | */ |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 583 | private class SwitchPair { |
| 584 | private Switch src; |
| 585 | private Switch dst; |
| 586 | |
| 587 | public SwitchPair(Switch src, Switch dst) { |
| 588 | this.src = src; |
| 589 | this.dst = dst; |
| 590 | } |
| 591 | |
| 592 | public Switch getSource() { |
| 593 | return src; |
| 594 | } |
| 595 | |
| 596 | public Switch getDestination() { |
| 597 | return dst; |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 598 | } |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | /** |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 602 | * Modify the routing rules for the lost links |
| 603 | * - Recompute the path if the link failed is included in the path |
| 604 | * (including src and dest). |
| 605 | * |
| 606 | * @param newLink |
| 607 | */ |
| 608 | private void modifyEcmpRoutingRules(LinkData linkRemoved) { |
| 609 | |
| 610 | //HashMap<Switch, SwitchPair> linksToRecompute = new HashMap<Switch, SwitchPair>(); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 611 | Set<SwitchPair> linksToRecompute = new HashSet<SwitchPair>(); |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 612 | |
| 613 | for (ECMPShortestPathGraph ecmpSPG : graphs.values()) { |
| 614 | Switch rootSw = ecmpSPG.getRootSwitch(); |
| 615 | HashMap<Integer, HashMap<Switch, ArrayList<Path>>> paths = |
| 616 | ecmpSPG.getCompleteLearnedSwitchesAndPaths(); |
| 617 | for (HashMap<Switch, ArrayList<Path>> p: paths.values()) { |
| 618 | for (Switch destSw: p.keySet()) { |
| 619 | ArrayList<Path> path = p.get(destSw); |
| 620 | if (checkPath(path, linkRemoved)) { |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 621 | boolean found = false; |
| 622 | for (SwitchPair pair: linksToRecompute) { |
| 623 | if (pair.getSource().getDpid() == rootSw.getDpid() && |
| 624 | pair.getSource().getDpid() == destSw.getDpid()) { |
| 625 | found = true; |
| 626 | } |
| 627 | } |
| 628 | if (!found) { |
| 629 | linksToRecompute.add(new SwitchPair(rootSw, destSw)); |
| 630 | } |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 631 | } |
| 632 | } |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | // Recompute the path for the specific route |
| 637 | for (SwitchPair pair: linksToRecompute) { |
| 638 | |
| 639 | log.debug("Recompute path from {} to {}", pair.getSource(), pair.getDestination()); |
Sangho Shin | 99918bd | 2014-10-08 15:52:35 -0700 | [diff] [blame] | 640 | // We need the following function for optimization |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 641 | //ECMPShortestPathGraph ecmpSPG = |
| 642 | // new ECMPShortestPathGraph(pair.getSource(), pair.getDestination()); |
| 643 | ECMPShortestPathGraph ecmpSPG = |
| 644 | new ECMPShortestPathGraph(pair.getSource()); |
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) { |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 662 | if (link.getDst().getDpid().equals(linkRemoved.getDst().getDpid()) && |
| 663 | link.getSrc().getDpid().equals(linkRemoved.getSrc().getDpid())) |
| 664 | return true; |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 665 | } |
| 666 | } |
| 667 | |
| 668 | return false; |
| 669 | } |
| 670 | |
| 671 | /** |
| 672 | * |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 673 | * Set routing rules in targetSw {forward packets to fwdToSw switches in |
| 674 | * order to send packets to destSw} - If the target switch is an edge router |
| 675 | * and final destnation switch is also an edge router, then set IP |
| 676 | * forwarding rules to subnets - If only the target switch is an edge |
| 677 | * router, then set IP forwarding rule to the transit router loopback IP |
| 678 | * address - If the target is a transit router, then just set the MPLS |
| 679 | * forwarding rule |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 680 | * |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 681 | * @param targetSw Switch to set the rules |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 682 | * @param destSw Final destination switches |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 683 | * @param fwdToSw next hop switches |
| 684 | */ |
Sangho Shin | 99918bd | 2014-10-08 15:52:35 -0700 | [diff] [blame] | 685 | private void setRoutingRule(Switch targetSw, String destSw, |
| 686 | List<String> fwdToSw, boolean modified) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 687 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 688 | if (fwdToSw.isEmpty()) { |
| 689 | fwdToSw.add(destSw); |
| 690 | } |
| 691 | |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 692 | // 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] | 693 | if (IsEdgeRouter(targetSw.getDpid().toString()) && |
| 694 | IsEdgeRouter(destSw)) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 695 | // We assume that there is at least one transit router b/w edge |
| 696 | // routers |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 697 | Switch destSwitch = mutableTopology.getSwitch(new Dpid(destSw)); |
| 698 | String subnets = destSwitch.getStringAttribute("subnets"); |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 699 | setIpTableRouterSubnet(targetSw, subnets, getMplsLabel(destSw) |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 700 | , fwdToSw, modified); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 701 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 702 | String routerIp = destSwitch.getStringAttribute("routerIp"); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 703 | setIpTableRouter(targetSw, routerIp, getMplsLabel(destSw), fwdToSw, |
| 704 | null, modified); |
Sangho Shin | 721ca04 | 2014-10-09 13:03:40 -0700 | [diff] [blame] | 705 | // Edge router can be a transit router |
| 706 | setMplsTable(targetSw, getMplsLabel(destSw), fwdToSw, modified); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 707 | } |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 708 | // 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] | 709 | else if (IsEdgeRouter(targetSw.getDpid().toString())) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 710 | // We assume that there is at least one transit router b/w edge |
| 711 | // routers |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 712 | Switch destSwitch = mutableTopology.getSwitch(new Dpid(destSw)); |
| 713 | String routerIp = destSwitch.getStringAttribute("routerIp"); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 714 | setIpTableRouter(targetSw, routerIp, getMplsLabel(destSw), fwdToSw, |
| 715 | null, modified); |
Sangho Shin | 721ca04 | 2014-10-09 13:03:40 -0700 | [diff] [blame] | 716 | // Edge router can be a transit router |
| 717 | setMplsTable(targetSw, getMplsLabel(destSw), fwdToSw, modified); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 718 | } |
| 719 | // if it is a transit router, then set rules in the MPLS table |
| 720 | else { |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 721 | setMplsTable(targetSw, getMplsLabel(destSw), fwdToSw, modified); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | } |
| 725 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 726 | /** |
| 727 | * Set IP forwarding rule to the gateway of each subnet of switches |
| 728 | * |
| 729 | * @param targetSw Switch to set rules |
| 730 | * @param subnets subnet information |
| 731 | * @param mplsLabel destination MPLS label |
| 732 | * @param fwdToSw router to forward packets to |
| 733 | */ |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 734 | private void setIpTableRouterSubnet(Switch targetSw, String subnets, |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 735 | String mplsLabel, List<String> fwdToSw, boolean modified) { |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 736 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 737 | Collection<MatchActionOperationEntry> entries = |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 738 | new ArrayList<MatchActionOperationEntry>(); |
| 739 | |
| 740 | try { |
| 741 | JSONArray arry = new JSONArray(subnets); |
| 742 | for (int i = 0; i < arry.length(); i++) { |
| 743 | String subnetIp = (String) arry.getJSONObject(i).get("subnetIp"); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 744 | setIpTableRouter(targetSw, subnetIp, mplsLabel, fwdToSw, entries, |
| 745 | modified); |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 746 | } |
| 747 | } catch (JSONException e) { |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 748 | e.printStackTrace(); |
| 749 | } |
| 750 | |
| 751 | if (!entries.isEmpty()) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 752 | IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 753 | getSwId(targetSw.getDpid().toString())); |
| 754 | |
Sangho Shin | 721ca04 | 2014-10-09 13:03:40 -0700 | [diff] [blame] | 755 | if (sw13 != null) { |
| 756 | try { |
| 757 | sw13.pushFlows(entries); |
| 758 | } catch (IOException e) { |
| 759 | e.printStackTrace(); |
| 760 | } |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 761 | } |
| 762 | } |
| 763 | |
| 764 | } |
| 765 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 766 | /** |
Sangho Shin | 99918bd | 2014-10-08 15:52:35 -0700 | [diff] [blame] | 767 | * Check if the switch is the edge router or not. |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 768 | * |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 769 | * @param dpid Dpid of the switch to check |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 770 | * @return true if it is an edge router, otherwise false |
| 771 | */ |
| 772 | private boolean IsEdgeRouter(String dpid) { |
| 773 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 774 | for (Switch sw : mutableTopology.getSwitches()) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 775 | String dpidStr = sw.getDpid().toString(); |
| 776 | if (dpid.equals(dpidStr)) { |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 777 | /* |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 778 | String subnetInfo = sw.getStringAttribute("subnets"); |
| 779 | if (subnetInfo == null || subnetInfo.equals("[]")) { |
| 780 | return false; |
| 781 | } |
| 782 | else |
| 783 | return true; |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 784 | */ |
| 785 | String isEdge = sw.getStringAttribute("isEdgeRouter"); |
| 786 | if (isEdge != null) { |
| 787 | if (isEdge.equals("true")) |
| 788 | return true; |
| 789 | else |
| 790 | return false; |
| 791 | } |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 792 | } |
| 793 | } |
| 794 | |
| 795 | return false; |
| 796 | } |
| 797 | |
| 798 | /** |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 799 | * Set IP forwarding rule - If the destination is the next hop, then do not |
| 800 | * push MPLS, just decrease the NW TTL - Otherwise, push MPLS label and set |
| 801 | * the MPLS ID |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 802 | * |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 803 | * @param sw target switch to set rules |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 804 | * @param subnetIp Match IP address |
| 805 | * @param mplsLabel MPLS label of final destination router |
| 806 | * @param fwdToSws next hop routers |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 807 | * @param entries |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 808 | */ |
| 809 | private void setIpTableRouter(Switch sw, String subnetIp, String mplsLabel, |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 810 | List<String> fwdToSws, Collection<MatchActionOperationEntry> entries, |
| 811 | boolean modified) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 812 | |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 813 | Ipv4Match ipMatch = new Ipv4Match(subnetIp); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 814 | List<Action> actions = new ArrayList<>(); |
Sangho Shin | 721ca04 | 2014-10-09 13:03:40 -0700 | [diff] [blame] | 815 | GroupAction groupAction = new GroupAction(); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 816 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 817 | // If destination SW is the same as the fwd SW, then do not push MPLS |
| 818 | // label |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 819 | |
| 820 | if (fwdToSws.size() > 1) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 821 | PushMplsAction pushMplsAction = new PushMplsAction(); |
| 822 | SetMplsIdAction setIdAction = new SetMplsIdAction(Integer.parseInt(mplsLabel)); |
| 823 | CopyTtlOutAction copyTtlOutAction = new CopyTtlOutAction(); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 824 | DecMplsTtlAction decMplsTtlAction = new DecMplsTtlAction(1); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 825 | |
Sangho Shin | 62ce5c1 | 2014-10-08 16:24:40 -0700 | [diff] [blame] | 826 | //actions.add(pushMplsAction); |
| 827 | //actions.add(copyTtlOutAction); |
| 828 | //actions.add(decMplsTtlAction); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 829 | actions.add(setIdAction); |
Sangho Shin | 721ca04 | 2014-10-09 13:03:40 -0700 | [diff] [blame] | 830 | groupAction.setEdgeLabel(Integer.parseInt(mplsLabel)); |
| 831 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 832 | } |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 833 | else { |
| 834 | String fwdToSw = fwdToSws.get(0); |
| 835 | if (getMplsLabel(fwdToSw).equals(mplsLabel)) { |
| 836 | DecNwTtlAction decTtlAction = new DecNwTtlAction(1); |
| 837 | actions.add(decTtlAction); |
| 838 | } |
| 839 | else { |
| 840 | PushMplsAction pushMplsAction = new PushMplsAction(); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 841 | SetMplsIdAction setIdAction = new SetMplsIdAction( |
| 842 | Integer.parseInt(mplsLabel)); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 843 | CopyTtlOutAction copyTtlOutAction = new CopyTtlOutAction(); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 844 | DecMplsTtlAction decMplsTtlAction = new DecMplsTtlAction(1); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 845 | |
Sangho Shin | 62ce5c1 | 2014-10-08 16:24:40 -0700 | [diff] [blame] | 846 | //actions.add(pushMplsAction); |
| 847 | //actions.add(copyTtlOutAction); |
| 848 | //actions.add(decMplsTtlAction); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 849 | actions.add(setIdAction); |
Sangho Shin | 721ca04 | 2014-10-09 13:03:40 -0700 | [diff] [blame] | 850 | groupAction.setEdgeLabel(Integer.parseInt(mplsLabel)); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 851 | } |
| 852 | } |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 853 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 854 | for (String fwdSw : fwdToSws) { |
| 855 | groupAction.addSwitch(new Dpid(fwdSw)); |
| 856 | } |
| 857 | actions.add(groupAction); |
| 858 | |
Sangho Shin | 99918bd | 2014-10-08 15:52:35 -0700 | [diff] [blame] | 859 | MatchAction matchAction = new MatchAction(new MatchActionId(matchActionId++), |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 860 | new SwitchPort((long) 0, (short) 0), ipMatch, actions); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 861 | |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 862 | Operator operator = null; |
| 863 | if (modified) |
| 864 | operator = Operator.MODIFY; |
| 865 | else |
| 866 | operator = Operator.ADD; |
| 867 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 868 | MatchActionOperationEntry maEntry = |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 869 | new MatchActionOperationEntry(operator, matchAction); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 870 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 871 | IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 872 | getSwId(sw.getDpid().toString())); |
| 873 | |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 874 | if (sw13 != null) { |
| 875 | try { |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 876 | //printMatchActionOperationEntry(sw, maEntry); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 877 | if (entries != null) |
| 878 | entries.add(maEntry); |
| 879 | else |
| 880 | sw13.pushFlow(maEntry); |
| 881 | } catch (IOException e) { |
| 882 | e.printStackTrace(); |
| 883 | } |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 884 | } |
| 885 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 886 | } |
| 887 | |
Sangho Shin | ac5ee2b | 2014-09-28 21:27:20 -0700 | [diff] [blame] | 888 | /** |
| 889 | * Convert a string DPID to its Switch Id (integer) |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 890 | * |
Sangho Shin | ac5ee2b | 2014-09-28 21:27:20 -0700 | [diff] [blame] | 891 | * @param dpid |
| 892 | * @return |
| 893 | */ |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 894 | private long getSwId(String dpid) { |
| 895 | |
| 896 | long swId = 0; |
| 897 | |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 898 | String swIdHexStr = "0x"+dpid.substring(dpid.lastIndexOf(":") + 1); |
| 899 | if (swIdHexStr != null) |
| 900 | swId = Integer.decode(swIdHexStr); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 901 | |
| 902 | return swId; |
| 903 | } |
| 904 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 905 | /** |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 906 | * Set MPLS forwarding rules to MPLS table - If the destination is the same |
| 907 | * as the next hop to forward packets then, pop the MPLS label according to |
| 908 | * PHP rule - Otherwise, just forward packets to next hops using Group |
| 909 | * action |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 910 | * |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 911 | * @param sw Switch to set the rules |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 912 | * @param mplsLabel destination MPLS label |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 913 | * @param fwdSws next hop switches |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 914 | */ |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 915 | private void setMplsTable(Switch sw, String mplsLabel, List<String> fwdSws, |
| 916 | boolean modified) { |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 917 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 918 | MplsMatch mplsMatch = new MplsMatch(Integer.parseInt(mplsLabel)); |
| 919 | |
| 920 | List<Action> actions = new ArrayList<Action>(); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 921 | |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 922 | // 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] | 923 | // Otherwise, just decrease the MPLS TTL. |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 924 | if (fwdSws.size() == 1) { |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 925 | String fwdMplsId = getMplsLabel(fwdSws.get(0)); |
| 926 | if (fwdMplsId.equals(mplsLabel)) { |
| 927 | String fwdSw = fwdSws.get(0); |
| 928 | if (mplsLabel.equals(getMplsLabel(fwdSw))) { |
| 929 | PopMplsAction popAction = new PopMplsAction(EthType.IPv4); |
| 930 | CopyTtlInAction copyTtlInAction = new CopyTtlInAction(); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 931 | DecNwTtlAction decNwTtlAction = new DecNwTtlAction(1); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 932 | |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 933 | actions.add(copyTtlInAction); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 934 | actions.add(popAction); |
| 935 | actions.add(decNwTtlAction); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 936 | } |
| 937 | } |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 938 | else { |
| 939 | DecMplsTtlAction decMplsTtlAction = new DecMplsTtlAction(1); |
| 940 | actions.add(decMplsTtlAction); |
| 941 | } |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 942 | } |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 943 | GroupAction groupAction = new GroupAction(); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 944 | for (String fwdSw : fwdSws) |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 945 | groupAction.addSwitch(new Dpid(fwdSw)); |
| 946 | actions.add(groupAction); |
| 947 | |
Sangho Shin | 99918bd | 2014-10-08 15:52:35 -0700 | [diff] [blame] | 948 | MatchAction matchAction = new MatchAction(new MatchActionId(matchActionId++), |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 949 | new SwitchPort((long) 0, (short) 0), mplsMatch, actions); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 950 | |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 951 | Operator operator = null; |
| 952 | if (modified) |
| 953 | operator = Operator.MODIFY; |
| 954 | else |
| 955 | operator = Operator.ADD; |
| 956 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 957 | MatchActionOperationEntry maEntry = |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 958 | new MatchActionOperationEntry(operator, matchAction); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 959 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 960 | IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 961 | getSwId(sw.getDpid().toString())); |
| 962 | |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 963 | if (sw13 != null) { |
| 964 | try { |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 965 | //printMatchActionOperationEntry(sw, maEntry); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 966 | sw13.pushFlow(maEntry); |
| 967 | } catch (IOException e) { |
| 968 | e.printStackTrace(); |
| 969 | } |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 970 | } |
| 971 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 972 | } |
| 973 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 974 | /** |
| 975 | * Debugging function to print out the Match Action Entry |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 976 | * |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 977 | * @param maEntry |
| 978 | */ |
Sangho Shin | ac5ee2b | 2014-09-28 21:27:20 -0700 | [diff] [blame] | 979 | private void printMatchActionOperationEntry(Switch sw, |
| 980 | MatchActionOperationEntry maEntry) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 981 | |
Sangho Shin | 0df0198 | 2014-09-25 17:11:18 -0700 | [diff] [blame] | 982 | StringBuilder logStr = new StringBuilder("In switch " + sw.getDpid() + ", "); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 983 | |
| 984 | MatchAction ma = maEntry.getTarget(); |
| 985 | Match m = ma.getMatch(); |
| 986 | List<Action> actions = ma.getActions(); |
| 987 | |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 988 | if (m instanceof Ipv4Match) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 989 | logStr.append("If the IP matches with "); |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 990 | IPv4Net ip = ((Ipv4Match) m).getDestination(); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 991 | logStr.append(ip.toString()); |
| 992 | logStr.append(" then "); |
| 993 | } |
| 994 | else if (m instanceof MplsMatch) { |
| 995 | logStr.append("If the MPLS label matches with "); |
| 996 | int mplsLabel = ((MplsMatch) m).getMplsLabel(); |
| 997 | logStr.append(mplsLabel); |
| 998 | logStr.append(" then "); |
| 999 | } |
| 1000 | |
| 1001 | logStr.append(" do { "); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 1002 | for (Action action : actions) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 1003 | if (action instanceof CopyTtlInAction) { |
| 1004 | logStr.append("copy ttl In, "); |
| 1005 | } |
| 1006 | else if (action instanceof CopyTtlOutAction) { |
| 1007 | logStr.append("copy ttl Out, "); |
| 1008 | } |
| 1009 | else if (action instanceof DecMplsTtlAction) { |
| 1010 | logStr.append("Dec MPLS TTL , "); |
| 1011 | } |
| 1012 | else if (action instanceof GroupAction) { |
| 1013 | logStr.append("Forward packet to < "); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 1014 | NeighborSet dpids = ((GroupAction) action).getDpids(); |
Sangho Shin | 0df0198 | 2014-09-25 17:11:18 -0700 | [diff] [blame] | 1015 | logStr.append(dpids.toString() + ","); |
| 1016 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 1017 | } |
| 1018 | else if (action instanceof PopMplsAction) { |
| 1019 | logStr.append("Pop MPLS label, "); |
| 1020 | } |
| 1021 | else if (action instanceof PushMplsAction) { |
| 1022 | logStr.append("Push MPLS label, "); |
| 1023 | } |
| 1024 | else if (action instanceof SetMplsIdAction) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 1025 | int id = ((SetMplsIdAction) action).getMplsId(); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 1026 | logStr.append("Set MPLS ID as " + id + ", "); |
| 1027 | |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | log.debug(logStr.toString()); |
| 1032 | |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | /** |
| 1036 | * Get MPLS label reading the config file |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 1037 | * |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 1038 | * @param dipid DPID of the switch |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 1039 | * @return MPLS label for the switch |
| 1040 | */ |
| 1041 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 1042 | private String getMplsLabel(String dpid) { |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 1043 | |
| 1044 | String mplsLabel = null; |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 1045 | for (Switch sw : mutableTopology.getSwitches()) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 1046 | String dpidStr = sw.getDpid().toString(); |
| 1047 | if (dpid.equals(dpidStr)) { |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 1048 | mplsLabel = sw.getStringAttribute("nodeSid"); |
| 1049 | break; |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 1050 | } |
| 1051 | } |
| 1052 | |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 1053 | return mplsLabel; |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 1056 | /** |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 1057 | * The function checks if given IP matches to the given subnet mask |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 1058 | * |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 1059 | * @param addr - subnet address to match |
| 1060 | * @param addr1 - IP address to check |
| 1061 | * @return true if the IP address matches to the subnet, otherwise false |
| 1062 | */ |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 1063 | public boolean netMatch(String addr, String addr1) { // addr is subnet |
| 1064 | // address and addr1 is |
| 1065 | // ip address. Function |
| 1066 | // will return true, if |
| 1067 | // addr1 is within |
| 1068 | // addr(subnet) |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 1069 | |
| 1070 | String[] parts = addr.split("/"); |
| 1071 | String ip = parts[0]; |
| 1072 | int prefix; |
| 1073 | |
| 1074 | if (parts.length < 2) { |
| 1075 | prefix = 0; |
| 1076 | } else { |
| 1077 | prefix = Integer.parseInt(parts[1]); |
| 1078 | } |
| 1079 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 1080 | Inet4Address a = null; |
| 1081 | Inet4Address a1 = null; |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 1082 | try { |
| 1083 | a = (Inet4Address) InetAddress.getByName(ip); |
| 1084 | a1 = (Inet4Address) InetAddress.getByName(addr1); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 1085 | } catch (UnknownHostException e) { |
| 1086 | } |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 1087 | |
| 1088 | byte[] b = a.getAddress(); |
| 1089 | int ipInt = ((b[0] & 0xFF) << 24) | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 1090 | ((b[1] & 0xFF) << 16) | |
| 1091 | ((b[2] & 0xFF) << 8) | |
| 1092 | ((b[3] & 0xFF) << 0); |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 1093 | |
| 1094 | byte[] b1 = a1.getAddress(); |
| 1095 | int ipInt1 = ((b1[0] & 0xFF) << 24) | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 1096 | ((b1[1] & 0xFF) << 16) | |
| 1097 | ((b1[2] & 0xFF) << 8) | |
| 1098 | ((b1[3] & 0xFF) << 0); |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 1099 | |
| 1100 | int mask = ~((1 << (32 - prefix)) - 1); |
| 1101 | |
| 1102 | if ((ipInt & mask) == (ipInt1 & mask)) { |
| 1103 | return true; |
| 1104 | } |
| 1105 | else { |
| 1106 | return false; |
| 1107 | } |
| 1108 | } |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 1109 | |
Sangho Shin | ac5ee2b | 2014-09-28 21:27:20 -0700 | [diff] [blame] | 1110 | /** |
| 1111 | * Add a routing rule for the host |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 1112 | * |
Sangho Shin | ac5ee2b | 2014-09-28 21:27:20 -0700 | [diff] [blame] | 1113 | * @param sw - Switch to add the rule |
| 1114 | * @param hostIpAddress Destination host IP address |
| 1115 | * @param hostMacAddress Destination host MAC address |
| 1116 | */ |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 1117 | public void addRouteToHost(Switch sw, int hostIpAddress, byte[] hostMacAddress) { |
| 1118 | ipHandler.addRouteToHost(sw, hostIpAddress, hostMacAddress); |
| 1119 | |
| 1120 | } |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 1121 | |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 1122 | /** |
| 1123 | * Add IP packet to a buffer queue |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 1124 | * |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 1125 | * @param ipv4 |
| 1126 | */ |
| 1127 | public void addPacket(IPv4 ipv4) { |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 1128 | ipPacketQueue.add(ipv4); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | /** |
| 1132 | * Retrieve all packets whose destination is the given address. |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 1133 | * |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 1134 | * @param destIp Destination address of packets to retrieve |
| 1135 | */ |
| 1136 | public List<IPv4> getIpPacketFromQueue(byte[] destIp) { |
| 1137 | |
| 1138 | List<IPv4> bufferedPackets = new ArrayList<IPv4>(); |
| 1139 | |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 1140 | if (!ipPacketQueue.isEmpty()) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 1141 | for (IPv4 ip : ipPacketQueue) { |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 1142 | int dest = ip.getDestinationAddress(); |
| 1143 | IPv4Address ip1 = IPv4Address.of(dest); |
| 1144 | IPv4Address ip2 = IPv4Address.of(destIp); |
| 1145 | if (ip1.equals(ip2)) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 1146 | bufferedPackets.add((IPv4) (ipPacketQueue.poll()).clone()); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 1147 | } |
| 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | return bufferedPackets; |
| 1152 | } |
| 1153 | |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 1154 | } |