HIGUCHI Yuta | ea60e5f | 2013-06-12 11:10:21 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.bgproute; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 2 | |
Jonathan Hart | d1f2325 | 2013-06-13 15:17:05 +1200 | [diff] [blame] | 3 | import java.io.File; |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 4 | import java.io.IOException; |
| 5 | import java.net.InetAddress; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 6 | import java.util.ArrayList; |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 7 | import java.util.Collection; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 8 | import java.util.HashMap; |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 9 | import java.util.Iterator; |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 10 | import java.util.List; |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 11 | import java.util.Map; |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 12 | import java.util.Set; |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 13 | import java.util.concurrent.BlockingQueue; |
| 14 | import java.util.concurrent.ExecutorService; |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 15 | import java.util.concurrent.Executors; |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 16 | import java.util.concurrent.LinkedBlockingQueue; |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 17 | import java.util.concurrent.ScheduledExecutorService; |
| 18 | import java.util.concurrent.TimeUnit; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 19 | |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 20 | import net.floodlightcontroller.core.IFloodlightProviderService; |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 21 | import net.floodlightcontroller.core.IOFSwitch; |
Jonathan Hart | 64c0b20 | 2013-08-20 15:45:07 +1200 | [diff] [blame] | 22 | import net.floodlightcontroller.core.IOFSwitchListener; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 23 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 24 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 25 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 26 | import net.floodlightcontroller.core.module.IFloodlightService; |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 27 | import net.floodlightcontroller.core.util.SingletonTask; |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 28 | import net.floodlightcontroller.packet.Ethernet; |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 29 | import net.floodlightcontroller.packet.IPv4; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 30 | import net.floodlightcontroller.restserver.IRestApiService; |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 31 | import net.floodlightcontroller.routing.Link; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 32 | import net.floodlightcontroller.topology.ITopologyListener; |
| 33 | import net.floodlightcontroller.topology.ITopologyService; |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 34 | import net.floodlightcontroller.util.MACAddress; |
Jonathan Hart | 0ee0f02 | 2013-08-03 22:21:54 +1200 | [diff] [blame] | 35 | import net.onrc.onos.ofcontroller.bgproute.RibUpdate.Operation; |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 36 | import net.onrc.onos.ofcontroller.core.INetMapTopologyService.ITopoLinkService; |
HIGUCHI Yuta | 2051490 | 2013-06-12 11:24:16 -0700 | [diff] [blame] | 37 | import net.onrc.onos.ofcontroller.core.INetMapTopologyService.ITopoRouteService; |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 38 | import net.onrc.onos.ofcontroller.core.internal.TopoLinkServiceImpl; |
HIGUCHI Yuta | a56fbde | 2013-06-17 14:26:05 -0700 | [diff] [blame] | 39 | import net.onrc.onos.ofcontroller.linkdiscovery.ILinkDiscovery; |
| 40 | import net.onrc.onos.ofcontroller.linkdiscovery.ILinkDiscovery.LDUpdate; |
Jonathan Hart | e769453 | 2013-09-12 12:34:46 +1200 | [diff] [blame] | 41 | import net.onrc.onos.ofcontroller.linkdiscovery.ILinkDiscoveryService; |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 42 | import net.onrc.onos.ofcontroller.proxyarp.IArpRequester; |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 43 | import net.onrc.onos.ofcontroller.proxyarp.ProxyArpManager; |
Pavlin Radoslavov | ddd01ba | 2013-07-03 15:40:44 -0700 | [diff] [blame] | 44 | import net.onrc.onos.ofcontroller.routing.TopoRouteService; |
HIGUCHI Yuta | 356086e | 2013-06-12 15:21:19 -0700 | [diff] [blame] | 45 | import net.onrc.onos.ofcontroller.util.DataPath; |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 46 | import net.onrc.onos.ofcontroller.util.Dpid; |
HIGUCHI Yuta | 356086e | 2013-06-12 15:21:19 -0700 | [diff] [blame] | 47 | import net.onrc.onos.ofcontroller.util.FlowEntry; |
HIGUCHI Yuta | 356086e | 2013-06-12 15:21:19 -0700 | [diff] [blame] | 48 | import net.onrc.onos.ofcontroller.util.Port; |
| 49 | import net.onrc.onos.ofcontroller.util.SwitchPort; |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 50 | import net.sf.json.JSONArray; |
| 51 | import net.sf.json.JSONObject; |
| 52 | import net.sf.json.JSONSerializer; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 53 | |
Jonathan Hart | d1f2325 | 2013-06-13 15:17:05 +1200 | [diff] [blame] | 54 | import org.codehaus.jackson.JsonParseException; |
| 55 | import org.codehaus.jackson.map.JsonMappingException; |
| 56 | import org.codehaus.jackson.map.ObjectMapper; |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 57 | import org.openflow.protocol.OFFlowMod; |
| 58 | import org.openflow.protocol.OFMatch; |
| 59 | import org.openflow.protocol.OFMessage; |
| 60 | import org.openflow.protocol.OFPacketOut; |
Jonathan Hart | 9575cb6 | 2013-07-05 13:43:49 +1200 | [diff] [blame] | 61 | import org.openflow.protocol.OFPort; |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 62 | import org.openflow.protocol.OFType; |
| 63 | import org.openflow.protocol.action.OFAction; |
| 64 | import org.openflow.protocol.action.OFActionDataLayerDestination; |
| 65 | import org.openflow.protocol.action.OFActionOutput; |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 66 | import org.openflow.util.HexString; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 67 | import org.slf4j.Logger; |
| 68 | import org.slf4j.LoggerFactory; |
| 69 | |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 70 | import com.google.common.collect.HashMultimap; |
Jonathan Hart | 0ee0f02 | 2013-08-03 22:21:54 +1200 | [diff] [blame] | 71 | import com.google.common.collect.Multimap; |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 72 | import com.google.common.collect.Multimaps; |
| 73 | import com.google.common.collect.SetMultimap; |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 74 | import com.google.common.net.InetAddresses; |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 75 | import com.google.common.util.concurrent.ThreadFactoryBuilder; |
| 76 | |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 77 | public class BgpRoute implements IFloodlightModule, IBgpRouteService, |
Jonathan Hart | 64c0b20 | 2013-08-20 15:45:07 +1200 | [diff] [blame] | 78 | ITopologyListener, IArpRequester, |
Jonathan Hart | 08ee852 | 2013-09-22 17:34:43 +1200 | [diff] [blame] | 79 | IOFSwitchListener, ILayer3InfoService { |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 80 | |
| 81 | protected static Logger log = LoggerFactory.getLogger(BgpRoute.class); |
| 82 | |
| 83 | protected IFloodlightProviderService floodlightProvider; |
| 84 | protected ITopologyService topology; |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 85 | protected ITopoRouteService topoRouteService; |
Jonathan Hart | e769453 | 2013-09-12 12:34:46 +1200 | [diff] [blame] | 86 | protected ILinkDiscoveryService linkDiscoveryService; |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 87 | protected IRestApiService restApi; |
| 88 | |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 89 | protected ProxyArpManager proxyArp; |
| 90 | |
Jonathan Hart | 29b972d | 2013-08-12 23:43:51 +1200 | [diff] [blame] | 91 | protected IPatriciaTrie<RibEntry> ptree; |
Jonathan Hart | abf1022 | 2013-08-13 10:19:34 +1200 | [diff] [blame] | 92 | protected IPatriciaTrie<Interface> interfacePtrie; |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 93 | protected BlockingQueue<RibUpdate> ribUpdates; |
| 94 | |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 95 | protected String bgpdRestIp; |
| 96 | protected String routerId; |
Jonathan Hart | 9575cb6 | 2013-07-05 13:43:49 +1200 | [diff] [blame] | 97 | protected String configFilename = "config.json"; |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 98 | |
| 99 | //We need to identify our flows somehow. But like it says in LearningSwitch.java, |
| 100 | //the controller/OS should hand out cookie IDs to prevent conflicts. |
| 101 | protected final long APP_COOKIE = 0xa0000000000000L; |
| 102 | //Cookie for flows that do L2 forwarding within SDN domain to egress routers |
| 103 | protected final long L2_FWD_COOKIE = APP_COOKIE + 1; |
| 104 | //Cookie for flows in ingress switches that rewrite the MAC address |
| 105 | protected final long MAC_RW_COOKIE = APP_COOKIE + 2; |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 106 | //Cookie for flows that setup BGP paths |
| 107 | protected final long BGP_COOKIE = APP_COOKIE + 3; |
Jonathan Hart | 50a8d1e | 2013-06-06 16:00:47 +1200 | [diff] [blame] | 108 | //Forwarding uses priority 0, and the mac rewrite entries in ingress switches |
| 109 | //need to be higher priority than this otherwise the rewrite may not get done |
| 110 | protected final short SDNIP_PRIORITY = 10; |
Jonathan Hart | c82051c | 2013-08-24 15:12:20 +1200 | [diff] [blame] | 111 | protected final short ARP_PRIORITY = 20; |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 112 | |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 113 | protected final short BGP_PORT = 179; |
| 114 | |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 115 | protected final int TOPO_DETECTION_WAIT = 2; //seconds |
| 116 | |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 117 | //Configuration stuff |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 118 | protected List<String> switches; |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 119 | protected Map<String, Interface> interfaces; |
Jonathan Hart | c824ad0 | 2013-07-03 15:58:45 +1200 | [diff] [blame] | 120 | protected Map<InetAddress, BgpPeer> bgpPeers; |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 121 | protected SwitchPort bgpdAttachmentPoint; |
Jonathan Hart | 2f790d2 | 2013-08-15 14:01:24 +1200 | [diff] [blame] | 122 | protected MACAddress bgpdMacAddress; |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 123 | |
| 124 | //True when all switches have connected |
| 125 | protected volatile boolean switchesConnected = false; |
| 126 | //True when we have a full mesh of shortest paths between gateways |
| 127 | protected volatile boolean topologyReady = false; |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 128 | |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 129 | protected ArrayList<LDUpdate> linkUpdates; |
| 130 | protected SingletonTask topologyChangeDetectorTask; |
| 131 | |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 132 | protected SetMultimap<InetAddress, RibUpdate> prefixesWaitingOnArp; |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 133 | |
Jonathan Hart | a23ffdb | 2013-08-14 14:36:54 +1200 | [diff] [blame] | 134 | protected Map<InetAddress, Path> pathsWaitingOnArp; |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 135 | |
Jonathan Hart | 0a46fe4 | 2013-08-10 17:08:47 +1200 | [diff] [blame] | 136 | protected ExecutorService bgpUpdatesExecutor; |
| 137 | |
Jonathan Hart | a23ffdb | 2013-08-14 14:36:54 +1200 | [diff] [blame] | 138 | protected Map<InetAddress, Path> pushedPaths; |
| 139 | protected Map<Prefix, Path> prefixToPath; |
Jonathan Hart | 0ee0f02 | 2013-08-03 22:21:54 +1200 | [diff] [blame] | 140 | protected Multimap<Prefix, PushedFlowMod> pushedFlows; |
Jonathan Hart | defa44d | 2013-08-15 19:51:13 +1200 | [diff] [blame] | 141 | |
| 142 | protected volatile Map<Long, ?> topoRouteTopology = null; |
Jonathan Hart | 0ee0f02 | 2013-08-03 22:21:54 +1200 | [diff] [blame] | 143 | |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 144 | protected class TopologyChangeDetector implements Runnable { |
| 145 | @Override |
| 146 | public void run() { |
| 147 | log.debug("Running topology change detection task"); |
| 148 | synchronized (linkUpdates) { |
Jonathan Hart | c824ad0 | 2013-07-03 15:58:45 +1200 | [diff] [blame] | 149 | //This is the model the REST API uses to retrieve network graph info |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 150 | ITopoLinkService topoLinkService = new TopoLinkServiceImpl(); |
| 151 | |
| 152 | List<Link> activeLinks = topoLinkService.getActiveLinks(); |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 153 | |
| 154 | Iterator<LDUpdate> it = linkUpdates.iterator(); |
| 155 | while (it.hasNext()){ |
| 156 | LDUpdate ldu = it.next(); |
| 157 | Link l = new Link(ldu.getSrc(), ldu.getSrcPort(), |
| 158 | ldu.getDst(), ldu.getDstPort()); |
| 159 | |
| 160 | if (activeLinks.contains(l)){ |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 161 | it.remove(); |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
Jonathan Hart | 64c0b20 | 2013-08-20 15:45:07 +1200 | [diff] [blame] | 166 | if (!topologyReady) { |
| 167 | if (linkUpdates.isEmpty()){ |
| 168 | //All updates have been seen in network map. |
| 169 | //We can check if topology is ready |
| 170 | log.debug("No known changes outstanding. Checking topology now"); |
| 171 | checkStatus(); |
| 172 | } |
| 173 | else { |
| 174 | //We know of some link updates that haven't propagated to the database yet |
| 175 | log.debug("Some changes not found in network map - {} links missing", linkUpdates.size()); |
| 176 | topologyChangeDetectorTask.reschedule(TOPO_DETECTION_WAIT, TimeUnit.SECONDS); |
| 177 | } |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | } |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 181 | |
Jonathan Hart | d1f2325 | 2013-06-13 15:17:05 +1200 | [diff] [blame] | 182 | private void readGatewaysConfiguration(String gatewaysFilename){ |
| 183 | File gatewaysFile = new File(gatewaysFilename); |
| 184 | ObjectMapper mapper = new ObjectMapper(); |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 185 | |
Jonathan Hart | d1f2325 | 2013-06-13 15:17:05 +1200 | [diff] [blame] | 186 | try { |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 187 | Configuration config = mapper.readValue(gatewaysFile, Configuration.class); |
| 188 | |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 189 | switches = config.getSwitches(); |
Jonathan Hart | 9575cb6 | 2013-07-05 13:43:49 +1200 | [diff] [blame] | 190 | interfaces = new HashMap<String, Interface>(); |
| 191 | for (Interface intf : config.getInterfaces()){ |
| 192 | interfaces.put(intf.getName(), intf); |
| 193 | } |
Jonathan Hart | c824ad0 | 2013-07-03 15:58:45 +1200 | [diff] [blame] | 194 | bgpPeers = new HashMap<InetAddress, BgpPeer>(); |
| 195 | for (BgpPeer peer : config.getPeers()){ |
| 196 | bgpPeers.put(peer.getIpAddress(), peer); |
| 197 | } |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 198 | |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 199 | bgpdAttachmentPoint = new SwitchPort( |
| 200 | new Dpid(config.getBgpdAttachmentDpid()), |
| 201 | new Port(config.getBgpdAttachmentPort())); |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 202 | |
Jonathan Hart | 2f790d2 | 2013-08-15 14:01:24 +1200 | [diff] [blame] | 203 | bgpdMacAddress = config.getBgpdMacAddress(); |
Jonathan Hart | d1f2325 | 2013-06-13 15:17:05 +1200 | [diff] [blame] | 204 | } catch (JsonParseException e) { |
| 205 | log.error("Error in JSON file", e); |
| 206 | System.exit(1); |
| 207 | } catch (JsonMappingException e) { |
| 208 | log.error("Error in JSON file", e); |
| 209 | System.exit(1); |
| 210 | } catch (IOException e) { |
| 211 | log.error("Error reading JSON file", e); |
| 212 | System.exit(1); |
| 213 | } |
Jonathan Hart | abf1022 | 2013-08-13 10:19:34 +1200 | [diff] [blame] | 214 | |
| 215 | //Populate the interface Patricia Trie |
| 216 | for (Interface intf : interfaces.values()) { |
| 217 | Prefix prefix = new Prefix(intf.getIpAddress().getAddress(), intf.getPrefixLength()); |
| 218 | interfacePtrie.put(prefix, intf); |
| 219 | } |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 220 | } |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 221 | |
| 222 | @Override |
| 223 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 224 | Collection<Class<? extends IFloodlightService>> l |
| 225 | = new ArrayList<Class<? extends IFloodlightService>>(); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 226 | l.add(IBgpRouteService.class); |
| 227 | return l; |
| 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 232 | Map<Class<? extends IFloodlightService>, IFloodlightService> m |
| 233 | = new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(); |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 234 | m.put(IBgpRouteService.class, this); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 235 | return m; |
| 236 | } |
| 237 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 238 | @Override |
| 239 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 240 | Collection<Class<? extends IFloodlightService>> l |
| 241 | = new ArrayList<Class<? extends IFloodlightService>>(); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 242 | l.add(IFloodlightProviderService.class); |
| 243 | l.add(ITopologyService.class); |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 244 | l.add(IRestApiService.class); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 245 | return l; |
| 246 | } |
| 247 | |
| 248 | @Override |
| 249 | public void init(FloodlightModuleContext context) |
| 250 | throws FloodlightModuleException { |
| 251 | |
Jonathan Hart | 29b972d | 2013-08-12 23:43:51 +1200 | [diff] [blame] | 252 | ptree = new PatriciaTrie<RibEntry>(32); |
Jonathan Hart | abf1022 | 2013-08-13 10:19:34 +1200 | [diff] [blame] | 253 | interfacePtrie = new PatriciaTrie<Interface>(32); |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 254 | |
| 255 | ribUpdates = new LinkedBlockingQueue<RibUpdate>(); |
Jonathan Hart | c824ad0 | 2013-07-03 15:58:45 +1200 | [diff] [blame] | 256 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 257 | // Register floodlight provider and REST handler. |
| 258 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 259 | topology = context.getServiceImpl(ITopologyService.class); |
Jonathan Hart | e769453 | 2013-09-12 12:34:46 +1200 | [diff] [blame] | 260 | linkDiscoveryService = context.getServiceImpl(ILinkDiscoveryService.class); |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 261 | restApi = context.getServiceImpl(IRestApiService.class); |
| 262 | |
| 263 | //TODO We'll initialise this here for now, but it should really be done as |
| 264 | //part of the controller core |
Jonathan Hart | 08ee852 | 2013-09-22 17:34:43 +1200 | [diff] [blame] | 265 | proxyArp = new ProxyArpManager(floodlightProvider, topology, this); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 266 | |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 267 | linkUpdates = new ArrayList<LDUpdate>(); |
| 268 | ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); |
| 269 | topologyChangeDetectorTask = new SingletonTask(executor, new TopologyChangeDetector()); |
Pavlin Radoslavov | ddd01ba | 2013-07-03 15:40:44 -0700 | [diff] [blame] | 270 | |
| 271 | topoRouteService = new TopoRouteService(""); |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 272 | |
Jonathan Hart | a23ffdb | 2013-08-14 14:36:54 +1200 | [diff] [blame] | 273 | pathsWaitingOnArp = new HashMap<InetAddress, Path>(); |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 274 | prefixesWaitingOnArp = Multimaps.synchronizedSetMultimap( |
| 275 | HashMultimap.<InetAddress, RibUpdate>create()); |
| 276 | |
Jonathan Hart | a23ffdb | 2013-08-14 14:36:54 +1200 | [diff] [blame] | 277 | pushedPaths = new HashMap<InetAddress, Path>(); |
| 278 | prefixToPath = new HashMap<Prefix, Path>(); |
Jonathan Hart | 0ee0f02 | 2013-08-03 22:21:54 +1200 | [diff] [blame] | 279 | pushedFlows = HashMultimap.<Prefix, PushedFlowMod>create(); |
| 280 | |
Jonathan Hart | 0a46fe4 | 2013-08-10 17:08:47 +1200 | [diff] [blame] | 281 | bgpUpdatesExecutor = Executors.newSingleThreadExecutor( |
| 282 | new ThreadFactoryBuilder().setNameFormat("bgp-updates-%d").build()); |
| 283 | |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 284 | //Read in config values |
| 285 | bgpdRestIp = context.getConfigParams(this).get("BgpdRestIp"); |
| 286 | if (bgpdRestIp == null){ |
| 287 | log.error("BgpdRestIp property not found in config file"); |
| 288 | System.exit(1); |
| 289 | } |
| 290 | else { |
| 291 | log.info("BgpdRestIp set to {}", bgpdRestIp); |
| 292 | } |
| 293 | |
| 294 | routerId = context.getConfigParams(this).get("RouterId"); |
| 295 | if (routerId == null){ |
| 296 | log.error("RouterId property not found in config file"); |
| 297 | System.exit(1); |
| 298 | } |
| 299 | else { |
| 300 | log.info("RouterId set to {}", routerId); |
| 301 | } |
Jonathan Hart | d1f2325 | 2013-06-13 15:17:05 +1200 | [diff] [blame] | 302 | |
Jonathan Hart | 9575cb6 | 2013-07-05 13:43:49 +1200 | [diff] [blame] | 303 | String configFilenameParameter = context.getConfigParams(this).get("configfile"); |
| 304 | if (configFilenameParameter != null){ |
| 305 | configFilename = configFilenameParameter; |
| 306 | } |
| 307 | log.debug("Config file set to {}", configFilename); |
| 308 | |
| 309 | readGatewaysConfiguration(configFilename); |
Jonathan Hart | a23ffdb | 2013-08-14 14:36:54 +1200 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | @Override |
| 313 | public void startUp(FloodlightModuleContext context) { |
| 314 | restApi.addRestletRoutable(new BgpRouteWebRoutable()); |
| 315 | topology.addListener(this); |
Jonathan Hart | 64c0b20 | 2013-08-20 15:45:07 +1200 | [diff] [blame] | 316 | floodlightProvider.addOFSwitchListener(this); |
Jonathan Hart | a23ffdb | 2013-08-14 14:36:54 +1200 | [diff] [blame] | 317 | |
Jonathan Hart | 2f790d2 | 2013-08-15 14:01:24 +1200 | [diff] [blame] | 318 | proxyArp.startUp(); |
| 319 | |
Jonathan Hart | a23ffdb | 2013-08-14 14:36:54 +1200 | [diff] [blame] | 320 | floodlightProvider.addOFMessageListener(OFType.PACKET_IN, proxyArp); |
| 321 | |
| 322 | //Retrieve the RIB from BGPd during startup |
| 323 | retrieveRib(); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 324 | } |
| 325 | |
Jonathan Hart | 29b972d | 2013-08-12 23:43:51 +1200 | [diff] [blame] | 326 | public IPatriciaTrie<RibEntry> getPtree() { |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 327 | return ptree; |
| 328 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 329 | |
| 330 | public void clearPtree() { |
Jonathan Hart | 29b972d | 2013-08-12 23:43:51 +1200 | [diff] [blame] | 331 | ptree = new PatriciaTrie<RibEntry>(32); |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 332 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 333 | |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 334 | public String getBGPdRestIp() { |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 335 | return bgpdRestIp; |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 336 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 337 | |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 338 | public String getRouterId() { |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 339 | return routerId; |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 340 | } |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 341 | |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 342 | private void retrieveRib(){ |
| 343 | String url = "http://" + bgpdRestIp + "/wm/bgp/" + routerId; |
| 344 | String response = RestClient.get(url); |
| 345 | |
| 346 | if (response.equals("")){ |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | response = response.replaceAll("\"", "'"); |
| 351 | JSONObject jsonObj = (JSONObject) JSONSerializer.toJSON(response); |
| 352 | JSONArray rib_json_array = jsonObj.getJSONArray("rib"); |
| 353 | String router_id = jsonObj.getString("router-id"); |
| 354 | |
| 355 | int size = rib_json_array.size(); |
| 356 | |
| 357 | log.info("Retrived RIB of {} entries from BGPd", size); |
| 358 | |
| 359 | for (int j = 0; j < size; j++) { |
| 360 | JSONObject second_json_object = rib_json_array.getJSONObject(j); |
| 361 | String prefix = second_json_object.getString("prefix"); |
| 362 | String nexthop = second_json_object.getString("nexthop"); |
| 363 | |
| 364 | //insert each rib entry into the local rib; |
| 365 | String[] substring = prefix.split("/"); |
| 366 | String prefix1 = substring[0]; |
| 367 | String mask1 = substring[1]; |
| 368 | |
| 369 | Prefix p; |
| 370 | try { |
| 371 | p = new Prefix(prefix1, Integer.valueOf(mask1)); |
| 372 | } catch (NumberFormatException e) { |
| 373 | log.warn("Wrong mask format in RIB JSON: {}", mask1); |
| 374 | continue; |
Jonathan Hart | 32e1822 | 2013-08-07 22:05:42 +1200 | [diff] [blame] | 375 | } catch (IllegalArgumentException e1) { |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 376 | log.warn("Wrong prefix format in RIB JSON: {}", prefix1); |
| 377 | continue; |
| 378 | } |
| 379 | |
Jonathan Hart | b39a67d | 2013-08-10 23:59:50 +1200 | [diff] [blame] | 380 | RibEntry rib = new RibEntry(router_id, nexthop); |
Jonathan Hart | a23ffdb | 2013-08-14 14:36:54 +1200 | [diff] [blame] | 381 | |
Jonathan Hart | 0a46fe4 | 2013-08-10 17:08:47 +1200 | [diff] [blame] | 382 | try { |
| 383 | ribUpdates.put(new RibUpdate(Operation.UPDATE, p, rib)); |
| 384 | } catch (InterruptedException e) { |
| 385 | log.debug("Interrupted while pushing onto update queue"); |
| 386 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 390 | @Override |
| 391 | public void newRibUpdate(RibUpdate update) { |
Jonathan Hart | 9ea3121 | 2013-08-12 21:40:34 +1200 | [diff] [blame] | 392 | try { |
| 393 | ribUpdates.put(update); |
| 394 | } catch (InterruptedException e) { |
Jonathan Hart | a23ffdb | 2013-08-14 14:36:54 +1200 | [diff] [blame] | 395 | log.debug("Interrupted while putting on ribUpdates queue", e); |
| 396 | Thread.currentThread().interrupt(); |
Jonathan Hart | 9ea3121 | 2013-08-12 21:40:34 +1200 | [diff] [blame] | 397 | } |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 398 | } |
| 399 | |
Jonathan Hart | 0a46fe4 | 2013-08-10 17:08:47 +1200 | [diff] [blame] | 400 | public synchronized void processRibAdd(RibUpdate update) { |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 401 | Prefix prefix = update.getPrefix(); |
| 402 | |
Jonathan Hart | 9ea3121 | 2013-08-12 21:40:34 +1200 | [diff] [blame] | 403 | log.debug("Processing prefix add {}", prefix); |
| 404 | |
Jonathan Hart | b39a67d | 2013-08-10 23:59:50 +1200 | [diff] [blame] | 405 | RibEntry rib = ptree.put(prefix, update.getRibEntry()); |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 406 | |
Jonathan Hart | 0a46fe4 | 2013-08-10 17:08:47 +1200 | [diff] [blame] | 407 | if (rib != null && !rib.equals(update.getRibEntry())) { |
Jonathan Hart | 2f74078 | 2013-08-04 00:49:21 +1200 | [diff] [blame] | 408 | //There was an existing nexthop for this prefix. This update supersedes that, |
| 409 | //so we need to remove the old flows for this prefix from the switches |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 410 | _processDeletePrefix(prefix, rib); |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 411 | } |
Jonathan Hart | 2f74078 | 2013-08-04 00:49:21 +1200 | [diff] [blame] | 412 | |
Jonathan Hart | a23ffdb | 2013-08-14 14:36:54 +1200 | [diff] [blame] | 413 | if (update.getRibEntry().getNextHop().equals( |
| 414 | InetAddresses.forString("0.0.0.0"))) { |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 415 | //Route originated by SDN domain |
| 416 | //We don't handle these at the moment |
Jonathan Hart | a23ffdb | 2013-08-14 14:36:54 +1200 | [diff] [blame] | 417 | log.debug("Own route {} to {}", prefix, |
| 418 | update.getRibEntry().getNextHop().getHostAddress()); |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 419 | return; |
| 420 | } |
| 421 | |
| 422 | _processRibAdd(update); |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 423 | } |
| 424 | |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 425 | private void _processRibAdd(RibUpdate update) { |
| 426 | Prefix prefix = update.getPrefix(); |
| 427 | RibEntry rib = update.getRibEntry(); |
| 428 | |
| 429 | InetAddress dstIpAddress = rib.getNextHop(); |
| 430 | |
Jonathan Hart | a23ffdb | 2013-08-14 14:36:54 +1200 | [diff] [blame] | 431 | //See if we know the MAC address of the next hop |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 432 | MACAddress nextHopMacAddress = proxyArp.getMacAddress(rib.getNextHop()); |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 433 | |
Jonathan Hart | a23ffdb | 2013-08-14 14:36:54 +1200 | [diff] [blame] | 434 | //Find the attachment point (egress interface) of the next hop |
| 435 | Interface egressInterface = null; |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 436 | if (bgpPeers.containsKey(dstIpAddress)) { |
| 437 | //Route to a peer |
| 438 | log.debug("Route to peer {}", dstIpAddress); |
| 439 | BgpPeer peer = bgpPeers.get(dstIpAddress); |
| 440 | egressInterface = interfaces.get(peer.getInterfaceName()); |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 441 | } |
| 442 | else { |
Jonathan Hart | a23ffdb | 2013-08-14 14:36:54 +1200 | [diff] [blame] | 443 | //Route to non-peer |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 444 | log.debug("Route to non-peer {}", dstIpAddress); |
| 445 | egressInterface = interfacePtrie.match( |
| 446 | new Prefix(dstIpAddress.getAddress(), 32)); |
| 447 | if (egressInterface == null) { |
| 448 | log.warn("No outgoing interface found for {}", dstIpAddress.getHostAddress()); |
| 449 | return; |
| 450 | } |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | if (nextHopMacAddress == null) { |
| 454 | prefixesWaitingOnArp.put(dstIpAddress, |
| 455 | new RibUpdate(Operation.UPDATE, prefix, rib)); |
| 456 | proxyArp.sendArpRequest(dstIpAddress, this, true); |
| 457 | return; |
| 458 | } |
| 459 | else { |
| 460 | if (!bgpPeers.containsKey(dstIpAddress)) { |
Jonathan Hart | a23ffdb | 2013-08-14 14:36:54 +1200 | [diff] [blame] | 461 | //If the prefix is for a non-peer we need to ensure there's a path, |
| 462 | //and push one if there isn't. |
| 463 | Path path = pushedPaths.get(dstIpAddress); |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 464 | if (path == null) { |
Jonathan Hart | a23ffdb | 2013-08-14 14:36:54 +1200 | [diff] [blame] | 465 | path = new Path(egressInterface, dstIpAddress); |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 466 | calculateAndPushPath(path, nextHopMacAddress); |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 467 | pushedPaths.put(dstIpAddress, path); |
| 468 | } |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 469 | |
| 470 | path.incrementUsers(); |
| 471 | prefixToPath.put(prefix, path); |
| 472 | } |
Jonathan Hart | a23ffdb | 2013-08-14 14:36:54 +1200 | [diff] [blame] | 473 | |
| 474 | //For all prefixes we need to add the first-hop mac-rewriting flows |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 475 | addPrefixFlows(prefix, egressInterface, nextHopMacAddress); |
| 476 | } |
| 477 | } |
| 478 | |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 479 | private void addPrefixFlows(Prefix prefix, Interface egressInterface, MACAddress nextHopMacAddress) { |
Jonathan Hart | a23ffdb | 2013-08-14 14:36:54 +1200 | [diff] [blame] | 480 | log.debug("Adding flows for prefix {} added, next hop mac {}", |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 481 | prefix, nextHopMacAddress); |
Jonathan Hart | d5f2e95 | 2013-09-13 16:31:23 +1200 | [diff] [blame] | 482 | |
| 483 | //We only need one flow mod per switch, so pick one interface on each switch |
| 484 | Map<Long, Interface> srcInterfaces = new HashMap<Long, Interface>(); |
| 485 | for (Interface intf : interfaces.values()) { |
| 486 | if (!srcInterfaces.containsKey(intf.getDpid()) |
| 487 | && intf != egressInterface) { |
| 488 | srcInterfaces.put(intf.getDpid(), intf); |
Jonathan Hart | a23ffdb | 2013-08-14 14:36:54 +1200 | [diff] [blame] | 489 | } |
Jonathan Hart | d5f2e95 | 2013-09-13 16:31:23 +1200 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | //Add a flow to rewrite mac for this prefix to all other border switches |
Jonathan Hart | d5f2e95 | 2013-09-13 16:31:23 +1200 | [diff] [blame] | 493 | for (Interface srcInterface : srcInterfaces.values()) { |
Jonathan Hart | defa44d | 2013-08-15 19:51:13 +1200 | [diff] [blame] | 494 | DataPath shortestPath; |
| 495 | if (topoRouteTopology == null) { |
| 496 | shortestPath = topoRouteService.getShortestPath( |
| 497 | srcInterface.getSwitchPort(), |
| 498 | egressInterface.getSwitchPort()); |
| 499 | } |
| 500 | else { |
| 501 | shortestPath = topoRouteService.getTopoShortestPath( |
| 502 | topoRouteTopology, srcInterface.getSwitchPort(), |
| 503 | egressInterface.getSwitchPort()); |
| 504 | } |
Jonathan Hart | a23ffdb | 2013-08-14 14:36:54 +1200 | [diff] [blame] | 505 | |
| 506 | if (shortestPath == null){ |
| 507 | log.debug("Shortest path between {} and {} not found", |
| 508 | srcInterface.getSwitchPort(), |
| 509 | egressInterface.getSwitchPort()); |
| 510 | return; // just quit here? |
| 511 | } |
| 512 | |
| 513 | //Set up the flow mod |
| 514 | OFFlowMod fm = (OFFlowMod) floodlightProvider.getOFMessageFactory() |
| 515 | .getMessage(OFType.FLOW_MOD); |
| 516 | |
| 517 | fm.setIdleTimeout((short)0) |
| 518 | .setHardTimeout((short)0) |
| 519 | .setBufferId(OFPacketOut.BUFFER_ID_NONE) |
| 520 | .setCookie(MAC_RW_COOKIE) |
| 521 | .setCommand(OFFlowMod.OFPFC_ADD) |
| 522 | .setPriority(SDNIP_PRIORITY) |
| 523 | .setLengthU(OFFlowMod.MINIMUM_LENGTH |
| 524 | + OFActionDataLayerDestination.MINIMUM_LENGTH |
| 525 | + OFActionOutput.MINIMUM_LENGTH); |
| 526 | |
| 527 | OFMatch match = new OFMatch(); |
| 528 | match.setDataLayerType(Ethernet.TYPE_IPv4); |
| 529 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE); |
| 530 | |
| 531 | match.setFromCIDR(prefix.toString(), OFMatch.STR_NW_DST); |
| 532 | fm.setMatch(match); |
| 533 | |
| 534 | //Set up MAC rewrite action |
| 535 | OFActionDataLayerDestination macRewriteAction = new OFActionDataLayerDestination(); |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 536 | macRewriteAction.setDataLayerAddress(nextHopMacAddress.toBytes()); |
Jonathan Hart | a23ffdb | 2013-08-14 14:36:54 +1200 | [diff] [blame] | 537 | |
| 538 | //Set up output action |
| 539 | OFActionOutput outputAction = new OFActionOutput(); |
| 540 | outputAction.setMaxLength((short)0xffff); |
| 541 | Port outputPort = shortestPath.flowEntries().get(0).outPort(); |
| 542 | outputAction.setPort(outputPort.value()); |
| 543 | |
| 544 | List<OFAction> actions = new ArrayList<OFAction>(); |
| 545 | actions.add(macRewriteAction); |
| 546 | actions.add(outputAction); |
| 547 | fm.setActions(actions); |
| 548 | |
| 549 | //Write to switch |
| 550 | IOFSwitch sw = floodlightProvider.getSwitches() |
| 551 | .get(srcInterface.getDpid()); |
| 552 | |
| 553 | if (sw == null){ |
| 554 | log.warn("Switch not found when pushing flow mod"); |
| 555 | continue; |
| 556 | } |
| 557 | |
| 558 | pushedFlows.put(prefix, new PushedFlowMod(sw.getId(), fm)); |
| 559 | |
| 560 | List<OFMessage> msglist = new ArrayList<OFMessage>(); |
| 561 | msglist.add(fm); |
| 562 | try { |
| 563 | sw.write(msglist, null); |
| 564 | sw.flush(); |
Jonathan Hart | 08ee852 | 2013-09-22 17:34:43 +1200 | [diff] [blame] | 565 | |
| 566 | /* |
| 567 | * XXX Rate limit hack! |
| 568 | * This should be solved properly by adding a rate limiting |
| 569 | * layer on top of the switches if we know they need it. |
| 570 | */ |
Jonathan Hart | 9971e1c | 2013-09-17 10:47:40 +1200 | [diff] [blame] | 571 | Thread.sleep(1); |
Jonathan Hart | a23ffdb | 2013-08-14 14:36:54 +1200 | [diff] [blame] | 572 | } catch (IOException e) { |
| 573 | log.error("Failure writing flow mod", e); |
Jonathan Hart | 65139e4 | 2013-09-13 16:52:25 +1200 | [diff] [blame] | 574 | } catch (InterruptedException e) { |
| 575 | // TODO handle this properly |
| 576 | log.error("Interrupted", e); |
Jonathan Hart | a23ffdb | 2013-08-14 14:36:54 +1200 | [diff] [blame] | 577 | } |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 578 | } |
| 579 | } |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 580 | |
Jonathan Hart | 0a46fe4 | 2013-08-10 17:08:47 +1200 | [diff] [blame] | 581 | public synchronized void processRibDelete(RibUpdate update) { |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 582 | Prefix prefix = update.getPrefix(); |
| 583 | |
Jonathan Hart | d7e158d | 2013-08-07 23:04:48 +1200 | [diff] [blame] | 584 | if (ptree.remove(prefix, update.getRibEntry())) { |
| 585 | /* |
| 586 | * Only delete flows if an entry was actually removed from the trie. |
| 587 | * If no entry was removed, the <prefix, nexthop> wasn't there so |
| 588 | * it's probably already been removed and we don't need to do anything |
| 589 | */ |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 590 | _processDeletePrefix(prefix, update.getRibEntry()); |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 591 | } |
| 592 | } |
| 593 | |
| 594 | private void _processDeletePrefix(Prefix prefix, RibEntry ribEntry) { |
| 595 | deletePrefixFlows(prefix); |
| 596 | |
| 597 | log.debug("Deleting {} to {}", prefix, ribEntry.getNextHop()); |
Jonathan Hart | d992f1a | 2013-09-13 13:46:44 +1200 | [diff] [blame] | 598 | |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 599 | if (!bgpPeers.containsKey(ribEntry.getNextHop())) { |
| 600 | log.debug("Getting path for route with non-peer nexthop"); |
Jonathan Hart | fb1ebc5 | 2013-08-17 16:25:51 +1200 | [diff] [blame] | 601 | Path path = prefixToPath.remove(prefix); |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 602 | |
Jonathan Hart | d992f1a | 2013-09-13 13:46:44 +1200 | [diff] [blame] | 603 | if (path != null) { |
| 604 | //path could be null if we added to the Ptree but didn't push |
| 605 | //flows yet because we were waiting to resolve ARP |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 606 | |
Jonathan Hart | d992f1a | 2013-09-13 13:46:44 +1200 | [diff] [blame] | 607 | path.decrementUsers(); |
Jonathan Hart | d992f1a | 2013-09-13 13:46:44 +1200 | [diff] [blame] | 608 | if (path.getUsers() <= 0 && !path.isPermanent()) { |
| 609 | deletePath(path); |
| 610 | pushedPaths.remove(path.getDstIpAddress()); |
| 611 | } |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 612 | } |
| 613 | } |
| 614 | } |
| 615 | |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 616 | private void deletePrefixFlows(Prefix prefix) { |
| 617 | log.debug("Deleting flows for prefix {}", prefix); |
| 618 | |
Jonathan Hart | a23ffdb | 2013-08-14 14:36:54 +1200 | [diff] [blame] | 619 | Collection<PushedFlowMod> pushedFlowMods |
| 620 | = pushedFlows.removeAll(prefix); |
| 621 | |
| 622 | for (PushedFlowMod pfm : pushedFlowMods) { |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 623 | if (log.isTraceEnabled()) { |
| 624 | log.trace("Pushing a DELETE flow mod to {}, matches prefix {} with mac-rewrite {}", |
| 625 | new Object[] {HexString.toHexString(pfm.getDpid()), |
| 626 | pfm.getFlowMod().getMatch().getNetworkDestination() + |
| 627 | pfm.getFlowMod().getMatch().getNetworkDestinationMaskLen(), |
| 628 | HexString.toHexString(((OFActionDataLayerDestination)pfm.getFlowMod().getActions().get(0)) |
| 629 | .getDataLayerAddress())}); |
| 630 | } |
Jonathan Hart | a23ffdb | 2013-08-14 14:36:54 +1200 | [diff] [blame] | 631 | |
| 632 | sendDeleteFlowMod(pfm.getFlowMod(), pfm.getDpid()); |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | private void deletePath(Path path) { |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 637 | log.debug("Deleting flows for path to {}", |
| 638 | path.getDstIpAddress().getHostAddress()); |
| 639 | |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 640 | for (PushedFlowMod pfm : path.getFlowMods()) { |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 641 | if (log.isTraceEnabled()) { |
| 642 | log.trace("Pushing a DELETE flow mod to {}, dst MAC {}", |
| 643 | new Object[] {HexString.toHexString(pfm.getDpid()), |
| 644 | HexString.toHexString(pfm.getFlowMod().getMatch().getDataLayerDestination()) |
| 645 | }); |
| 646 | } |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 647 | |
| 648 | sendDeleteFlowMod(pfm.getFlowMod(), pfm.getDpid()); |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | private void sendDeleteFlowMod(OFFlowMod addFlowMod, long dpid) { |
| 653 | addFlowMod.setCommand(OFFlowMod.OFPFC_DELETE_STRICT) |
| 654 | .setOutPort(OFPort.OFPP_NONE) |
| 655 | .setLengthU(OFFlowMod.MINIMUM_LENGTH); |
| 656 | |
| 657 | addFlowMod.getActions().clear(); |
| 658 | |
| 659 | IOFSwitch sw = floodlightProvider.getSwitches().get(dpid); |
| 660 | if (sw == null) { |
| 661 | log.warn("Switch not found when pushing delete flow mod"); |
| 662 | return; |
| 663 | } |
| 664 | |
| 665 | try { |
| 666 | sw.write(addFlowMod, null); |
| 667 | sw.flush(); |
| 668 | } catch (IOException e) { |
| 669 | log.error("Failure writing flow mod", e); |
Jonathan Hart | d7e158d | 2013-08-07 23:04:48 +1200 | [diff] [blame] | 670 | } |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 671 | } |
| 672 | |
Jonathan Hart | 0ee0f02 | 2013-08-03 22:21:54 +1200 | [diff] [blame] | 673 | //TODO test next-hop changes |
Jonathan Hart | 2f74078 | 2013-08-04 00:49:21 +1200 | [diff] [blame] | 674 | //TODO check delete/add synchronization |
Jonathan Hart | 0ee0f02 | 2013-08-03 22:21:54 +1200 | [diff] [blame] | 675 | |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 676 | /* |
| 677 | * On startup we need to calculate a full mesh of paths between all gateway |
| 678 | * switches |
| 679 | */ |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 680 | private void setupFullMesh(){ |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 681 | //For each border router, calculate and install a path from every other |
| 682 | //border switch to said border router. However, don't install the entry |
| 683 | //in to the first hop switch, as we need to install an entry to rewrite |
| 684 | //for each prefix received. This will be done later when prefixes have |
| 685 | //actually been received. |
| 686 | |
Jonathan Hart | c824ad0 | 2013-07-03 15:58:45 +1200 | [diff] [blame] | 687 | for (BgpPeer peer : bgpPeers.values()) { |
| 688 | Interface peerInterface = interfaces.get(peer.getInterfaceName()); |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 689 | |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 690 | //We know there's not already a Path here pushed, because this is |
| 691 | //called before all other routing |
Jonathan Hart | a23ffdb | 2013-08-14 14:36:54 +1200 | [diff] [blame] | 692 | Path path = new Path(peerInterface, peer.getIpAddress()); |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 693 | path.setPermanent(); |
| 694 | |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 695 | //See if we know the MAC address of the peer. If not we can't |
| 696 | //do anything until we learn it |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 697 | MACAddress macAddress = proxyArp.getMacAddress(peer.getIpAddress()); |
| 698 | if (macAddress == null) { |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 699 | log.debug("Don't know MAC for {}", peer.getIpAddress().getHostAddress()); |
| 700 | //Put in the pending paths list first |
Jonathan Hart | a23ffdb | 2013-08-14 14:36:54 +1200 | [diff] [blame] | 701 | pathsWaitingOnArp.put(peer.getIpAddress(), path); |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 702 | |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 703 | proxyArp.sendArpRequest(peer.getIpAddress(), this, true); |
| 704 | continue; |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 705 | } |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 706 | |
| 707 | //If we know the MAC, lets go ahead and push the paths to this peer |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 708 | calculateAndPushPath(path, macAddress); |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 709 | } |
| 710 | } |
| 711 | |
Jonathan Hart | a23ffdb | 2013-08-14 14:36:54 +1200 | [diff] [blame] | 712 | private void calculateAndPushPath(Path path, MACAddress dstMacAddress) { |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 713 | Interface dstInterface = path.getDstInterface(); |
| 714 | |
Jonathan Hart | fb1ebc5 | 2013-08-17 16:25:51 +1200 | [diff] [blame] | 715 | log.debug("Setting up path to {}, {}", path.getDstIpAddress().getHostAddress(), |
| 716 | dstMacAddress); |
| 717 | |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 718 | List<PushedFlowMod> pushedFlows = new ArrayList<PushedFlowMod>(); |
| 719 | |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 720 | for (Interface srcInterface : interfaces.values()) { |
| 721 | if (dstInterface.equals(srcInterface.getName())){ |
| 722 | continue; |
| 723 | } |
| 724 | |
Jonathan Hart | defa44d | 2013-08-15 19:51:13 +1200 | [diff] [blame] | 725 | DataPath shortestPath; |
| 726 | if (topoRouteTopology == null) { |
Jonathan Hart | defa44d | 2013-08-15 19:51:13 +1200 | [diff] [blame] | 727 | shortestPath = topoRouteService.getShortestPath( |
| 728 | srcInterface.getSwitchPort(), dstInterface.getSwitchPort()); |
| 729 | } |
| 730 | else { |
Jonathan Hart | defa44d | 2013-08-15 19:51:13 +1200 | [diff] [blame] | 731 | shortestPath = topoRouteService.getTopoShortestPath(topoRouteTopology, |
| 732 | srcInterface.getSwitchPort(), dstInterface.getSwitchPort()); |
| 733 | } |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 734 | |
| 735 | if (shortestPath == null){ |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 736 | log.warn("Shortest path between {} and {} not found", |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 737 | srcInterface.getSwitchPort(), dstInterface.getSwitchPort()); |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 738 | return; |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 739 | } |
| 740 | |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 741 | pushedFlows.addAll(installPath(shortestPath.flowEntries(), dstMacAddress)); |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 742 | } |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 743 | |
| 744 | path.setFlowMods(pushedFlows); |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 745 | } |
| 746 | |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 747 | private List<PushedFlowMod> installPath(List<FlowEntry> flowEntries, MACAddress dstMacAddress){ |
| 748 | List<PushedFlowMod> flowMods = new ArrayList<PushedFlowMod>(); |
| 749 | |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 750 | //Set up the flow mod |
| 751 | OFFlowMod fm = |
| 752 | (OFFlowMod) floodlightProvider.getOFMessageFactory() |
| 753 | .getMessage(OFType.FLOW_MOD); |
| 754 | |
| 755 | OFActionOutput action = new OFActionOutput(); |
| 756 | action.setMaxLength((short)0xffff); |
| 757 | List<OFAction> actions = new ArrayList<OFAction>(); |
| 758 | actions.add(action); |
| 759 | |
| 760 | fm.setIdleTimeout((short)0) |
| 761 | .setHardTimeout((short)0) |
| 762 | .setBufferId(OFPacketOut.BUFFER_ID_NONE) |
| 763 | .setCookie(L2_FWD_COOKIE) |
| 764 | .setCommand(OFFlowMod.OFPFC_ADD) |
Jonathan Hart | bdc8746 | 2013-09-24 15:03:23 +1200 | [diff] [blame] | 765 | .setPriority(SDNIP_PRIORITY) |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 766 | .setActions(actions) |
| 767 | .setLengthU(OFFlowMod.MINIMUM_LENGTH+OFActionOutput.MINIMUM_LENGTH); |
| 768 | |
| 769 | //Don't push the first flow entry. We need to push entries in the |
| 770 | //first switch based on IP prefix which we don't know yet. |
| 771 | for (int i = 1; i < flowEntries.size(); i++){ |
| 772 | FlowEntry flowEntry = flowEntries.get(i); |
| 773 | |
| 774 | OFMatch match = new OFMatch(); |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 775 | match.setDataLayerDestination(dstMacAddress.toBytes()); |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 776 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_DST); |
| 777 | ((OFActionOutput) fm.getActions().get(0)).setPort(flowEntry.outPort().value()); |
| 778 | |
| 779 | fm.setMatch(match); |
| 780 | |
| 781 | IOFSwitch sw = floodlightProvider.getSwitches().get(flowEntry.dpid().value()); |
| 782 | |
| 783 | if (sw == null){ |
| 784 | log.warn("Switch not found when pushing flow mod"); |
| 785 | continue; |
| 786 | } |
| 787 | |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 788 | flowMods.add(new PushedFlowMod(sw.getId(), fm)); |
| 789 | |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 790 | List<OFMessage> msglist = new ArrayList<OFMessage>(); |
| 791 | msglist.add(fm); |
| 792 | try { |
| 793 | sw.write(msglist, null); |
| 794 | sw.flush(); |
| 795 | } catch (IOException e) { |
| 796 | log.error("Failure writing flow mod", e); |
| 797 | } |
| 798 | |
| 799 | try { |
| 800 | fm = fm.clone(); |
| 801 | } catch (CloneNotSupportedException e1) { |
| 802 | log.error("Failure cloning flow mod", e1); |
| 803 | } |
| 804 | } |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 805 | |
| 806 | return flowMods; |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 807 | } |
| 808 | |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 809 | private void setupBgpPaths(){ |
Jonathan Hart | c824ad0 | 2013-07-03 15:58:45 +1200 | [diff] [blame] | 810 | for (BgpPeer bgpPeer : bgpPeers.values()){ |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 811 | Interface peerInterface = interfaces.get(bgpPeer.getInterfaceName()); |
| 812 | |
| 813 | DataPath path = topoRouteService.getShortestPath( |
| 814 | peerInterface.getSwitchPort(), bgpdAttachmentPoint); |
| 815 | |
| 816 | if (path == null){ |
| 817 | log.debug("Unable to compute path for BGP traffic for {}", |
| 818 | bgpPeer.getIpAddress()); |
| 819 | continue; |
| 820 | } |
| 821 | |
| 822 | //Set up the flow mod |
| 823 | OFFlowMod fm = |
| 824 | (OFFlowMod) floodlightProvider.getOFMessageFactory() |
| 825 | .getMessage(OFType.FLOW_MOD); |
| 826 | |
| 827 | OFActionOutput action = new OFActionOutput(); |
| 828 | action.setMaxLength((short)0xffff); |
| 829 | List<OFAction> actions = new ArrayList<OFAction>(); |
| 830 | actions.add(action); |
| 831 | |
| 832 | fm.setIdleTimeout((short)0) |
| 833 | .setHardTimeout((short)0) |
| 834 | .setBufferId(OFPacketOut.BUFFER_ID_NONE) |
| 835 | .setCookie(BGP_COOKIE) |
| 836 | .setCommand(OFFlowMod.OFPFC_ADD) |
| 837 | .setPriority(SDNIP_PRIORITY) |
| 838 | .setActions(actions) |
| 839 | .setLengthU(OFFlowMod.MINIMUM_LENGTH+OFActionOutput.MINIMUM_LENGTH); |
| 840 | |
| 841 | //Forward = gateway -> bgpd, reverse = bgpd -> gateway |
| 842 | OFMatch forwardMatchSrc = new OFMatch(); |
| 843 | |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 844 | String interfaceCidrAddress = peerInterface.getIpAddress().getHostAddress() |
| 845 | + "/32"; |
| 846 | String peerCidrAddress = bgpPeer.getIpAddress().getHostAddress() |
| 847 | + "/32"; |
Jonathan Hart | 38c8493 | 2013-08-10 17:49:27 +1200 | [diff] [blame] | 848 | |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 849 | //Common match fields |
| 850 | forwardMatchSrc.setDataLayerType(Ethernet.TYPE_IPv4); |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 851 | forwardMatchSrc.setNetworkProtocol(IPv4.PROTOCOL_TCP); |
| 852 | forwardMatchSrc.setTransportDestination(BGP_PORT); |
| 853 | forwardMatchSrc.setWildcards(forwardMatchSrc.getWildcards() & ~OFMatch.OFPFW_IN_PORT |
| 854 | & ~OFMatch.OFPFW_DL_TYPE & ~OFMatch.OFPFW_NW_PROTO); |
| 855 | |
| 856 | |
| 857 | OFMatch reverseMatchSrc = forwardMatchSrc.clone(); |
| 858 | |
| 859 | forwardMatchSrc.setFromCIDR(peerCidrAddress, OFMatch.STR_NW_SRC); |
| 860 | forwardMatchSrc.setFromCIDR(interfaceCidrAddress, OFMatch.STR_NW_DST); |
| 861 | |
| 862 | OFMatch forwardMatchDst = forwardMatchSrc.clone(); |
| 863 | |
| 864 | forwardMatchSrc.setTransportSource(BGP_PORT); |
| 865 | forwardMatchSrc.setWildcards(forwardMatchSrc.getWildcards() & ~OFMatch.OFPFW_TP_SRC); |
| 866 | forwardMatchDst.setTransportDestination(BGP_PORT); |
| 867 | forwardMatchDst.setWildcards(forwardMatchDst.getWildcards() & ~OFMatch.OFPFW_TP_DST); |
| 868 | |
| 869 | reverseMatchSrc.setFromCIDR(interfaceCidrAddress, OFMatch.STR_NW_SRC); |
| 870 | reverseMatchSrc.setFromCIDR(peerCidrAddress, OFMatch.STR_NW_DST); |
| 871 | |
| 872 | OFMatch reverseMatchDst = reverseMatchSrc.clone(); |
| 873 | |
| 874 | reverseMatchSrc.setTransportSource(BGP_PORT); |
| 875 | reverseMatchSrc.setWildcards(forwardMatchSrc.getWildcards() & ~OFMatch.OFPFW_TP_SRC); |
| 876 | reverseMatchDst.setTransportDestination(BGP_PORT); |
| 877 | reverseMatchDst.setWildcards(forwardMatchDst.getWildcards() & ~OFMatch.OFPFW_TP_DST); |
| 878 | |
| 879 | fm.setMatch(forwardMatchSrc); |
| 880 | |
Jonathan Hart | 38c8493 | 2013-08-10 17:49:27 +1200 | [diff] [blame] | 881 | OFMatch forwardIcmpMatch = new OFMatch(); |
| 882 | forwardIcmpMatch.setDataLayerType(Ethernet.TYPE_IPv4); |
| 883 | forwardIcmpMatch.setNetworkProtocol(IPv4.PROTOCOL_ICMP); |
| 884 | forwardIcmpMatch.setWildcards(forwardIcmpMatch.getWildcards() & |
| 885 | ~OFMatch.OFPFW_DL_TYPE & ~OFMatch.OFPFW_NW_PROTO); |
| 886 | |
| 887 | OFMatch reverseIcmpMatch = forwardIcmpMatch.clone(); |
| 888 | forwardIcmpMatch.setFromCIDR(interfaceCidrAddress, OFMatch.STR_NW_DST); |
| 889 | reverseIcmpMatch.setFromCIDR(interfaceCidrAddress, OFMatch.STR_NW_SRC); |
| 890 | |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 891 | for (FlowEntry flowEntry : path.flowEntries()){ |
| 892 | OFFlowMod forwardFlowModSrc, forwardFlowModDst; |
| 893 | OFFlowMod reverseFlowModSrc, reverseFlowModDst; |
Jonathan Hart | 38c8493 | 2013-08-10 17:49:27 +1200 | [diff] [blame] | 894 | OFFlowMod forwardIcmp, reverseIcmp; |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 895 | try { |
| 896 | forwardFlowModSrc = fm.clone(); |
| 897 | forwardFlowModDst = fm.clone(); |
| 898 | reverseFlowModSrc = fm.clone(); |
| 899 | reverseFlowModDst = fm.clone(); |
Jonathan Hart | 38c8493 | 2013-08-10 17:49:27 +1200 | [diff] [blame] | 900 | forwardIcmp = fm.clone(); |
| 901 | reverseIcmp = fm.clone(); |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 902 | } catch (CloneNotSupportedException e) { |
| 903 | log.warn("Clone failed", e); |
| 904 | continue; |
| 905 | } |
| 906 | |
| 907 | forwardMatchSrc.setInputPort(flowEntry.inPort().value()); |
| 908 | forwardFlowModSrc.setMatch(forwardMatchSrc); |
| 909 | ((OFActionOutput)forwardFlowModSrc.getActions().get(0)) |
| 910 | .setPort(flowEntry.outPort().value()); |
| 911 | |
| 912 | forwardMatchDst.setInputPort(flowEntry.inPort().value()); |
| 913 | forwardFlowModDst.setMatch(forwardMatchDst); |
| 914 | ((OFActionOutput)forwardFlowModDst.getActions().get(0)) |
| 915 | .setPort(flowEntry.outPort().value()); |
| 916 | |
| 917 | reverseMatchSrc.setInputPort(flowEntry.outPort().value()); |
| 918 | reverseFlowModSrc.setMatch(reverseMatchSrc); |
| 919 | ((OFActionOutput)reverseFlowModSrc.getActions().get(0)) |
| 920 | .setPort(flowEntry.inPort().value()); |
| 921 | |
| 922 | reverseMatchDst.setInputPort(flowEntry.outPort().value()); |
| 923 | reverseFlowModDst.setMatch(reverseMatchDst); |
| 924 | ((OFActionOutput)reverseFlowModDst.getActions().get(0)) |
| 925 | .setPort(flowEntry.inPort().value()); |
| 926 | |
Jonathan Hart | 38c8493 | 2013-08-10 17:49:27 +1200 | [diff] [blame] | 927 | ((OFActionOutput)forwardIcmp.getActions().get(0)) |
| 928 | .setPort(flowEntry.outPort().value()); |
| 929 | forwardIcmp.setMatch(forwardIcmpMatch); |
| 930 | |
| 931 | ((OFActionOutput)reverseIcmp.getActions().get(0)) |
| 932 | .setPort(flowEntry.inPort().value()); |
| 933 | reverseIcmp.setMatch(reverseIcmpMatch); |
| 934 | |
| 935 | |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 936 | IOFSwitch sw = floodlightProvider.getSwitches().get(flowEntry.dpid().value()); |
| 937 | |
Jonathan Hart | 38c8493 | 2013-08-10 17:49:27 +1200 | [diff] [blame] | 938 | if (sw == null) { |
| 939 | log.warn("Switch not found when pushing BGP paths"); |
| 940 | return; |
| 941 | } |
| 942 | |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 943 | List<OFMessage> msgList = new ArrayList<OFMessage>(2); |
| 944 | msgList.add(forwardFlowModSrc); |
| 945 | msgList.add(forwardFlowModDst); |
| 946 | msgList.add(reverseFlowModSrc); |
| 947 | msgList.add(reverseFlowModDst); |
Jonathan Hart | 38c8493 | 2013-08-10 17:49:27 +1200 | [diff] [blame] | 948 | msgList.add(forwardIcmp); |
| 949 | msgList.add(reverseIcmp); |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 950 | |
| 951 | try { |
| 952 | sw.write(msgList, null); |
| 953 | sw.flush(); |
| 954 | } catch (IOException e) { |
| 955 | log.error("Failure writing flow mod", e); |
| 956 | } |
| 957 | } |
| 958 | } |
| 959 | } |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 960 | |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 961 | @Override |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 962 | public void arpResponse(InetAddress ipAddress, MACAddress macAddress) { |
| 963 | log.debug("Received ARP response: {} => {}", |
| 964 | ipAddress.getHostAddress(), macAddress); |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 965 | |
Jonathan Hart | 0a46fe4 | 2013-08-10 17:08:47 +1200 | [diff] [blame] | 966 | /* |
| 967 | * We synchronize on this to prevent changes to the ptree while we're pushing |
| 968 | * flows to the switches. If the ptree changes, the ptree and switches |
| 969 | * could get out of sync. |
| 970 | */ |
| 971 | synchronized (this) { |
Jonathan Hart | a23ffdb | 2013-08-14 14:36:54 +1200 | [diff] [blame] | 972 | Path path = pathsWaitingOnArp.remove(ipAddress); |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 973 | |
Jonathan Hart | a23ffdb | 2013-08-14 14:36:54 +1200 | [diff] [blame] | 974 | if (path != null) { |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 975 | log.debug("Pushing path to {} at {} on {}", new Object[] { |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 976 | path.getDstIpAddress().getHostAddress(), macAddress, |
Jonathan Hart | a23ffdb | 2013-08-14 14:36:54 +1200 | [diff] [blame] | 977 | path.getDstInterface().getSwitchPort()}); |
| 978 | //These paths should always be to BGP peers. Paths to non-peers are |
| 979 | //handled once the first prefix is ready to push |
| 980 | if (pushedPaths.containsKey(path.getDstInterface())) { |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 981 | //A path already got pushed to this endpoint while we were waiting |
| 982 | //for ARP. We'll copy over the permanent attribute if it is set on this path. |
Jonathan Hart | a23ffdb | 2013-08-14 14:36:54 +1200 | [diff] [blame] | 983 | if (path.isPermanent()) { |
| 984 | pushedPaths.get(path.getDstInterface()).setPermanent(); |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 985 | } |
| 986 | } |
| 987 | else { |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 988 | calculateAndPushPath(path, macAddress); |
Jonathan Hart | a23ffdb | 2013-08-14 14:36:54 +1200 | [diff] [blame] | 989 | pushedPaths.put(path.getDstIpAddress(), path); |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 990 | } |
| 991 | } |
| 992 | |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 993 | Set<RibUpdate> prefixesToPush = prefixesWaitingOnArp.removeAll(ipAddress); |
| 994 | |
Jonathan Hart | 0a46fe4 | 2013-08-10 17:08:47 +1200 | [diff] [blame] | 995 | for (RibUpdate update : prefixesToPush) { |
| 996 | //These will always be adds |
| 997 | |
Jonathan Hart | b39a67d | 2013-08-10 23:59:50 +1200 | [diff] [blame] | 998 | RibEntry rib = ptree.lookup(update.getPrefix()); |
Jonathan Hart | 0a46fe4 | 2013-08-10 17:08:47 +1200 | [diff] [blame] | 999 | if (rib != null && rib.equals(update.getRibEntry())) { |
| 1000 | log.debug("Pushing prefix {} next hop {}", update.getPrefix(), |
Jonathan Hart | b39a67d | 2013-08-10 23:59:50 +1200 | [diff] [blame] | 1001 | rib.getNextHop().getHostAddress()); |
Jonathan Hart | 0a46fe4 | 2013-08-10 17:08:47 +1200 | [diff] [blame] | 1002 | //We only push prefix flows if the prefix is still in the ptree |
| 1003 | //and the next hop is the same as our update. The prefix could |
| 1004 | //have been removed while we were waiting for the ARP, or the |
| 1005 | //next hop could have changed. |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 1006 | _processRibAdd(update); |
Jonathan Hart | 0a46fe4 | 2013-08-10 17:08:47 +1200 | [diff] [blame] | 1007 | } else { |
| 1008 | log.debug("Received ARP response, but {},{} is no longer in ptree", |
| 1009 | update.getPrefix(), update.getRibEntry()); |
| 1010 | } |
| 1011 | } |
Jonathan Hart | 0ee0f02 | 2013-08-03 22:21:54 +1200 | [diff] [blame] | 1012 | } |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 1013 | } |
| 1014 | |
Jonathan Hart | c82051c | 2013-08-24 15:12:20 +1200 | [diff] [blame] | 1015 | private void setupArpFlows() { |
| 1016 | OFMatch match = new OFMatch(); |
| 1017 | match.setDataLayerType(Ethernet.TYPE_ARP); |
| 1018 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE); |
| 1019 | |
| 1020 | OFFlowMod fm = new OFFlowMod(); |
| 1021 | fm.setMatch(match); |
| 1022 | |
| 1023 | OFActionOutput action = new OFActionOutput(); |
| 1024 | action.setPort(OFPort.OFPP_CONTROLLER.getValue()); |
| 1025 | action.setMaxLength((short)0xffff); |
| 1026 | List<OFAction> actions = new ArrayList<OFAction>(1); |
| 1027 | actions.add(action); |
| 1028 | fm.setActions(actions); |
| 1029 | |
| 1030 | fm.setIdleTimeout((short)0) |
| 1031 | .setHardTimeout((short)0) |
| 1032 | .setBufferId(OFPacketOut.BUFFER_ID_NONE) |
| 1033 | .setCookie(0) |
| 1034 | .setCommand(OFFlowMod.OFPFC_ADD) |
| 1035 | .setPriority(ARP_PRIORITY) |
| 1036 | .setLengthU(OFFlowMod.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH); |
| 1037 | |
| 1038 | for (String strdpid : switches){ |
| 1039 | IOFSwitch sw = floodlightProvider.getSwitches().get(HexString.toLong(strdpid)); |
| 1040 | if (sw == null) { |
| 1041 | log.debug("Couldn't find switch to push ARP flow"); |
| 1042 | } |
| 1043 | else { |
| 1044 | try { |
| 1045 | sw.write(fm, null); |
| 1046 | } catch (IOException e) { |
| 1047 | log.warn("Failure writing ARP flow to switch", e); |
| 1048 | } |
| 1049 | } |
| 1050 | } |
| 1051 | } |
| 1052 | |
Jonathan Hart | f886fa1 | 2013-09-18 14:46:29 +1200 | [diff] [blame] | 1053 | private void setupDefaultDropFlows() { |
| 1054 | OFFlowMod fm = new OFFlowMod(); |
| 1055 | fm.setMatch(new OFMatch()); |
Jonathan Hart | 4aa2b4e | 2013-09-24 14:50:23 +1200 | [diff] [blame] | 1056 | fm.setActions(new ArrayList<OFAction>()); //No action means drop |
Jonathan Hart | f886fa1 | 2013-09-18 14:46:29 +1200 | [diff] [blame] | 1057 | |
| 1058 | fm.setIdleTimeout((short)0) |
| 1059 | .setHardTimeout((short)0) |
| 1060 | .setBufferId(OFPacketOut.BUFFER_ID_NONE) |
| 1061 | .setCookie(0) |
| 1062 | .setCommand(OFFlowMod.OFPFC_ADD) |
| 1063 | .setPriority((short)0) |
| 1064 | .setLengthU(OFFlowMod.MINIMUM_LENGTH); |
| 1065 | |
Jonathan Hart | 4aa2b4e | 2013-09-24 14:50:23 +1200 | [diff] [blame] | 1066 | OFFlowMod fmLLDP; |
| 1067 | OFFlowMod fmBDDP; |
| 1068 | try { |
| 1069 | fmLLDP = fm.clone(); |
| 1070 | fmBDDP = fm.clone(); |
| 1071 | } catch (CloneNotSupportedException e1) { |
| 1072 | log.error("Error cloning flow mod", e1); |
| 1073 | return; |
| 1074 | } |
| 1075 | |
| 1076 | OFMatch matchLLDP = new OFMatch(); |
| 1077 | matchLLDP.setDataLayerType((short)0x8942); |
| 1078 | matchLLDP.setWildcards(matchLLDP.getWildcards() & ~ OFMatch.OFPFW_DL_TYPE); |
| 1079 | fmLLDP.setMatch(matchLLDP); |
| 1080 | |
| 1081 | OFMatch matchBDDP = new OFMatch(); |
| 1082 | matchBDDP.setDataLayerType((short)0x88cc); |
| 1083 | matchBDDP.setWildcards(matchBDDP.getWildcards() & ~ OFMatch.OFPFW_DL_TYPE); |
| 1084 | fmBDDP.setMatch(matchBDDP); |
| 1085 | |
| 1086 | OFActionOutput action = new OFActionOutput(); |
| 1087 | action.setPort(OFPort.OFPP_CONTROLLER.getValue()); |
| 1088 | action.setMaxLength((short)0xffff); |
| 1089 | List<OFAction> actions = new ArrayList<OFAction>(1); |
| 1090 | actions.add(action); |
| 1091 | |
| 1092 | fmLLDP.setActions(actions); |
| 1093 | fmBDDP.setActions(actions); |
| 1094 | |
| 1095 | fmLLDP.setPriority(ARP_PRIORITY); |
| 1096 | fmLLDP.setLengthU(OFFlowMod.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH); |
| 1097 | fmBDDP.setPriority(ARP_PRIORITY); |
| 1098 | fmBDDP.setLengthU(OFFlowMod.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH); |
| 1099 | |
Jonathan Hart | f886fa1 | 2013-09-18 14:46:29 +1200 | [diff] [blame] | 1100 | for (String strdpid : switches){ |
| 1101 | IOFSwitch sw = floodlightProvider.getSwitches().get(HexString.toLong(strdpid)); |
| 1102 | if (sw == null) { |
| 1103 | log.debug("Couldn't find switch to push default deny flow"); |
| 1104 | } |
| 1105 | else { |
Jonathan Hart | 4aa2b4e | 2013-09-24 14:50:23 +1200 | [diff] [blame] | 1106 | List<OFMessage> msgList = new ArrayList<OFMessage>(); |
| 1107 | msgList.add(fm); |
| 1108 | msgList.add(fmLLDP); |
| 1109 | msgList.add(fmBDDP); |
Jonathan Hart | f886fa1 | 2013-09-18 14:46:29 +1200 | [diff] [blame] | 1110 | try { |
Jonathan Hart | 4aa2b4e | 2013-09-24 14:50:23 +1200 | [diff] [blame] | 1111 | sw.write(msgList, null); |
Jonathan Hart | f886fa1 | 2013-09-18 14:46:29 +1200 | [diff] [blame] | 1112 | } catch (IOException e) { |
| 1113 | log.warn("Failure writing default deny flow to switch", e); |
| 1114 | } |
| 1115 | } |
| 1116 | } |
| 1117 | } |
| 1118 | |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 1119 | private void beginRouting(){ |
| 1120 | log.debug("Topology is now ready, beginning routing function"); |
Jonathan Hart | defa44d | 2013-08-15 19:51:13 +1200 | [diff] [blame] | 1121 | topoRouteTopology = topoRouteService.prepareShortestPathTopo(); |
| 1122 | |
Jonathan Hart | c82051c | 2013-08-24 15:12:20 +1200 | [diff] [blame] | 1123 | setupArpFlows(); |
Jonathan Hart | f886fa1 | 2013-09-18 14:46:29 +1200 | [diff] [blame] | 1124 | setupDefaultDropFlows(); |
Jonathan Hart | c82051c | 2013-08-24 15:12:20 +1200 | [diff] [blame] | 1125 | |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 1126 | setupBgpPaths(); |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 1127 | setupFullMesh(); |
Jonathan Hart | e769453 | 2013-09-12 12:34:46 +1200 | [diff] [blame] | 1128 | |
| 1129 | //Suppress link discovery on external-facing router ports |
| 1130 | for (Interface intf : interfaces.values()) { |
| 1131 | linkDiscoveryService.AddToSuppressLLDPs(intf.getDpid(), intf.getPort()); |
| 1132 | } |
| 1133 | |
Jonathan Hart | 0a46fe4 | 2013-08-10 17:08:47 +1200 | [diff] [blame] | 1134 | bgpUpdatesExecutor.execute(new Runnable() { |
| 1135 | @Override |
| 1136 | public void run() { |
| 1137 | doUpdatesThread(); |
| 1138 | } |
| 1139 | }); |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | private void checkSwitchesConnected(){ |
| 1143 | for (String dpid : switches){ |
| 1144 | if (floodlightProvider.getSwitches().get(HexString.toLong(dpid)) == null){ |
| 1145 | log.debug("Not all switches are here yet"); |
| 1146 | return; |
| 1147 | } |
| 1148 | } |
| 1149 | switchesConnected = true; |
| 1150 | } |
| 1151 | |
Jonathan Hart | c824ad0 | 2013-07-03 15:58:45 +1200 | [diff] [blame] | 1152 | //Actually we only need to go half way round to verify full mesh connectivity |
| 1153 | //(n^2)/2 |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 1154 | private void checkTopologyReady(){ |
Jonathan Hart | c824ad0 | 2013-07-03 15:58:45 +1200 | [diff] [blame] | 1155 | for (Interface dstInterface : interfaces.values()) { |
| 1156 | for (Interface srcInterface : interfaces.values()) { |
| 1157 | if (dstInterface == srcInterface) { |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 1158 | continue; |
| 1159 | } |
| 1160 | |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 1161 | DataPath shortestPath = topoRouteService.getShortestPath( |
Jonathan Hart | c824ad0 | 2013-07-03 15:58:45 +1200 | [diff] [blame] | 1162 | srcInterface.getSwitchPort(), dstInterface.getSwitchPort()); |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 1163 | |
| 1164 | if (shortestPath == null){ |
| 1165 | log.debug("Shortest path between {} and {} not found", |
Jonathan Hart | c824ad0 | 2013-07-03 15:58:45 +1200 | [diff] [blame] | 1166 | srcInterface.getSwitchPort(), dstInterface.getSwitchPort()); |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 1167 | return; |
| 1168 | } |
| 1169 | } |
| 1170 | } |
| 1171 | topologyReady = true; |
| 1172 | } |
| 1173 | |
| 1174 | private void checkStatus(){ |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 1175 | if (!switchesConnected){ |
| 1176 | checkSwitchesConnected(); |
| 1177 | } |
| 1178 | boolean oldTopologyReadyStatus = topologyReady; |
| 1179 | if (switchesConnected && !topologyReady){ |
| 1180 | checkTopologyReady(); |
| 1181 | } |
| 1182 | if (!oldTopologyReadyStatus && topologyReady){ |
| 1183 | beginRouting(); |
| 1184 | } |
| 1185 | } |
Jonathan Hart | a23ffdb | 2013-08-14 14:36:54 +1200 | [diff] [blame] | 1186 | |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 1187 | private void doUpdatesThread() { |
| 1188 | boolean interrupted = false; |
| 1189 | try { |
| 1190 | while (true) { |
| 1191 | try { |
| 1192 | RibUpdate update = ribUpdates.take(); |
| 1193 | switch (update.getOperation()){ |
| 1194 | case UPDATE: |
Jonathan Hart | 2f74078 | 2013-08-04 00:49:21 +1200 | [diff] [blame] | 1195 | processRibAdd(update); |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 1196 | break; |
| 1197 | case DELETE: |
Jonathan Hart | 2f74078 | 2013-08-04 00:49:21 +1200 | [diff] [blame] | 1198 | processRibDelete(update); |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 1199 | break; |
| 1200 | } |
| 1201 | } catch (InterruptedException e) { |
Jonathan Hart | a23ffdb | 2013-08-14 14:36:54 +1200 | [diff] [blame] | 1202 | log.debug("Interrupted while taking from updates queue", e); |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 1203 | interrupted = true; |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 1204 | } catch (Exception e) { |
| 1205 | log.debug("exception", e); |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 1206 | } |
| 1207 | } |
| 1208 | } finally { |
| 1209 | if (interrupted) { |
| 1210 | Thread.currentThread().interrupt(); |
| 1211 | } |
| 1212 | } |
| 1213 | } |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 1214 | |
| 1215 | @Override |
Jonathan Hart | 64c0b20 | 2013-08-20 15:45:07 +1200 | [diff] [blame] | 1216 | public void topologyChanged() { |
| 1217 | if (topologyReady) { |
| 1218 | return; |
| 1219 | } |
| 1220 | |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 1221 | boolean refreshNeeded = false; |
| 1222 | for (LDUpdate ldu : topology.getLastLinkUpdates()){ |
| 1223 | if (!ldu.getOperation().equals(ILinkDiscovery.UpdateOperation.LINK_UPDATED)){ |
| 1224 | //We don't need to recalculate anything for just link updates |
Jonathan Hart | b39a67d | 2013-08-10 23:59:50 +1200 | [diff] [blame] | 1225 | //They happen very frequently |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 1226 | refreshNeeded = true; |
| 1227 | } |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 1228 | |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 1229 | log.debug("Topo change {}", ldu.getOperation()); |
Jonathan Hart | c824ad0 | 2013-07-03 15:58:45 +1200 | [diff] [blame] | 1230 | |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 1231 | if (ldu.getOperation().equals(ILinkDiscovery.UpdateOperation.LINK_ADDED)){ |
| 1232 | synchronized (linkUpdates) { |
| 1233 | linkUpdates.add(ldu); |
| 1234 | } |
| 1235 | } |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 1236 | } |
| 1237 | |
Jonathan Hart | 64c0b20 | 2013-08-20 15:45:07 +1200 | [diff] [blame] | 1238 | if (refreshNeeded && !topologyReady){ |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 1239 | topologyChangeDetectorTask.reschedule(TOPO_DETECTION_WAIT, TimeUnit.SECONDS); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 1240 | } |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 1241 | } |
Jonathan Hart | 64c0b20 | 2013-08-20 15:45:07 +1200 | [diff] [blame] | 1242 | |
| 1243 | @Override |
| 1244 | public void addedSwitch(IOFSwitch sw) { |
| 1245 | if (!topologyReady) { |
| 1246 | sw.clearAllFlowMods(); |
| 1247 | } |
| 1248 | } |
| 1249 | |
| 1250 | @Override |
Jonathan Hart | 08ee852 | 2013-09-22 17:34:43 +1200 | [diff] [blame] | 1251 | public void removedSwitch(IOFSwitch sw) {} |
Jonathan Hart | 64c0b20 | 2013-08-20 15:45:07 +1200 | [diff] [blame] | 1252 | |
| 1253 | @Override |
Jonathan Hart | 08ee852 | 2013-09-22 17:34:43 +1200 | [diff] [blame] | 1254 | public void switchPortChanged(Long switchId) {} |
Jonathan Hart | 64c0b20 | 2013-08-20 15:45:07 +1200 | [diff] [blame] | 1255 | |
| 1256 | @Override |
| 1257 | public String getName() { |
Jonathan Hart | 08ee852 | 2013-09-22 17:34:43 +1200 | [diff] [blame] | 1258 | return "BgpRoute"; |
| 1259 | } |
| 1260 | |
| 1261 | /* |
| 1262 | * ILayer3InfoService methods |
| 1263 | */ |
| 1264 | |
| 1265 | @Override |
| 1266 | public boolean isInterfaceAddress(InetAddress address) { |
| 1267 | Interface intf = interfacePtrie.match(new Prefix(address.getAddress(), 32)); |
| 1268 | return (intf != null && intf.getIpAddress().equals(address)); |
| 1269 | } |
| 1270 | |
| 1271 | @Override |
| 1272 | public boolean inConnectedNetwork(InetAddress address) { |
| 1273 | Interface intf = interfacePtrie.match(new Prefix(address.getAddress(), 32)); |
| 1274 | return (intf != null && !intf.getIpAddress().equals(address)); |
| 1275 | } |
| 1276 | |
| 1277 | @Override |
| 1278 | public boolean fromExternalNetwork(long inDpid, short inPort) { |
| 1279 | for (Interface intf : interfaces.values()) { |
| 1280 | if (intf.getDpid() == inDpid && intf.getPort() == inPort) { |
| 1281 | return true; |
| 1282 | } |
| 1283 | } |
| 1284 | return false; |
| 1285 | } |
| 1286 | |
| 1287 | @Override |
| 1288 | public Interface getOutgoingInterface(InetAddress dstIpAddress) { |
| 1289 | return interfacePtrie.match(new Prefix(dstIpAddress.getAddress(), 32)); |
| 1290 | } |
| 1291 | |
| 1292 | @Override |
| 1293 | public boolean hasLayer3Configuration() { |
| 1294 | return !interfaces.isEmpty(); |
| 1295 | } |
| 1296 | |
| 1297 | @Override |
| 1298 | public MACAddress getRouterMacAddress() { |
| 1299 | return bgpdMacAddress; |
Jonathan Hart | 64c0b20 | 2013-08-20 15:45:07 +1200 | [diff] [blame] | 1300 | } |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 1301 | } |