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