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