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())); |
Fahad Naeem Khan | d89448d | 2014-10-06 18:40:45 -0700 | [diff] [blame] | 315 | if (sw == null){ |
| 316 | return; |
| 317 | } |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 318 | sw.addPortToGroups(port.getPortNumber()); |
| 319 | |
| 320 | log.debug("Add port {} to switch {}", port, dpid); |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Reports ports of new links to driver and recalculate ECMP SPG |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 326 | * |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 327 | * @param linkEntries |
| 328 | */ |
| 329 | private void processLinkAdd(Collection<LinkData> linkEntries) { |
| 330 | |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 331 | boolean linkRecovered = false; |
| 332 | |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 333 | // TODO: How to determine this link was broken before and back now |
| 334 | // If so, we need to ad the link slowly... |
| 335 | // Or, just add any new or restored link slowly ??? |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 336 | for (LinkData link : linkEntries) { |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 337 | |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 338 | SwitchPort srcPort = link.getSrc(); |
| 339 | SwitchPort dstPort = link.getDst(); |
| 340 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 341 | IOF13Switch srcSw = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 342 | getSwId(srcPort.getDpid().toString())); |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 343 | IOF13Switch dstSw = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 344 | getSwId(srcPort.getDpid().toString())); |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 345 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 346 | srcSw.addPortToGroups(srcPort.getPortNumber()); |
| 347 | dstSw.addPortToGroups(dstPort.getPortNumber()); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 348 | |
| 349 | |
| 350 | if (!topologyLinks.contains(link)) { |
| 351 | topologyLinks.add(link); |
| 352 | } |
| 353 | else { |
| 354 | linkRecovered = true; |
| 355 | } |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 356 | } |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 357 | if (linkRecovered) { |
| 358 | populateEcmpRoutingRules(true); |
| 359 | } |
| 360 | else { |
| 361 | populateEcmpRoutingRules(false); |
| 362 | } |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | /** |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 366 | * Check if all links are gone b/w the two switches. If all links are gone, |
| 367 | * then we need to recalculate the path. Otherwise, just report link failure |
| 368 | * to the driver. |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 369 | * |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 370 | * @param linkEntries |
| 371 | */ |
| 372 | private void processLinkRemoval(Collection<LinkData> linkEntries) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 373 | for (LinkData link : linkEntries) { |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 374 | SwitchPort srcPort = link.getSrc(); |
| 375 | SwitchPort dstPort = link.getDst(); |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 376 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 377 | IOF13Switch srcSw = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 378 | getSwId(srcPort.getDpid().toString())); |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 379 | IOF13Switch dstSw = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 380 | getSwId(srcPort.getDpid().toString())); |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 381 | |
| 382 | // TODO: Why do we get null for the master switch?? |
| 383 | if (srcSw != null) |
| 384 | srcSw.removePortFromGroups(srcPort.getPortNumber()); |
| 385 | if (dstSw != null) |
| 386 | dstSw.removePortFromGroups(dstPort.getPortNumber()); |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 387 | |
| 388 | Switch srcSwitch = mutableTopology.getSwitch(srcPort.getDpid()); |
| 389 | if (srcSwitch.getLinkToNeighbor(dstPort.getDpid()) == null) { |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 390 | //modifyEcmpRoutingRules(link); |
| 391 | populateEcmpRoutingRules(true); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 392 | log.debug("All links are gone b/w {} and {}", srcPort.getDpid(), |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 393 | dstPort.getDpid()); |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 394 | } |
| 395 | } |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 396 | } |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 397 | |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 398 | /** |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 399 | * report ports removed to the driver immediately |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 400 | * |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 401 | * @param portEntries |
| 402 | */ |
| 403 | private void processPortRemoval(Collection<PortData> portEntries) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 404 | for (PortData port : portEntries) { |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 405 | Dpid dpid = port.getDpid(); |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 406 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 407 | IOF13Switch sw = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 408 | getSwId(port.getDpid().toString())); |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 409 | if (sw != null) |
| 410 | sw.removePortFromGroups(port.getPortNumber()); |
| 411 | log.debug("Remove port {} from switch {}", port, dpid); |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 412 | } |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 413 | } |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 414 | |
| 415 | /** |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 416 | * Populate routing rules walking through the ECMP shortest paths |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 417 | * |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 418 | */ |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 419 | private void populateEcmpRoutingRules(boolean modified) { |
| 420 | graphs.clear(); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 421 | Iterable<Switch> switches = mutableTopology.getSwitches(); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 422 | for (Switch sw : switches) { |
| 423 | ECMPShortestPathGraph ecmpSPG = new ECMPShortestPathGraph(sw); |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 424 | graphs.put(sw, ecmpSPG); |
| 425 | //log.debug("ECMPShortestPathGraph is computed for switch {}", |
| 426 | // HexString.toHexString(sw.getDpid().value())); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 427 | populateEcmpRoutingRulesForPath(sw, ecmpSPG, modified); |
| 428 | if (modified) { |
| 429 | log.debug("Modify the rules for {}" , sw.getDpid()); |
| 430 | } |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 431 | } |
| 432 | } |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 433 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 434 | private void populateEcmpRoutingRulesForPath(Switch sw, |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 435 | ECMPShortestPathGraph ecmpSPG, boolean modified) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 436 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 437 | HashMap<Integer, HashMap<Switch, ArrayList<ArrayList<Dpid>>>> switchVia = |
| 438 | ecmpSPG.getAllLearnedSwitchesAndVia(); |
| 439 | for (Integer itrIdx : switchVia.keySet()) { |
| 440 | //log.debug("ECMPShortestPathGraph:Switches learned in " |
| 441 | // + "Iteration{} from switch {}:", |
| 442 | // itrIdx, |
| 443 | // HexString.toHexString(sw.getDpid().value())); |
| 444 | HashMap<Switch, ArrayList<ArrayList<Dpid>>> swViaMap = |
| 445 | switchVia.get(itrIdx); |
| 446 | for (Switch targetSw : swViaMap.keySet()) { |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 447 | //log.debug("ECMPShortestPathGraph:****switch {} via:", |
| 448 | // HexString.toHexString(targetSw.getDpid().value())); |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 449 | String destSw = sw.getDpid().toString(); |
| 450 | List<String> fwdToSw = new ArrayList<String>(); |
| 451 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 452 | for (ArrayList<Dpid> via : swViaMap.get(targetSw)) { |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 453 | //log.debug("ECMPShortestPathGraph:******{}) {}", ++i, via); |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 454 | if (via.isEmpty()) { |
| 455 | fwdToSw.add(destSw); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 456 | } |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 457 | else { |
| 458 | fwdToSw.add(via.get(0).toString()); |
| 459 | } |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 460 | } |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 461 | setRoutingRule(targetSw, destSw, fwdToSw, modified); |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 462 | } |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 463 | |
| 464 | // Send Barrier Message and make sure all rules are set |
| 465 | // before we set the rules to next routers |
| 466 | IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch( |
| 467 | getSwId(sw.getDpid().toString())); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 468 | if (sw13 != null) { |
| 469 | try { |
| 470 | OFBarrierReplyFuture replyFuture = sw13.sendBarrier(); |
| 471 | replyFuture.get(10, TimeUnit.SECONDS); |
| 472 | } catch (IOException e) { |
| 473 | e.printStackTrace(); |
| 474 | } catch (InterruptedException | ExecutionException | TimeoutException e) { |
| 475 | log.error("Barrier message not received for sw: {}", sw.getDpid()); |
| 476 | e.printStackTrace(); |
| 477 | } |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 478 | } |
| 479 | } |
| 480 | |
| 481 | } |
| 482 | |
| 483 | |
| 484 | private class SwitchPair { |
| 485 | private Switch src; |
| 486 | private Switch dst; |
| 487 | |
| 488 | public SwitchPair(Switch src, Switch dst) { |
| 489 | this.src = src; |
| 490 | this.dst = dst; |
| 491 | } |
| 492 | |
| 493 | public Switch getSource() { |
| 494 | return src; |
| 495 | } |
| 496 | |
| 497 | public Switch getDestination() { |
| 498 | return dst; |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 499 | } |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | /** |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 503 | * Modify the routing rules for the lost links |
| 504 | * - Recompute the path if the link failed is included in the path |
| 505 | * (including src and dest). |
| 506 | * |
| 507 | * @param newLink |
| 508 | */ |
| 509 | private void modifyEcmpRoutingRules(LinkData linkRemoved) { |
| 510 | |
| 511 | //HashMap<Switch, SwitchPair> linksToRecompute = new HashMap<Switch, SwitchPair>(); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 512 | Set<SwitchPair> linksToRecompute = new HashSet<SwitchPair>(); |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 513 | |
| 514 | for (ECMPShortestPathGraph ecmpSPG : graphs.values()) { |
| 515 | Switch rootSw = ecmpSPG.getRootSwitch(); |
| 516 | HashMap<Integer, HashMap<Switch, ArrayList<Path>>> paths = |
| 517 | ecmpSPG.getCompleteLearnedSwitchesAndPaths(); |
| 518 | for (HashMap<Switch, ArrayList<Path>> p: paths.values()) { |
| 519 | for (Switch destSw: p.keySet()) { |
| 520 | ArrayList<Path> path = p.get(destSw); |
| 521 | if (checkPath(path, linkRemoved)) { |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 522 | boolean found = false; |
| 523 | for (SwitchPair pair: linksToRecompute) { |
| 524 | if (pair.getSource().getDpid() == rootSw.getDpid() && |
| 525 | pair.getSource().getDpid() == destSw.getDpid()) { |
| 526 | found = true; |
| 527 | } |
| 528 | } |
| 529 | if (!found) { |
| 530 | linksToRecompute.add(new SwitchPair(rootSw, destSw)); |
| 531 | } |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 532 | } |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | // Recompute the path for the specific route |
| 538 | for (SwitchPair pair: linksToRecompute) { |
| 539 | |
| 540 | log.debug("Recompute path from {} to {}", pair.getSource(), pair.getDestination()); |
| 541 | // TODO : we need the following function |
| 542 | //ECMPShortestPathGraph ecmpSPG = |
| 543 | // new ECMPShortestPathGraph(pair.getSource(), pair.getDestination()); |
| 544 | ECMPShortestPathGraph ecmpSPG = |
| 545 | new ECMPShortestPathGraph(pair.getSource()); |
| 546 | // TODO: we need to use different function to MODIFY the rules |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 547 | populateEcmpRoutingRulesForPath(pair.getSource(), ecmpSPG, true); |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 548 | } |
| 549 | } |
| 550 | |
| 551 | /** |
| 552 | * Check if the path is affected from the link removed |
| 553 | * |
| 554 | * @param path Path to check |
| 555 | * @param linkRemoved link removed |
| 556 | * @return true if the path contains the link removed |
| 557 | */ |
| 558 | private boolean checkPath(ArrayList<Path> path, LinkData linkRemoved) { |
| 559 | |
| 560 | for (Path ppp: path) { |
| 561 | // TODO: need to check if this is a bidirectional or |
| 562 | // unidirectional |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 563 | for (LinkData link: ppp) { |
| 564 | //if (link.equals(linkRemoved)) |
| 565 | if (link.getDst().getDpid().equals(linkRemoved.getDst().getDpid()) && |
| 566 | link.getSrc().getDpid().equals(linkRemoved.getSrc().getDpid())) |
| 567 | return true; |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | |
| 571 | return false; |
| 572 | } |
| 573 | |
| 574 | /** |
| 575 | * |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 576 | * Set routing rules in targetSw {forward packets to fwdToSw switches in |
| 577 | * order to send packets to destSw} - If the target switch is an edge router |
| 578 | * and final destnation switch is also an edge router, then set IP |
| 579 | * forwarding rules to subnets - If only the target switch is an edge |
| 580 | * router, then set IP forwarding rule to the transit router loopback IP |
| 581 | * address - If the target is a transit router, then just set the MPLS |
| 582 | * forwarding rule |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 583 | * |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 584 | * @param targetSw Switch to set the rules |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 585 | * @param destSw Final destination switches |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 586 | * @param fwdToSw next hop switches |
| 587 | */ |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 588 | private void setRoutingRule(Switch targetSw, String destSw, List<String> fwdToSw, |
| 589 | boolean modified) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 590 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 591 | if (fwdToSw.isEmpty()) { |
| 592 | fwdToSw.add(destSw); |
| 593 | } |
| 594 | |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 595 | // 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] | 596 | if (IsEdgeRouter(targetSw.getDpid().toString()) && |
| 597 | IsEdgeRouter(destSw)) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 598 | // We assume that there is at least one transit router b/w edge |
| 599 | // routers |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 600 | Switch destSwitch = mutableTopology.getSwitch(new Dpid(destSw)); |
| 601 | String subnets = destSwitch.getStringAttribute("subnets"); |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 602 | setIpTableRouterSubnet(targetSw, subnets, getMplsLabel(destSw) |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 603 | , fwdToSw, modified); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 604 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 605 | String routerIp = destSwitch.getStringAttribute("routerIp"); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 606 | setIpTableRouter(targetSw, routerIp, getMplsLabel(destSw), fwdToSw, |
| 607 | null, modified); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 608 | } |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 609 | // 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] | 610 | else if (IsEdgeRouter(targetSw.getDpid().toString())) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 611 | // We assume that there is at least one transit router b/w edge |
| 612 | // routers |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 613 | Switch destSwitch = mutableTopology.getSwitch(new Dpid(destSw)); |
| 614 | String routerIp = destSwitch.getStringAttribute("routerIp"); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 615 | setIpTableRouter(targetSw, routerIp, getMplsLabel(destSw), fwdToSw, |
| 616 | null, modified); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 617 | } |
| 618 | // if it is a transit router, then set rules in the MPLS table |
| 619 | else { |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 620 | setMplsTable(targetSw, getMplsLabel(destSw), fwdToSw, modified); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | } |
| 624 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 625 | /** |
| 626 | * Set IP forwarding rule to the gateway of each subnet of switches |
| 627 | * |
| 628 | * @param targetSw Switch to set rules |
| 629 | * @param subnets subnet information |
| 630 | * @param mplsLabel destination MPLS label |
| 631 | * @param fwdToSw router to forward packets to |
| 632 | */ |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 633 | private void setIpTableRouterSubnet(Switch targetSw, String subnets, |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 634 | String mplsLabel, List<String> fwdToSw, boolean modified) { |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 635 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 636 | Collection<MatchActionOperationEntry> entries = |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 637 | new ArrayList<MatchActionOperationEntry>(); |
| 638 | |
| 639 | try { |
| 640 | JSONArray arry = new JSONArray(subnets); |
| 641 | for (int i = 0; i < arry.length(); i++) { |
| 642 | String subnetIp = (String) arry.getJSONObject(i).get("subnetIp"); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 643 | setIpTableRouter(targetSw, subnetIp, mplsLabel, fwdToSw, entries, |
| 644 | modified); |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 645 | } |
| 646 | } catch (JSONException e) { |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 647 | e.printStackTrace(); |
| 648 | } |
| 649 | |
| 650 | if (!entries.isEmpty()) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 651 | IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 652 | getSwId(targetSw.getDpid().toString())); |
| 653 | |
| 654 | try { |
| 655 | sw13.pushFlows(entries); |
| 656 | } catch (IOException e) { |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 657 | e.printStackTrace(); |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | } |
| 662 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 663 | /** |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 664 | * Check if the switch is the edge router or not If any subnet information |
| 665 | * 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] | 666 | * |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 667 | * @param dpid Dpid of the switch to check |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 668 | * @return true if it is an edge router, otherwise false |
| 669 | */ |
| 670 | private boolean IsEdgeRouter(String dpid) { |
| 671 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 672 | for (Switch sw : mutableTopology.getSwitches()) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 673 | String dpidStr = sw.getDpid().toString(); |
| 674 | if (dpid.equals(dpidStr)) { |
| 675 | String subnetInfo = sw.getStringAttribute("subnets"); |
| 676 | if (subnetInfo == null || subnetInfo.equals("[]")) { |
| 677 | return false; |
| 678 | } |
| 679 | else |
| 680 | return true; |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | return false; |
| 685 | } |
| 686 | |
| 687 | /** |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 688 | * Set IP forwarding rule - If the destination is the next hop, then do not |
| 689 | * push MPLS, just decrease the NW TTL - Otherwise, push MPLS label and set |
| 690 | * the MPLS ID |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 691 | * |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 692 | * @param sw target switch to set rules |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 693 | * @param subnetIp Match IP address |
| 694 | * @param mplsLabel MPLS label of final destination router |
| 695 | * @param fwdToSws next hop routers |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 696 | * @param entries |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 697 | */ |
| 698 | private void setIpTableRouter(Switch sw, String subnetIp, String mplsLabel, |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 699 | List<String> fwdToSws, Collection<MatchActionOperationEntry> entries, |
| 700 | boolean modified) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 701 | |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 702 | Ipv4Match ipMatch = new Ipv4Match(subnetIp); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 703 | List<Action> actions = new ArrayList<>(); |
| 704 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 705 | // If destination SW is the same as the fwd SW, then do not push MPLS |
| 706 | // label |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 707 | |
| 708 | if (fwdToSws.size() > 1) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 709 | PushMplsAction pushMplsAction = new PushMplsAction(); |
| 710 | SetMplsIdAction setIdAction = new SetMplsIdAction(Integer.parseInt(mplsLabel)); |
| 711 | CopyTtlOutAction copyTtlOutAction = new CopyTtlOutAction(); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 712 | DecMplsTtlAction decMplsTtlAction = new DecMplsTtlAction(1); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 713 | |
| 714 | actions.add(pushMplsAction); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 715 | actions.add(copyTtlOutAction); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 716 | actions.add(decMplsTtlAction); |
| 717 | actions.add(setIdAction); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 718 | } |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 719 | else { |
| 720 | String fwdToSw = fwdToSws.get(0); |
| 721 | if (getMplsLabel(fwdToSw).equals(mplsLabel)) { |
| 722 | DecNwTtlAction decTtlAction = new DecNwTtlAction(1); |
| 723 | actions.add(decTtlAction); |
| 724 | } |
| 725 | else { |
| 726 | PushMplsAction pushMplsAction = new PushMplsAction(); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 727 | SetMplsIdAction setIdAction = new SetMplsIdAction( |
| 728 | Integer.parseInt(mplsLabel)); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 729 | CopyTtlOutAction copyTtlOutAction = new CopyTtlOutAction(); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 730 | DecMplsTtlAction decMplsTtlAction = new DecMplsTtlAction(1); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 731 | |
| 732 | actions.add(pushMplsAction); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 733 | actions.add(copyTtlOutAction); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 734 | actions.add(decMplsTtlAction); |
| 735 | actions.add(setIdAction); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 736 | } |
| 737 | } |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 738 | |
| 739 | GroupAction groupAction = new GroupAction(); |
| 740 | |
| 741 | for (String fwdSw : fwdToSws) { |
| 742 | groupAction.addSwitch(new Dpid(fwdSw)); |
| 743 | } |
| 744 | actions.add(groupAction); |
| 745 | |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 746 | // TODO: Mactch Action Id should be set correctly |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 747 | MatchAction matchAction = new MatchAction(new MatchActionId(0), |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 748 | new SwitchPort((long) 0, (short) 0), ipMatch, actions); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 749 | |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 750 | Operator operator = null; |
| 751 | if (modified) |
| 752 | operator = Operator.MODIFY; |
| 753 | else |
| 754 | operator = Operator.ADD; |
| 755 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 756 | MatchActionOperationEntry maEntry = |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 757 | new MatchActionOperationEntry(operator, matchAction); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 758 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 759 | IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 760 | getSwId(sw.getDpid().toString())); |
| 761 | |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 762 | /* TODO: we should check the SWICH REMOVED events */ |
| 763 | if (sw13 != null) { |
| 764 | try { |
| 765 | printMatchActionOperationEntry(sw, maEntry); |
| 766 | if (entries != null) |
| 767 | entries.add(maEntry); |
| 768 | else |
| 769 | sw13.pushFlow(maEntry); |
| 770 | } catch (IOException e) { |
| 771 | e.printStackTrace(); |
| 772 | } |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 773 | } |
| 774 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 775 | } |
| 776 | |
Sangho Shin | ac5ee2b | 2014-09-28 21:27:20 -0700 | [diff] [blame] | 777 | /** |
| 778 | * Convert a string DPID to its Switch Id (integer) |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 779 | * |
Sangho Shin | ac5ee2b | 2014-09-28 21:27:20 -0700 | [diff] [blame] | 780 | * @param dpid |
| 781 | * @return |
| 782 | */ |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 783 | private long getSwId(String dpid) { |
| 784 | |
| 785 | long swId = 0; |
| 786 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 787 | String swIdStr = dpid.substring(dpid.lastIndexOf(":") + 1); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 788 | if (swIdStr != null) |
| 789 | swId = Integer.parseInt(swIdStr); |
| 790 | |
| 791 | return swId; |
| 792 | } |
| 793 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 794 | /** |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 795 | * Set MPLS forwarding rules to MPLS table - If the destination is the same |
| 796 | * as the next hop to forward packets then, pop the MPLS label according to |
| 797 | * PHP rule - Otherwise, just forward packets to next hops using Group |
| 798 | * action |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 799 | * |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 800 | * @param sw Switch to set the rules |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 801 | * @param mplsLabel destination MPLS label |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 802 | * @param fwdSws next hop switches |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 803 | */ |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 804 | private void setMplsTable(Switch sw, String mplsLabel, List<String> fwdSws, |
| 805 | boolean modified) { |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 806 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 807 | MplsMatch mplsMatch = new MplsMatch(Integer.parseInt(mplsLabel)); |
| 808 | |
| 809 | List<Action> actions = new ArrayList<Action>(); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 810 | |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 811 | // 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] | 812 | // Otherwise, just decrease the MPLS TTL. |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 813 | if (fwdSws.size() == 1) { |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 814 | String fwdMplsId = getMplsLabel(fwdSws.get(0)); |
| 815 | if (fwdMplsId.equals(mplsLabel)) { |
| 816 | String fwdSw = fwdSws.get(0); |
| 817 | if (mplsLabel.equals(getMplsLabel(fwdSw))) { |
| 818 | PopMplsAction popAction = new PopMplsAction(EthType.IPv4); |
| 819 | CopyTtlInAction copyTtlInAction = new CopyTtlInAction(); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 820 | DecNwTtlAction decNwTtlAction = new DecNwTtlAction(1); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 821 | |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 822 | actions.add(copyTtlInAction); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 823 | actions.add(popAction); |
| 824 | actions.add(decNwTtlAction); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 825 | } |
| 826 | } |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 827 | else { |
| 828 | DecMplsTtlAction decMplsTtlAction = new DecMplsTtlAction(1); |
| 829 | actions.add(decMplsTtlAction); |
| 830 | } |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 831 | } |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 832 | GroupAction groupAction = new GroupAction(); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 833 | for (String fwdSw : fwdSws) |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 834 | groupAction.addSwitch(new Dpid(fwdSw)); |
| 835 | actions.add(groupAction); |
| 836 | |
| 837 | MatchAction matchAction = new MatchAction(new MatchActionId(0), |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 838 | new SwitchPort((long) 0, (short) 0), mplsMatch, actions); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 839 | |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 840 | Operator operator = null; |
| 841 | if (modified) |
| 842 | operator = Operator.MODIFY; |
| 843 | else |
| 844 | operator = Operator.ADD; |
| 845 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 846 | MatchActionOperationEntry maEntry = |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 847 | new MatchActionOperationEntry(operator, matchAction); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 848 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 849 | IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 850 | getSwId(sw.getDpid().toString())); |
| 851 | |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 852 | if (sw13 != null) { |
| 853 | try { |
| 854 | printMatchActionOperationEntry(sw, maEntry); |
| 855 | sw13.pushFlow(maEntry); |
| 856 | } catch (IOException e) { |
| 857 | e.printStackTrace(); |
| 858 | } |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 859 | } |
| 860 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 861 | } |
| 862 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 863 | /** |
| 864 | * Debugging function to print out the Match Action Entry |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 865 | * |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 866 | * @param maEntry |
| 867 | */ |
Sangho Shin | ac5ee2b | 2014-09-28 21:27:20 -0700 | [diff] [blame] | 868 | private void printMatchActionOperationEntry(Switch sw, |
| 869 | MatchActionOperationEntry maEntry) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 870 | |
Sangho Shin | 0df0198 | 2014-09-25 17:11:18 -0700 | [diff] [blame] | 871 | StringBuilder logStr = new StringBuilder("In switch " + sw.getDpid() + ", "); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 872 | |
| 873 | MatchAction ma = maEntry.getTarget(); |
| 874 | Match m = ma.getMatch(); |
| 875 | List<Action> actions = ma.getActions(); |
| 876 | |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 877 | if (m instanceof Ipv4Match) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 878 | logStr.append("If the IP matches with "); |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 879 | IPv4Net ip = ((Ipv4Match) m).getDestination(); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 880 | logStr.append(ip.toString()); |
| 881 | logStr.append(" then "); |
| 882 | } |
| 883 | else if (m instanceof MplsMatch) { |
| 884 | logStr.append("If the MPLS label matches with "); |
| 885 | int mplsLabel = ((MplsMatch) m).getMplsLabel(); |
| 886 | logStr.append(mplsLabel); |
| 887 | logStr.append(" then "); |
| 888 | } |
| 889 | |
| 890 | logStr.append(" do { "); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 891 | for (Action action : actions) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 892 | if (action instanceof CopyTtlInAction) { |
| 893 | logStr.append("copy ttl In, "); |
| 894 | } |
| 895 | else if (action instanceof CopyTtlOutAction) { |
| 896 | logStr.append("copy ttl Out, "); |
| 897 | } |
| 898 | else if (action instanceof DecMplsTtlAction) { |
| 899 | logStr.append("Dec MPLS TTL , "); |
| 900 | } |
| 901 | else if (action instanceof GroupAction) { |
| 902 | logStr.append("Forward packet to < "); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 903 | NeighborSet dpids = ((GroupAction) action).getDpids(); |
Sangho Shin | 0df0198 | 2014-09-25 17:11:18 -0700 | [diff] [blame] | 904 | logStr.append(dpids.toString() + ","); |
| 905 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 906 | } |
| 907 | else if (action instanceof PopMplsAction) { |
| 908 | logStr.append("Pop MPLS label, "); |
| 909 | } |
| 910 | else if (action instanceof PushMplsAction) { |
| 911 | logStr.append("Push MPLS label, "); |
| 912 | } |
| 913 | else if (action instanceof SetMplsIdAction) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 914 | int id = ((SetMplsIdAction) action).getMplsId(); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 915 | logStr.append("Set MPLS ID as " + id + ", "); |
| 916 | |
| 917 | } |
| 918 | } |
| 919 | |
| 920 | log.debug(logStr.toString()); |
| 921 | |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | /** |
| 925 | * Get MPLS label reading the config file |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 926 | * |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 927 | * @param dipid DPID of the switch |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 928 | * @return MPLS label for the switch |
| 929 | */ |
| 930 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 931 | private String getMplsLabel(String dpid) { |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 932 | |
| 933 | String mplsLabel = null; |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 934 | for (Switch sw : mutableTopology.getSwitches()) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 935 | String dpidStr = sw.getDpid().toString(); |
| 936 | if (dpid.equals(dpidStr)) { |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 937 | mplsLabel = sw.getStringAttribute("nodeSid"); |
| 938 | break; |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 939 | } |
| 940 | } |
| 941 | |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 942 | return mplsLabel; |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 943 | } |
| 944 | |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 945 | /** |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 946 | * The function checks if given IP matches to the given subnet mask |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 947 | * |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 948 | * @param addr - subnet address to match |
| 949 | * @param addr1 - IP address to check |
| 950 | * @return true if the IP address matches to the subnet, otherwise false |
| 951 | */ |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 952 | public boolean netMatch(String addr, String addr1) { // addr is subnet |
| 953 | // address and addr1 is |
| 954 | // ip address. Function |
| 955 | // will return true, if |
| 956 | // addr1 is within |
| 957 | // addr(subnet) |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 958 | |
| 959 | String[] parts = addr.split("/"); |
| 960 | String ip = parts[0]; |
| 961 | int prefix; |
| 962 | |
| 963 | if (parts.length < 2) { |
| 964 | prefix = 0; |
| 965 | } else { |
| 966 | prefix = Integer.parseInt(parts[1]); |
| 967 | } |
| 968 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 969 | Inet4Address a = null; |
| 970 | Inet4Address a1 = null; |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 971 | try { |
| 972 | a = (Inet4Address) InetAddress.getByName(ip); |
| 973 | a1 = (Inet4Address) InetAddress.getByName(addr1); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 974 | } catch (UnknownHostException e) { |
| 975 | } |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 976 | |
| 977 | byte[] b = a.getAddress(); |
| 978 | int ipInt = ((b[0] & 0xFF) << 24) | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 979 | ((b[1] & 0xFF) << 16) | |
| 980 | ((b[2] & 0xFF) << 8) | |
| 981 | ((b[3] & 0xFF) << 0); |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 982 | |
| 983 | byte[] b1 = a1.getAddress(); |
| 984 | int ipInt1 = ((b1[0] & 0xFF) << 24) | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 985 | ((b1[1] & 0xFF) << 16) | |
| 986 | ((b1[2] & 0xFF) << 8) | |
| 987 | ((b1[3] & 0xFF) << 0); |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 988 | |
| 989 | int mask = ~((1 << (32 - prefix)) - 1); |
| 990 | |
| 991 | if ((ipInt & mask) == (ipInt1 & mask)) { |
| 992 | return true; |
| 993 | } |
| 994 | else { |
| 995 | return false; |
| 996 | } |
| 997 | } |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 998 | |
Sangho Shin | ac5ee2b | 2014-09-28 21:27:20 -0700 | [diff] [blame] | 999 | /** |
| 1000 | * Add a routing rule for the host |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 1001 | * |
Sangho Shin | ac5ee2b | 2014-09-28 21:27:20 -0700 | [diff] [blame] | 1002 | * @param sw - Switch to add the rule |
| 1003 | * @param hostIpAddress Destination host IP address |
| 1004 | * @param hostMacAddress Destination host MAC address |
| 1005 | */ |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 1006 | public void addRouteToHost(Switch sw, int hostIpAddress, byte[] hostMacAddress) { |
| 1007 | ipHandler.addRouteToHost(sw, hostIpAddress, hostMacAddress); |
| 1008 | |
| 1009 | } |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 1010 | |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 1011 | /** |
| 1012 | * Add IP packet to a buffer queue |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 1013 | * |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 1014 | * @param ipv4 |
| 1015 | */ |
| 1016 | public void addPacket(IPv4 ipv4) { |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 1017 | ipPacketQueue.add(ipv4); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | /** |
| 1021 | * Retrieve all packets whose destination is the given address. |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 1022 | * |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 1023 | * @param destIp Destination address of packets to retrieve |
| 1024 | */ |
| 1025 | public List<IPv4> getIpPacketFromQueue(byte[] destIp) { |
| 1026 | |
| 1027 | List<IPv4> bufferedPackets = new ArrayList<IPv4>(); |
| 1028 | |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 1029 | if (!ipPacketQueue.isEmpty()) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 1030 | for (IPv4 ip : ipPacketQueue) { |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 1031 | int dest = ip.getDestinationAddress(); |
| 1032 | IPv4Address ip1 = IPv4Address.of(dest); |
| 1033 | IPv4Address ip2 = IPv4Address.of(destIp); |
| 1034 | if (ip1.equals(ip2)) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 1035 | bufferedPackets.add((IPv4) (ipPacketQueue.poll()).clone()); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 1036 | } |
| 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | return bufferedPackets; |
| 1041 | } |
| 1042 | |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 1043 | } |