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