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