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