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