blob: 4a0aa27f6c1dbca661c443cbb25f7d34a6d04073 [file] [log] [blame]
HIGUCHI Yutaea60e5f2013-06-12 11:10:21 -07001package net.onrc.onos.ofcontroller.bgproute;
pingping-lina2cbfad2013-03-07 08:39:21 +08002
Jonathan Hartd1f23252013-06-13 15:17:05 +12003import java.io.File;
Jonathan Harte7e1c6e2013-06-04 20:50:23 -07004import java.io.IOException;
5import java.net.InetAddress;
pingping-lina2cbfad2013-03-07 08:39:21 +08006import java.util.ArrayList;
Jonathan Hart61ba9372013-05-19 20:10:29 -07007import java.util.Collection;
pingping-lina2cbfad2013-03-07 08:39:21 +08008import java.util.HashMap;
Jonathan Hart98957bf2013-07-01 14:49:24 +12009import java.util.Iterator;
Jonathan Harte7e1c6e2013-06-04 20:50:23 -070010import java.util.List;
Jonathan Hart61ba9372013-05-19 20:10:29 -070011import java.util.Map;
Jonathan Hart4dfc3652013-08-02 20:22:36 +120012import java.util.Set;
Jonathan Hart8b9349e2013-07-26 15:55:28 +120013import java.util.concurrent.BlockingQueue;
14import java.util.concurrent.ExecutorService;
Jonathan Hart98957bf2013-07-01 14:49:24 +120015import java.util.concurrent.Executors;
Jonathan Hart8b9349e2013-07-26 15:55:28 +120016import java.util.concurrent.LinkedBlockingQueue;
Jonathan Hart98957bf2013-07-01 14:49:24 +120017import java.util.concurrent.ScheduledExecutorService;
18import java.util.concurrent.TimeUnit;
pingping-lina2cbfad2013-03-07 08:39:21 +080019
Jonathan Hart61ba9372013-05-19 20:10:29 -070020import net.floodlightcontroller.core.IFloodlightProviderService;
Jonathan Harte7e1c6e2013-06-04 20:50:23 -070021import net.floodlightcontroller.core.IOFSwitch;
Jonathan Hart64c0b202013-08-20 15:45:07 +120022import net.floodlightcontroller.core.IOFSwitchListener;
pingping-lina2cbfad2013-03-07 08:39:21 +080023import net.floodlightcontroller.core.module.FloodlightModuleContext;
24import net.floodlightcontroller.core.module.FloodlightModuleException;
25import net.floodlightcontroller.core.module.IFloodlightModule;
26import net.floodlightcontroller.core.module.IFloodlightService;
Jonathan Hart98957bf2013-07-01 14:49:24 +120027import net.floodlightcontroller.core.util.SingletonTask;
Jonathan Harte7e1c6e2013-06-04 20:50:23 -070028import net.floodlightcontroller.packet.Ethernet;
Jonathan Hart832a7cb2013-06-24 11:25:35 +120029import net.floodlightcontroller.packet.IPv4;
pingping-lina2cbfad2013-03-07 08:39:21 +080030import net.floodlightcontroller.restserver.IRestApiService;
Jonathan Hart98957bf2013-07-01 14:49:24 +120031import net.floodlightcontroller.routing.Link;
pingping-lina2cbfad2013-03-07 08:39:21 +080032import net.floodlightcontroller.topology.ITopologyListener;
33import net.floodlightcontroller.topology.ITopologyService;
Jonathan Hart4dfc3652013-08-02 20:22:36 +120034import net.floodlightcontroller.util.MACAddress;
Jonathan Hart0ee0f022013-08-03 22:21:54 +120035import net.onrc.onos.ofcontroller.bgproute.RibUpdate.Operation;
Jonathan Hart98957bf2013-07-01 14:49:24 +120036import net.onrc.onos.ofcontroller.core.INetMapTopologyService.ITopoLinkService;
HIGUCHI Yuta20514902013-06-12 11:24:16 -070037import net.onrc.onos.ofcontroller.core.INetMapTopologyService.ITopoRouteService;
Jonathan Hart98957bf2013-07-01 14:49:24 +120038import net.onrc.onos.ofcontroller.core.internal.TopoLinkServiceImpl;
HIGUCHI Yutaa56fbde2013-06-17 14:26:05 -070039import net.onrc.onos.ofcontroller.linkdiscovery.ILinkDiscovery;
40import net.onrc.onos.ofcontroller.linkdiscovery.ILinkDiscovery.LDUpdate;
Jonathan Harte7694532013-09-12 12:34:46 +120041import net.onrc.onos.ofcontroller.linkdiscovery.ILinkDiscoveryService;
Jonathan Hart4dfc3652013-08-02 20:22:36 +120042import net.onrc.onos.ofcontroller.proxyarp.IArpRequester;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120043import net.onrc.onos.ofcontroller.proxyarp.ProxyArpManager;
Pavlin Radoslavovddd01ba2013-07-03 15:40:44 -070044import net.onrc.onos.ofcontroller.routing.TopoRouteService;
HIGUCHI Yuta356086e2013-06-12 15:21:19 -070045import net.onrc.onos.ofcontroller.util.DataPath;
Jonathan Hart832a7cb2013-06-24 11:25:35 +120046import net.onrc.onos.ofcontroller.util.Dpid;
HIGUCHI Yuta356086e2013-06-12 15:21:19 -070047import net.onrc.onos.ofcontroller.util.FlowEntry;
HIGUCHI Yuta356086e2013-06-12 15:21:19 -070048import net.onrc.onos.ofcontroller.util.Port;
49import net.onrc.onos.ofcontroller.util.SwitchPort;
pingping-line2a09ca2013-03-23 09:33:58 +080050import net.sf.json.JSONArray;
51import net.sf.json.JSONObject;
52import net.sf.json.JSONSerializer;
pingping-lina2cbfad2013-03-07 08:39:21 +080053
Jonathan Hartd1f23252013-06-13 15:17:05 +120054import org.codehaus.jackson.JsonParseException;
55import org.codehaus.jackson.map.JsonMappingException;
56import org.codehaus.jackson.map.ObjectMapper;
Jonathan Harte7e1c6e2013-06-04 20:50:23 -070057import org.openflow.protocol.OFFlowMod;
58import org.openflow.protocol.OFMatch;
59import org.openflow.protocol.OFMessage;
60import org.openflow.protocol.OFPacketOut;
Jonathan Hart9575cb62013-07-05 13:43:49 +120061import org.openflow.protocol.OFPort;
Jonathan Harte7e1c6e2013-06-04 20:50:23 -070062import org.openflow.protocol.OFType;
63import org.openflow.protocol.action.OFAction;
64import org.openflow.protocol.action.OFActionDataLayerDestination;
65import org.openflow.protocol.action.OFActionOutput;
Jonathan Hart1236a9b2013-06-18 22:10:05 +120066import org.openflow.util.HexString;
pingping-lina2cbfad2013-03-07 08:39:21 +080067import org.slf4j.Logger;
68import org.slf4j.LoggerFactory;
69
Jonathan Hart4dfc3652013-08-02 20:22:36 +120070import com.google.common.collect.HashMultimap;
Jonathan Hart0ee0f022013-08-03 22:21:54 +120071import com.google.common.collect.Multimap;
Jonathan Hart4dfc3652013-08-02 20:22:36 +120072import com.google.common.collect.Multimaps;
73import com.google.common.collect.SetMultimap;
Jonathan Hart309889c2013-08-13 23:26:24 +120074import com.google.common.net.InetAddresses;
Jonathan Hart8b9349e2013-07-26 15:55:28 +120075import com.google.common.util.concurrent.ThreadFactoryBuilder;
76
Jonathan Hart1236a9b2013-06-18 22:10:05 +120077public class BgpRoute implements IFloodlightModule, IBgpRouteService,
Jonathan Hart64c0b202013-08-20 15:45:07 +120078 ITopologyListener, IArpRequester,
Jonathan Hart08ee8522013-09-22 17:34:43 +120079 IOFSwitchListener, ILayer3InfoService {
pingping-lina2cbfad2013-03-07 08:39:21 +080080
81 protected static Logger log = LoggerFactory.getLogger(BgpRoute.class);
82
83 protected IFloodlightProviderService floodlightProvider;
84 protected ITopologyService topology;
Jonathan Harte7e1c6e2013-06-04 20:50:23 -070085 protected ITopoRouteService topoRouteService;
Jonathan Harte7694532013-09-12 12:34:46 +120086 protected ILinkDiscoveryService linkDiscoveryService;
Jonathan Harte7e1c6e2013-06-04 20:50:23 -070087 protected IRestApiService restApi;
88
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120089 protected ProxyArpManager proxyArp;
90
Jonathan Hart29b972d2013-08-12 23:43:51 +120091 protected IPatriciaTrie<RibEntry> ptree;
Jonathan Hartabf10222013-08-13 10:19:34 +120092 protected IPatriciaTrie<Interface> interfacePtrie;
Jonathan Hart8b9349e2013-07-26 15:55:28 +120093 protected BlockingQueue<RibUpdate> ribUpdates;
94
Jonathan Hart61ba9372013-05-19 20:10:29 -070095 protected String bgpdRestIp;
96 protected String routerId;
Jonathan Hart9575cb62013-07-05 13:43:49 +120097 protected String configFilename = "config.json";
Jonathan Harte7e1c6e2013-06-04 20:50:23 -070098
99 //We need to identify our flows somehow. But like it says in LearningSwitch.java,
100 //the controller/OS should hand out cookie IDs to prevent conflicts.
101 protected final long APP_COOKIE = 0xa0000000000000L;
102 //Cookie for flows that do L2 forwarding within SDN domain to egress routers
103 protected final long L2_FWD_COOKIE = APP_COOKIE + 1;
104 //Cookie for flows in ingress switches that rewrite the MAC address
105 protected final long MAC_RW_COOKIE = APP_COOKIE + 2;
Jonathan Hart832a7cb2013-06-24 11:25:35 +1200106 //Cookie for flows that setup BGP paths
107 protected final long BGP_COOKIE = APP_COOKIE + 3;
Jonathan Hart50a8d1e2013-06-06 16:00:47 +1200108 //Forwarding uses priority 0, and the mac rewrite entries in ingress switches
109 //need to be higher priority than this otherwise the rewrite may not get done
110 protected final short SDNIP_PRIORITY = 10;
Jonathan Hartc82051c2013-08-24 15:12:20 +1200111 protected final short ARP_PRIORITY = 20;
Jonathan Harte7e1c6e2013-06-04 20:50:23 -0700112
Jonathan Hart832a7cb2013-06-24 11:25:35 +1200113 protected final short BGP_PORT = 179;
114
Jonathan Hart98957bf2013-07-01 14:49:24 +1200115 protected final int TOPO_DETECTION_WAIT = 2; //seconds
116
Jonathan Hart832a7cb2013-06-24 11:25:35 +1200117 //Configuration stuff
Jonathan Hart1236a9b2013-06-18 22:10:05 +1200118 protected List<String> switches;
Jonathan Hart832a7cb2013-06-24 11:25:35 +1200119 protected Map<String, Interface> interfaces;
Jonathan Hartc824ad02013-07-03 15:58:45 +1200120 protected Map<InetAddress, BgpPeer> bgpPeers;
Jonathan Hart832a7cb2013-06-24 11:25:35 +1200121 protected SwitchPort bgpdAttachmentPoint;
Jonathan Hart2f790d22013-08-15 14:01:24 +1200122 protected MACAddress bgpdMacAddress;
Jonathan Hart1236a9b2013-06-18 22:10:05 +1200123
124 //True when all switches have connected
125 protected volatile boolean switchesConnected = false;
126 //True when we have a full mesh of shortest paths between gateways
127 protected volatile boolean topologyReady = false;
Jonathan Hart98957bf2013-07-01 14:49:24 +1200128
Jonathan Hart98957bf2013-07-01 14:49:24 +1200129 protected ArrayList<LDUpdate> linkUpdates;
130 protected SingletonTask topologyChangeDetectorTask;
131
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200132 protected SetMultimap<InetAddress, RibUpdate> prefixesWaitingOnArp;
Jonathan Hart309889c2013-08-13 23:26:24 +1200133
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200134 protected Map<InetAddress, Path> pathsWaitingOnArp;
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200135
Jonathan Hart0a46fe42013-08-10 17:08:47 +1200136 protected ExecutorService bgpUpdatesExecutor;
137
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200138 protected Map<InetAddress, Path> pushedPaths;
139 protected Map<Prefix, Path> prefixToPath;
Jonathan Hart0ee0f022013-08-03 22:21:54 +1200140 protected Multimap<Prefix, PushedFlowMod> pushedFlows;
Jonathan Hartdefa44d2013-08-15 19:51:13 +1200141
142 protected volatile Map<Long, ?> topoRouteTopology = null;
Jonathan Hart0ee0f022013-08-03 22:21:54 +1200143
Jonathan Hart98957bf2013-07-01 14:49:24 +1200144 protected class TopologyChangeDetector implements Runnable {
145 @Override
146 public void run() {
147 log.debug("Running topology change detection task");
148 synchronized (linkUpdates) {
Jonathan Hartc824ad02013-07-03 15:58:45 +1200149 //This is the model the REST API uses to retrieve network graph info
Jonathan Hart98957bf2013-07-01 14:49:24 +1200150 ITopoLinkService topoLinkService = new TopoLinkServiceImpl();
151
152 List<Link> activeLinks = topoLinkService.getActiveLinks();
Jonathan Hart98957bf2013-07-01 14:49:24 +1200153
154 Iterator<LDUpdate> it = linkUpdates.iterator();
155 while (it.hasNext()){
156 LDUpdate ldu = it.next();
157 Link l = new Link(ldu.getSrc(), ldu.getSrcPort(),
158 ldu.getDst(), ldu.getDstPort());
159
160 if (activeLinks.contains(l)){
Jonathan Hart98957bf2013-07-01 14:49:24 +1200161 it.remove();
162 }
163 }
164 }
165
Jonathan Hart64c0b202013-08-20 15:45:07 +1200166 if (!topologyReady) {
167 if (linkUpdates.isEmpty()){
168 //All updates have been seen in network map.
169 //We can check if topology is ready
170 log.debug("No known changes outstanding. Checking topology now");
171 checkStatus();
172 }
173 else {
174 //We know of some link updates that haven't propagated to the database yet
175 log.debug("Some changes not found in network map - {} links missing", linkUpdates.size());
176 topologyChangeDetectorTask.reschedule(TOPO_DETECTION_WAIT, TimeUnit.SECONDS);
177 }
Jonathan Hart98957bf2013-07-01 14:49:24 +1200178 }
179 }
180 }
Jonathan Harte7e1c6e2013-06-04 20:50:23 -0700181
Jonathan Hartd1f23252013-06-13 15:17:05 +1200182 private void readGatewaysConfiguration(String gatewaysFilename){
183 File gatewaysFile = new File(gatewaysFilename);
184 ObjectMapper mapper = new ObjectMapper();
Jonathan Harte7e1c6e2013-06-04 20:50:23 -0700185
Jonathan Hartd1f23252013-06-13 15:17:05 +1200186 try {
Jonathan Hart1236a9b2013-06-18 22:10:05 +1200187 Configuration config = mapper.readValue(gatewaysFile, Configuration.class);
188
Jonathan Hart1236a9b2013-06-18 22:10:05 +1200189 switches = config.getSwitches();
Jonathan Hart9575cb62013-07-05 13:43:49 +1200190 interfaces = new HashMap<String, Interface>();
191 for (Interface intf : config.getInterfaces()){
192 interfaces.put(intf.getName(), intf);
193 }
Jonathan Hartc824ad02013-07-03 15:58:45 +1200194 bgpPeers = new HashMap<InetAddress, BgpPeer>();
195 for (BgpPeer peer : config.getPeers()){
196 bgpPeers.put(peer.getIpAddress(), peer);
197 }
Jonathan Hart1236a9b2013-06-18 22:10:05 +1200198
Jonathan Hart832a7cb2013-06-24 11:25:35 +1200199 bgpdAttachmentPoint = new SwitchPort(
200 new Dpid(config.getBgpdAttachmentDpid()),
201 new Port(config.getBgpdAttachmentPort()));
Jonathan Hart1236a9b2013-06-18 22:10:05 +1200202
Jonathan Hart2f790d22013-08-15 14:01:24 +1200203 bgpdMacAddress = config.getBgpdMacAddress();
Jonathan Hartd1f23252013-06-13 15:17:05 +1200204 } catch (JsonParseException e) {
205 log.error("Error in JSON file", e);
206 System.exit(1);
207 } catch (JsonMappingException e) {
208 log.error("Error in JSON file", e);
209 System.exit(1);
210 } catch (IOException e) {
211 log.error("Error reading JSON file", e);
212 System.exit(1);
213 }
Jonathan Hartabf10222013-08-13 10:19:34 +1200214
215 //Populate the interface Patricia Trie
216 for (Interface intf : interfaces.values()) {
217 Prefix prefix = new Prefix(intf.getIpAddress().getAddress(), intf.getPrefixLength());
218 interfacePtrie.put(prefix, intf);
219 }
Jonathan Harte7e1c6e2013-06-04 20:50:23 -0700220 }
pingping-lina2cbfad2013-03-07 08:39:21 +0800221
222 @Override
223 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Jonathan Hart61ba9372013-05-19 20:10:29 -0700224 Collection<Class<? extends IFloodlightService>> l
225 = new ArrayList<Class<? extends IFloodlightService>>();
pingping-lina2cbfad2013-03-07 08:39:21 +0800226 l.add(IBgpRouteService.class);
227 return l;
228 }
229
230 @Override
231 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
Jonathan Hart61ba9372013-05-19 20:10:29 -0700232 Map<Class<? extends IFloodlightService>, IFloodlightService> m
233 = new HashMap<Class<? extends IFloodlightService>, IFloodlightService>();
pingping-line2a09ca2013-03-23 09:33:58 +0800234 m.put(IBgpRouteService.class, this);
pingping-lina2cbfad2013-03-07 08:39:21 +0800235 return m;
236 }
237
pingping-lina2cbfad2013-03-07 08:39:21 +0800238 @Override
239 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
Jonathan Hart61ba9372013-05-19 20:10:29 -0700240 Collection<Class<? extends IFloodlightService>> l
241 = new ArrayList<Class<? extends IFloodlightService>>();
pingping-lina2cbfad2013-03-07 08:39:21 +0800242 l.add(IFloodlightProviderService.class);
243 l.add(ITopologyService.class);
Jonathan Harte7e1c6e2013-06-04 20:50:23 -0700244 l.add(IRestApiService.class);
pingping-lina2cbfad2013-03-07 08:39:21 +0800245 return l;
246 }
247
248 @Override
249 public void init(FloodlightModuleContext context)
250 throws FloodlightModuleException {
251
Jonathan Hart29b972d2013-08-12 23:43:51 +1200252 ptree = new PatriciaTrie<RibEntry>(32);
Jonathan Hartabf10222013-08-13 10:19:34 +1200253 interfacePtrie = new PatriciaTrie<Interface>(32);
Jonathan Hart8b9349e2013-07-26 15:55:28 +1200254
255 ribUpdates = new LinkedBlockingQueue<RibUpdate>();
Jonathan Hartc824ad02013-07-03 15:58:45 +1200256
pingping-lina2cbfad2013-03-07 08:39:21 +0800257 // Register floodlight provider and REST handler.
258 floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
pingping-lina2cbfad2013-03-07 08:39:21 +0800259 topology = context.getServiceImpl(ITopologyService.class);
Jonathan Harte7694532013-09-12 12:34:46 +1200260 linkDiscoveryService = context.getServiceImpl(ILinkDiscoveryService.class);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200261 restApi = context.getServiceImpl(IRestApiService.class);
262
263 //TODO We'll initialise this here for now, but it should really be done as
264 //part of the controller core
Jonathan Hart08ee8522013-09-22 17:34:43 +1200265 proxyArp = new ProxyArpManager(floodlightProvider, topology, this);
pingping-lina2cbfad2013-03-07 08:39:21 +0800266
Jonathan Hart98957bf2013-07-01 14:49:24 +1200267 linkUpdates = new ArrayList<LDUpdate>();
268 ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
269 topologyChangeDetectorTask = new SingletonTask(executor, new TopologyChangeDetector());
Pavlin Radoslavovddd01ba2013-07-03 15:40:44 -0700270
271 topoRouteService = new TopoRouteService("");
Jonathan Hart98957bf2013-07-01 14:49:24 +1200272
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200273 pathsWaitingOnArp = new HashMap<InetAddress, Path>();
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200274 prefixesWaitingOnArp = Multimaps.synchronizedSetMultimap(
275 HashMultimap.<InetAddress, RibUpdate>create());
276
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200277 pushedPaths = new HashMap<InetAddress, Path>();
278 prefixToPath = new HashMap<Prefix, Path>();
Jonathan Hart0ee0f022013-08-03 22:21:54 +1200279 pushedFlows = HashMultimap.<Prefix, PushedFlowMod>create();
280
Jonathan Hart0a46fe42013-08-10 17:08:47 +1200281 bgpUpdatesExecutor = Executors.newSingleThreadExecutor(
282 new ThreadFactoryBuilder().setNameFormat("bgp-updates-%d").build());
283
Jonathan Hart61ba9372013-05-19 20:10:29 -0700284 //Read in config values
285 bgpdRestIp = context.getConfigParams(this).get("BgpdRestIp");
286 if (bgpdRestIp == null){
287 log.error("BgpdRestIp property not found in config file");
288 System.exit(1);
289 }
290 else {
291 log.info("BgpdRestIp set to {}", bgpdRestIp);
292 }
293
294 routerId = context.getConfigParams(this).get("RouterId");
295 if (routerId == null){
296 log.error("RouterId property not found in config file");
297 System.exit(1);
298 }
299 else {
300 log.info("RouterId set to {}", routerId);
301 }
Jonathan Hartd1f23252013-06-13 15:17:05 +1200302
Jonathan Hart9575cb62013-07-05 13:43:49 +1200303 String configFilenameParameter = context.getConfigParams(this).get("configfile");
304 if (configFilenameParameter != null){
305 configFilename = configFilenameParameter;
306 }
307 log.debug("Config file set to {}", configFilename);
308
309 readGatewaysConfiguration(configFilename);
Jonathan Hart2f790d22013-08-15 14:01:24 +1200310
Jonathan Hart08ee8522013-09-22 17:34:43 +1200311 //proxyArp.setL3Mode(interfacePtrie, interfaces.values(), bgpdMacAddress);
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200312 }
313
314 @Override
315 public void startUp(FloodlightModuleContext context) {
316 restApi.addRestletRoutable(new BgpRouteWebRoutable());
317 topology.addListener(this);
Jonathan Hart64c0b202013-08-20 15:45:07 +1200318 floodlightProvider.addOFSwitchListener(this);
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200319
Jonathan Hart2f790d22013-08-15 14:01:24 +1200320 proxyArp.startUp();
321
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200322 floodlightProvider.addOFMessageListener(OFType.PACKET_IN, proxyArp);
323
324 //Retrieve the RIB from BGPd during startup
325 retrieveRib();
pingping-lina2cbfad2013-03-07 08:39:21 +0800326 }
327
Jonathan Hart29b972d2013-08-12 23:43:51 +1200328 public IPatriciaTrie<RibEntry> getPtree() {
pingping-lina2cbfad2013-03-07 08:39:21 +0800329 return ptree;
330 }
Jonathan Hart61ba9372013-05-19 20:10:29 -0700331
332 public void clearPtree() {
Jonathan Hart29b972d2013-08-12 23:43:51 +1200333 ptree = new PatriciaTrie<RibEntry>(32);
pingping-line2a09ca2013-03-23 09:33:58 +0800334 }
Jonathan Hart61ba9372013-05-19 20:10:29 -0700335
pingping-line2a09ca2013-03-23 09:33:58 +0800336 public String getBGPdRestIp() {
Jonathan Hart61ba9372013-05-19 20:10:29 -0700337 return bgpdRestIp;
pingping-line2a09ca2013-03-23 09:33:58 +0800338 }
Jonathan Hart61ba9372013-05-19 20:10:29 -0700339
pingping-line2a09ca2013-03-23 09:33:58 +0800340 public String getRouterId() {
Jonathan Hart61ba9372013-05-19 20:10:29 -0700341 return routerId;
pingping-line2a09ca2013-03-23 09:33:58 +0800342 }
pingping-lina2cbfad2013-03-07 08:39:21 +0800343
Jonathan Hart61ba9372013-05-19 20:10:29 -0700344 private void retrieveRib(){
345 String url = "http://" + bgpdRestIp + "/wm/bgp/" + routerId;
346 String response = RestClient.get(url);
347
348 if (response.equals("")){
349 return;
350 }
351
352 response = response.replaceAll("\"", "'");
353 JSONObject jsonObj = (JSONObject) JSONSerializer.toJSON(response);
354 JSONArray rib_json_array = jsonObj.getJSONArray("rib");
355 String router_id = jsonObj.getString("router-id");
356
357 int size = rib_json_array.size();
358
359 log.info("Retrived RIB of {} entries from BGPd", size);
360
361 for (int j = 0; j < size; j++) {
362 JSONObject second_json_object = rib_json_array.getJSONObject(j);
363 String prefix = second_json_object.getString("prefix");
364 String nexthop = second_json_object.getString("nexthop");
365
366 //insert each rib entry into the local rib;
367 String[] substring = prefix.split("/");
368 String prefix1 = substring[0];
369 String mask1 = substring[1];
370
371 Prefix p;
372 try {
373 p = new Prefix(prefix1, Integer.valueOf(mask1));
374 } catch (NumberFormatException e) {
375 log.warn("Wrong mask format in RIB JSON: {}", mask1);
376 continue;
Jonathan Hart32e18222013-08-07 22:05:42 +1200377 } catch (IllegalArgumentException e1) {
Jonathan Hart61ba9372013-05-19 20:10:29 -0700378 log.warn("Wrong prefix format in RIB JSON: {}", prefix1);
379 continue;
380 }
381
Jonathan Hartb39a67d2013-08-10 23:59:50 +1200382 RibEntry rib = new RibEntry(router_id, nexthop);
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200383
Jonathan Hart0a46fe42013-08-10 17:08:47 +1200384 try {
385 ribUpdates.put(new RibUpdate(Operation.UPDATE, p, rib));
386 } catch (InterruptedException e) {
387 log.debug("Interrupted while pushing onto update queue");
388 }
Jonathan Hart61ba9372013-05-19 20:10:29 -0700389 }
390 }
391
Jonathan Hart8b9349e2013-07-26 15:55:28 +1200392 @Override
393 public void newRibUpdate(RibUpdate update) {
Jonathan Hart9ea31212013-08-12 21:40:34 +1200394 try {
395 ribUpdates.put(update);
396 } catch (InterruptedException e) {
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200397 log.debug("Interrupted while putting on ribUpdates queue", e);
398 Thread.currentThread().interrupt();
Jonathan Hart9ea31212013-08-12 21:40:34 +1200399 }
Jonathan Hart8b9349e2013-07-26 15:55:28 +1200400 }
401
Jonathan Hart0a46fe42013-08-10 17:08:47 +1200402 public synchronized void processRibAdd(RibUpdate update) {
Jonathan Hart8b9349e2013-07-26 15:55:28 +1200403 Prefix prefix = update.getPrefix();
404
Jonathan Hart9ea31212013-08-12 21:40:34 +1200405 log.debug("Processing prefix add {}", prefix);
406
Jonathan Hartb39a67d2013-08-10 23:59:50 +1200407 RibEntry rib = ptree.put(prefix, update.getRibEntry());
Jonathan Hart8b9349e2013-07-26 15:55:28 +1200408
Jonathan Hart0a46fe42013-08-10 17:08:47 +1200409 if (rib != null && !rib.equals(update.getRibEntry())) {
Jonathan Hart2f740782013-08-04 00:49:21 +1200410 //There was an existing nexthop for this prefix. This update supersedes that,
411 //so we need to remove the old flows for this prefix from the switches
Jonathan Hart309889c2013-08-13 23:26:24 +1200412 _processDeletePrefix(prefix, rib);
Jonathan Hart8b9349e2013-07-26 15:55:28 +1200413 }
Jonathan Hart2f740782013-08-04 00:49:21 +1200414
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200415 if (update.getRibEntry().getNextHop().equals(
416 InetAddresses.forString("0.0.0.0"))) {
Jonathan Hart309889c2013-08-13 23:26:24 +1200417 //Route originated by SDN domain
418 //We don't handle these at the moment
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200419 log.debug("Own route {} to {}", prefix,
420 update.getRibEntry().getNextHop().getHostAddress());
Jonathan Hart309889c2013-08-13 23:26:24 +1200421 return;
422 }
423
424 _processRibAdd(update);
Jonathan Hart8b9349e2013-07-26 15:55:28 +1200425 }
426
Jonathan Hart309889c2013-08-13 23:26:24 +1200427 private void _processRibAdd(RibUpdate update) {
428 Prefix prefix = update.getPrefix();
429 RibEntry rib = update.getRibEntry();
430
431 InetAddress dstIpAddress = rib.getNextHop();
432
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200433 //See if we know the MAC address of the next hop
Jonathan Hart309889c2013-08-13 23:26:24 +1200434 byte[] nextHopMacAddress = proxyArp.getMacAddress(rib.getNextHop());
Jonathan Hart309889c2013-08-13 23:26:24 +1200435
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200436 //Find the attachment point (egress interface) of the next hop
437 Interface egressInterface = null;
Jonathan Hart309889c2013-08-13 23:26:24 +1200438 if (bgpPeers.containsKey(dstIpAddress)) {
439 //Route to a peer
440 log.debug("Route to peer {}", dstIpAddress);
441 BgpPeer peer = bgpPeers.get(dstIpAddress);
442 egressInterface = interfaces.get(peer.getInterfaceName());
Jonathan Hart309889c2013-08-13 23:26:24 +1200443 }
444 else {
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200445 //Route to non-peer
Jonathan Hart309889c2013-08-13 23:26:24 +1200446 log.debug("Route to non-peer {}", dstIpAddress);
447 egressInterface = interfacePtrie.match(
448 new Prefix(dstIpAddress.getAddress(), 32));
449 if (egressInterface == null) {
450 log.warn("No outgoing interface found for {}", dstIpAddress.getHostAddress());
451 return;
452 }
Jonathan Hart309889c2013-08-13 23:26:24 +1200453 }
454
455 if (nextHopMacAddress == null) {
456 prefixesWaitingOnArp.put(dstIpAddress,
457 new RibUpdate(Operation.UPDATE, prefix, rib));
458 proxyArp.sendArpRequest(dstIpAddress, this, true);
459 return;
460 }
461 else {
462 if (!bgpPeers.containsKey(dstIpAddress)) {
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200463 //If the prefix is for a non-peer we need to ensure there's a path,
464 //and push one if there isn't.
465 Path path = pushedPaths.get(dstIpAddress);
Jonathan Hart309889c2013-08-13 23:26:24 +1200466 if (path == null) {
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200467 path = new Path(egressInterface, dstIpAddress);
Jonathan Hart08ee8522013-09-22 17:34:43 +1200468 //setUpDataPath(path, MACAddress.valueOf(nextHopMacAddress));
469 calculateAndPushPath(path, MACAddress.valueOf(nextHopMacAddress));
Jonathan Hart309889c2013-08-13 23:26:24 +1200470 pushedPaths.put(dstIpAddress, path);
471 }
Jonathan Hart309889c2013-08-13 23:26:24 +1200472
473 path.incrementUsers();
474 prefixToPath.put(prefix, path);
475 }
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200476
477 //For all prefixes we need to add the first-hop mac-rewriting flows
Jonathan Hart309889c2013-08-13 23:26:24 +1200478 addPrefixFlows(prefix, egressInterface, nextHopMacAddress);
479 }
480 }
481
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200482 private void addPrefixFlows(Prefix prefix, Interface egressInterface, byte[] nextHopMacAddress) {
483 log.debug("Adding flows for prefix {} added, next hop mac {}",
484 prefix, HexString.toHexString(nextHopMacAddress));
Jonathan Hartd5f2e952013-09-13 16:31:23 +1200485
486 //We only need one flow mod per switch, so pick one interface on each switch
487 Map<Long, Interface> srcInterfaces = new HashMap<Long, Interface>();
488 for (Interface intf : interfaces.values()) {
489 if (!srcInterfaces.containsKey(intf.getDpid())
490 && intf != egressInterface) {
491 srcInterfaces.put(intf.getDpid(), intf);
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200492 }
Jonathan Hartd5f2e952013-09-13 16:31:23 +1200493 }
494
495 //Add a flow to rewrite mac for this prefix to all other border switches
496 //for (Interface srcInterface : interfaces.values()) {
497 for (Interface srcInterface : srcInterfaces.values()) {
498 //if (srcInterface == egressInterface) {
499 //Don't push a flow for the switch where this peer is attached
500 //continue;
501 //}
Jonathan Hartdefa44d2013-08-15 19:51:13 +1200502
503
504 DataPath shortestPath;
505 if (topoRouteTopology == null) {
506 shortestPath = topoRouteService.getShortestPath(
507 srcInterface.getSwitchPort(),
508 egressInterface.getSwitchPort());
509 }
510 else {
511 shortestPath = topoRouteService.getTopoShortestPath(
512 topoRouteTopology, srcInterface.getSwitchPort(),
513 egressInterface.getSwitchPort());
514 }
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200515
516 if (shortestPath == null){
517 log.debug("Shortest path between {} and {} not found",
518 srcInterface.getSwitchPort(),
519 egressInterface.getSwitchPort());
520 return; // just quit here?
521 }
522
523 //Set up the flow mod
524 OFFlowMod fm = (OFFlowMod) floodlightProvider.getOFMessageFactory()
525 .getMessage(OFType.FLOW_MOD);
526
527 fm.setIdleTimeout((short)0)
528 .setHardTimeout((short)0)
529 .setBufferId(OFPacketOut.BUFFER_ID_NONE)
530 .setCookie(MAC_RW_COOKIE)
531 .setCommand(OFFlowMod.OFPFC_ADD)
532 .setPriority(SDNIP_PRIORITY)
533 .setLengthU(OFFlowMod.MINIMUM_LENGTH
534 + OFActionDataLayerDestination.MINIMUM_LENGTH
535 + OFActionOutput.MINIMUM_LENGTH);
536
537 OFMatch match = new OFMatch();
538 match.setDataLayerType(Ethernet.TYPE_IPv4);
539 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE);
540
541 match.setFromCIDR(prefix.toString(), OFMatch.STR_NW_DST);
542 fm.setMatch(match);
543
544 //Set up MAC rewrite action
545 OFActionDataLayerDestination macRewriteAction = new OFActionDataLayerDestination();
546 macRewriteAction.setDataLayerAddress(nextHopMacAddress);
547
548 //Set up output action
549 OFActionOutput outputAction = new OFActionOutput();
550 outputAction.setMaxLength((short)0xffff);
551 Port outputPort = shortestPath.flowEntries().get(0).outPort();
552 outputAction.setPort(outputPort.value());
553
554 List<OFAction> actions = new ArrayList<OFAction>();
555 actions.add(macRewriteAction);
556 actions.add(outputAction);
557 fm.setActions(actions);
558
559 //Write to switch
560 IOFSwitch sw = floodlightProvider.getSwitches()
561 .get(srcInterface.getDpid());
562
563 if (sw == null){
564 log.warn("Switch not found when pushing flow mod");
565 continue;
566 }
567
568 pushedFlows.put(prefix, new PushedFlowMod(sw.getId(), fm));
569
570 List<OFMessage> msglist = new ArrayList<OFMessage>();
571 msglist.add(fm);
572 try {
573 sw.write(msglist, null);
574 sw.flush();
Jonathan Hart08ee8522013-09-22 17:34:43 +1200575
576 /*
577 * XXX Rate limit hack!
578 * This should be solved properly by adding a rate limiting
579 * layer on top of the switches if we know they need it.
580 */
Jonathan Hart9971e1c2013-09-17 10:47:40 +1200581 Thread.sleep(1);
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200582 } catch (IOException e) {
583 log.error("Failure writing flow mod", e);
Jonathan Hart65139e42013-09-13 16:52:25 +1200584 } catch (InterruptedException e) {
585 // TODO handle this properly
586 log.error("Interrupted", e);
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200587 }
Jonathan Hart309889c2013-08-13 23:26:24 +1200588 }
589 }
Jonathan Hart309889c2013-08-13 23:26:24 +1200590
Jonathan Hart0a46fe42013-08-10 17:08:47 +1200591 public synchronized void processRibDelete(RibUpdate update) {
Jonathan Hart8b9349e2013-07-26 15:55:28 +1200592 Prefix prefix = update.getPrefix();
593
Jonathan Hartd7e158d2013-08-07 23:04:48 +1200594 if (ptree.remove(prefix, update.getRibEntry())) {
595 /*
596 * Only delete flows if an entry was actually removed from the trie.
597 * If no entry was removed, the <prefix, nexthop> wasn't there so
598 * it's probably already been removed and we don't need to do anything
599 */
Jonathan Hart309889c2013-08-13 23:26:24 +1200600 _processDeletePrefix(prefix, update.getRibEntry());
Jonathan Hart309889c2013-08-13 23:26:24 +1200601 }
602 }
603
604 private void _processDeletePrefix(Prefix prefix, RibEntry ribEntry) {
605 deletePrefixFlows(prefix);
606
607 log.debug("Deleting {} to {}", prefix, ribEntry.getNextHop());
Jonathan Hartd992f1a2013-09-13 13:46:44 +1200608
Jonathan Hart309889c2013-08-13 23:26:24 +1200609 if (!bgpPeers.containsKey(ribEntry.getNextHop())) {
610 log.debug("Getting path for route with non-peer nexthop");
Jonathan Hartfb1ebc52013-08-17 16:25:51 +1200611 //Path path = prefixToPath.get(prefix);
612 Path path = prefixToPath.remove(prefix);
Jonathan Hart309889c2013-08-13 23:26:24 +1200613
Jonathan Hartd992f1a2013-09-13 13:46:44 +1200614 if (path != null) {
615 //path could be null if we added to the Ptree but didn't push
616 //flows yet because we were waiting to resolve ARP
Jonathan Hart309889c2013-08-13 23:26:24 +1200617
Jonathan Hartd992f1a2013-09-13 13:46:44 +1200618 path.decrementUsers();
619 log.debug("users {}, permanent {}", path.getUsers(), path.isPermanent());
620 if (path.getUsers() <= 0 && !path.isPermanent()) {
621 deletePath(path);
622 pushedPaths.remove(path.getDstIpAddress());
623 }
Jonathan Hart309889c2013-08-13 23:26:24 +1200624 }
625 }
626 }
627
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200628 private void deletePrefixFlows(Prefix prefix) {
629 Collection<PushedFlowMod> pushedFlowMods
630 = pushedFlows.removeAll(prefix);
631
632 for (PushedFlowMod pfm : pushedFlowMods) {
633 log.debug("Pushing a DELETE flow mod to {}, matches prefix {} with mac-rewrite {}",
634 new Object[] {HexString.toHexString(pfm.getDpid()),
635 pfm.getFlowMod().getMatch().getNetworkDestination() +
636 pfm.getFlowMod().getMatch().getNetworkDestinationMaskLen(),
637 HexString.toHexString(((OFActionDataLayerDestination)pfm.getFlowMod().getActions().get(0))
638 .getDataLayerAddress())});
639
640 sendDeleteFlowMod(pfm.getFlowMod(), pfm.getDpid());
641 }
642 }
643
644 private void deletePath(Path path) {
Jonathan Hart309889c2013-08-13 23:26:24 +1200645 for (PushedFlowMod pfm : path.getFlowMods()) {
646 log.debug("Pushing a DELETE flow mod to {}, dst MAC {}",
647 new Object[] {HexString.toHexString(pfm.getDpid()),
648 HexString.toHexString(pfm.getFlowMod().getMatch().getDataLayerDestination())
649 });
650
651 sendDeleteFlowMod(pfm.getFlowMod(), pfm.getDpid());
652 }
653 }
654
655 private void sendDeleteFlowMod(OFFlowMod addFlowMod, long dpid) {
656 addFlowMod.setCommand(OFFlowMod.OFPFC_DELETE_STRICT)
657 .setOutPort(OFPort.OFPP_NONE)
658 .setLengthU(OFFlowMod.MINIMUM_LENGTH);
659
660 addFlowMod.getActions().clear();
661
662 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
663 if (sw == null) {
664 log.warn("Switch not found when pushing delete flow mod");
665 return;
666 }
667
668 try {
669 sw.write(addFlowMod, null);
670 sw.flush();
671 } catch (IOException e) {
672 log.error("Failure writing flow mod", e);
Jonathan Hartd7e158d2013-08-07 23:04:48 +1200673 }
Jonathan Hart8b9349e2013-07-26 15:55:28 +1200674 }
675
Jonathan Hart0ee0f022013-08-03 22:21:54 +1200676 //TODO test next-hop changes
Jonathan Hart2f740782013-08-04 00:49:21 +1200677 //TODO check delete/add synchronization
Jonathan Hart0ee0f022013-08-03 22:21:54 +1200678
Jonathan Harte7e1c6e2013-06-04 20:50:23 -0700679 /*
680 * On startup we need to calculate a full mesh of paths between all gateway
681 * switches
682 */
Jonathan Hart1236a9b2013-06-18 22:10:05 +1200683 private void setupFullMesh(){
Jonathan Harte7e1c6e2013-06-04 20:50:23 -0700684 //For each border router, calculate and install a path from every other
685 //border switch to said border router. However, don't install the entry
686 //in to the first hop switch, as we need to install an entry to rewrite
687 //for each prefix received. This will be done later when prefixes have
688 //actually been received.
689
Jonathan Hartc824ad02013-07-03 15:58:45 +1200690 for (BgpPeer peer : bgpPeers.values()) {
691 Interface peerInterface = interfaces.get(peer.getInterfaceName());
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200692
Jonathan Hart309889c2013-08-13 23:26:24 +1200693 //We know there's not already a Path here pushed, because this is
694 //called before all other routing
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200695 Path path = new Path(peerInterface, peer.getIpAddress());
Jonathan Hart309889c2013-08-13 23:26:24 +1200696 path.setPermanent();
697
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200698 //See if we know the MAC address of the peer. If not we can't
699 //do anything until we learn it
700 byte[] mac = proxyArp.getMacAddress(peer.getIpAddress());
701 if (mac == null) {
702 log.debug("Don't know MAC for {}", peer.getIpAddress().getHostAddress());
703 //Put in the pending paths list first
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200704 pathsWaitingOnArp.put(peer.getIpAddress(), path);
Jonathan Harte7e1c6e2013-06-04 20:50:23 -0700705
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200706 proxyArp.sendArpRequest(peer.getIpAddress(), this, true);
707 continue;
Jonathan Harte7e1c6e2013-06-04 20:50:23 -0700708 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200709
710 //If we know the MAC, lets go ahead and push the paths to this peer
Jonathan Hart08ee8522013-09-22 17:34:43 +1200711 //setUpDataPath(path, MACAddress.valueOf(mac));
712 calculateAndPushPath(path, MACAddress.valueOf(mac));
Jonathan Harte7e1c6e2013-06-04 20:50:23 -0700713 }
714 }
715
Jonathan Hart08ee8522013-09-22 17:34:43 +1200716 /*
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200717 private void setUpDataPath(Path path, MACAddress dstMacAddress) {
718 calculateAndPushPath(path, dstMacAddress);
Jonathan Hart309889c2013-08-13 23:26:24 +1200719 }
Jonathan Hart08ee8522013-09-22 17:34:43 +1200720 */
Jonathan Hart309889c2013-08-13 23:26:24 +1200721
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200722 private void calculateAndPushPath(Path path, MACAddress dstMacAddress) {
Jonathan Hart309889c2013-08-13 23:26:24 +1200723 Interface dstInterface = path.getDstInterface();
724
Jonathan Hartfb1ebc52013-08-17 16:25:51 +1200725 log.debug("Setting up path to {}, {}", path.getDstIpAddress().getHostAddress(),
726 dstMacAddress);
727
Jonathan Hart309889c2013-08-13 23:26:24 +1200728 List<PushedFlowMod> pushedFlows = new ArrayList<PushedFlowMod>();
729
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200730 for (Interface srcInterface : interfaces.values()) {
731 if (dstInterface.equals(srcInterface.getName())){
732 continue;
733 }
734
Jonathan Hartdefa44d2013-08-15 19:51:13 +1200735 DataPath shortestPath;
736 if (topoRouteTopology == null) {
737 log.debug("Using database topo");
738 shortestPath = topoRouteService.getShortestPath(
739 srcInterface.getSwitchPort(), dstInterface.getSwitchPort());
740 }
741 else {
742 log.debug("Using prepared topo");
743 shortestPath = topoRouteService.getTopoShortestPath(topoRouteTopology,
744 srcInterface.getSwitchPort(), dstInterface.getSwitchPort());
745 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200746
747 if (shortestPath == null){
748 log.debug("Shortest path between {} and {} not found",
749 srcInterface.getSwitchPort(), dstInterface.getSwitchPort());
750 return; // just quit here?
751 }
752
Jonathan Hart309889c2013-08-13 23:26:24 +1200753 pushedFlows.addAll(installPath(shortestPath.flowEntries(), dstMacAddress));
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200754 }
Jonathan Hart309889c2013-08-13 23:26:24 +1200755
756 path.setFlowMods(pushedFlows);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200757 }
758
Jonathan Hart309889c2013-08-13 23:26:24 +1200759 private List<PushedFlowMod> installPath(List<FlowEntry> flowEntries, MACAddress dstMacAddress){
760 List<PushedFlowMod> flowMods = new ArrayList<PushedFlowMod>();
761
Jonathan Harte7e1c6e2013-06-04 20:50:23 -0700762 //Set up the flow mod
763 OFFlowMod fm =
764 (OFFlowMod) floodlightProvider.getOFMessageFactory()
765 .getMessage(OFType.FLOW_MOD);
766
767 OFActionOutput action = new OFActionOutput();
768 action.setMaxLength((short)0xffff);
769 List<OFAction> actions = new ArrayList<OFAction>();
770 actions.add(action);
771
772 fm.setIdleTimeout((short)0)
773 .setHardTimeout((short)0)
774 .setBufferId(OFPacketOut.BUFFER_ID_NONE)
775 .setCookie(L2_FWD_COOKIE)
776 .setCommand(OFFlowMod.OFPFC_ADD)
Jonathan Harte7e1c6e2013-06-04 20:50:23 -0700777 .setActions(actions)
778 .setLengthU(OFFlowMod.MINIMUM_LENGTH+OFActionOutput.MINIMUM_LENGTH);
779
780 //Don't push the first flow entry. We need to push entries in the
781 //first switch based on IP prefix which we don't know yet.
782 for (int i = 1; i < flowEntries.size(); i++){
783 FlowEntry flowEntry = flowEntries.get(i);
784
785 OFMatch match = new OFMatch();
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200786 match.setDataLayerDestination(dstMacAddress.toBytes());
Jonathan Harte7e1c6e2013-06-04 20:50:23 -0700787 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_DST);
788 ((OFActionOutput) fm.getActions().get(0)).setPort(flowEntry.outPort().value());
789
790 fm.setMatch(match);
791
792 IOFSwitch sw = floodlightProvider.getSwitches().get(flowEntry.dpid().value());
793
794 if (sw == null){
795 log.warn("Switch not found when pushing flow mod");
796 continue;
797 }
798
Jonathan Hart309889c2013-08-13 23:26:24 +1200799 flowMods.add(new PushedFlowMod(sw.getId(), fm));
800
Jonathan Harte7e1c6e2013-06-04 20:50:23 -0700801 List<OFMessage> msglist = new ArrayList<OFMessage>();
802 msglist.add(fm);
803 try {
804 sw.write(msglist, null);
805 sw.flush();
806 } catch (IOException e) {
807 log.error("Failure writing flow mod", e);
808 }
809
810 try {
811 fm = fm.clone();
812 } catch (CloneNotSupportedException e1) {
813 log.error("Failure cloning flow mod", e1);
814 }
815 }
Jonathan Hart309889c2013-08-13 23:26:24 +1200816
817 return flowMods;
Jonathan Harte7e1c6e2013-06-04 20:50:23 -0700818 }
819
Jonathan Hart832a7cb2013-06-24 11:25:35 +1200820 private void setupBgpPaths(){
Jonathan Hartc824ad02013-07-03 15:58:45 +1200821 for (BgpPeer bgpPeer : bgpPeers.values()){
Jonathan Hart832a7cb2013-06-24 11:25:35 +1200822 Interface peerInterface = interfaces.get(bgpPeer.getInterfaceName());
823
824 DataPath path = topoRouteService.getShortestPath(
825 peerInterface.getSwitchPort(), bgpdAttachmentPoint);
826
827 if (path == null){
828 log.debug("Unable to compute path for BGP traffic for {}",
829 bgpPeer.getIpAddress());
830 continue;
831 }
832
833 //Set up the flow mod
834 OFFlowMod fm =
835 (OFFlowMod) floodlightProvider.getOFMessageFactory()
836 .getMessage(OFType.FLOW_MOD);
837
838 OFActionOutput action = new OFActionOutput();
839 action.setMaxLength((short)0xffff);
840 List<OFAction> actions = new ArrayList<OFAction>();
841 actions.add(action);
842
843 fm.setIdleTimeout((short)0)
844 .setHardTimeout((short)0)
845 .setBufferId(OFPacketOut.BUFFER_ID_NONE)
846 .setCookie(BGP_COOKIE)
847 .setCommand(OFFlowMod.OFPFC_ADD)
848 .setPriority(SDNIP_PRIORITY)
849 .setActions(actions)
850 .setLengthU(OFFlowMod.MINIMUM_LENGTH+OFActionOutput.MINIMUM_LENGTH);
851
852 //Forward = gateway -> bgpd, reverse = bgpd -> gateway
853 OFMatch forwardMatchSrc = new OFMatch();
854
Jonathan Hart832a7cb2013-06-24 11:25:35 +1200855 String interfaceCidrAddress = peerInterface.getIpAddress().getHostAddress()
856 + "/32";
857 String peerCidrAddress = bgpPeer.getIpAddress().getHostAddress()
858 + "/32";
Jonathan Hart38c84932013-08-10 17:49:27 +1200859
Jonathan Hart832a7cb2013-06-24 11:25:35 +1200860 //Common match fields
861 forwardMatchSrc.setDataLayerType(Ethernet.TYPE_IPv4);
Jonathan Hart832a7cb2013-06-24 11:25:35 +1200862 forwardMatchSrc.setNetworkProtocol(IPv4.PROTOCOL_TCP);
863 forwardMatchSrc.setTransportDestination(BGP_PORT);
864 forwardMatchSrc.setWildcards(forwardMatchSrc.getWildcards() & ~OFMatch.OFPFW_IN_PORT
865 & ~OFMatch.OFPFW_DL_TYPE & ~OFMatch.OFPFW_NW_PROTO);
866
867
868 OFMatch reverseMatchSrc = forwardMatchSrc.clone();
869
870 forwardMatchSrc.setFromCIDR(peerCidrAddress, OFMatch.STR_NW_SRC);
871 forwardMatchSrc.setFromCIDR(interfaceCidrAddress, OFMatch.STR_NW_DST);
872
873 OFMatch forwardMatchDst = forwardMatchSrc.clone();
874
875 forwardMatchSrc.setTransportSource(BGP_PORT);
876 forwardMatchSrc.setWildcards(forwardMatchSrc.getWildcards() & ~OFMatch.OFPFW_TP_SRC);
877 forwardMatchDst.setTransportDestination(BGP_PORT);
878 forwardMatchDst.setWildcards(forwardMatchDst.getWildcards() & ~OFMatch.OFPFW_TP_DST);
879
880 reverseMatchSrc.setFromCIDR(interfaceCidrAddress, OFMatch.STR_NW_SRC);
881 reverseMatchSrc.setFromCIDR(peerCidrAddress, OFMatch.STR_NW_DST);
882
883 OFMatch reverseMatchDst = reverseMatchSrc.clone();
884
885 reverseMatchSrc.setTransportSource(BGP_PORT);
886 reverseMatchSrc.setWildcards(forwardMatchSrc.getWildcards() & ~OFMatch.OFPFW_TP_SRC);
887 reverseMatchDst.setTransportDestination(BGP_PORT);
888 reverseMatchDst.setWildcards(forwardMatchDst.getWildcards() & ~OFMatch.OFPFW_TP_DST);
889
890 fm.setMatch(forwardMatchSrc);
891
Jonathan Hart38c84932013-08-10 17:49:27 +1200892 OFMatch forwardIcmpMatch = new OFMatch();
893 forwardIcmpMatch.setDataLayerType(Ethernet.TYPE_IPv4);
894 forwardIcmpMatch.setNetworkProtocol(IPv4.PROTOCOL_ICMP);
895 forwardIcmpMatch.setWildcards(forwardIcmpMatch.getWildcards() &
896 ~OFMatch.OFPFW_DL_TYPE & ~OFMatch.OFPFW_NW_PROTO);
897
898 OFMatch reverseIcmpMatch = forwardIcmpMatch.clone();
899 forwardIcmpMatch.setFromCIDR(interfaceCidrAddress, OFMatch.STR_NW_DST);
900 reverseIcmpMatch.setFromCIDR(interfaceCidrAddress, OFMatch.STR_NW_SRC);
901
Jonathan Hart832a7cb2013-06-24 11:25:35 +1200902 for (FlowEntry flowEntry : path.flowEntries()){
903 OFFlowMod forwardFlowModSrc, forwardFlowModDst;
904 OFFlowMod reverseFlowModSrc, reverseFlowModDst;
Jonathan Hart38c84932013-08-10 17:49:27 +1200905 OFFlowMod forwardIcmp, reverseIcmp;
Jonathan Hart832a7cb2013-06-24 11:25:35 +1200906 try {
907 forwardFlowModSrc = fm.clone();
908 forwardFlowModDst = fm.clone();
909 reverseFlowModSrc = fm.clone();
910 reverseFlowModDst = fm.clone();
Jonathan Hart38c84932013-08-10 17:49:27 +1200911 forwardIcmp = fm.clone();
912 reverseIcmp = fm.clone();
Jonathan Hart832a7cb2013-06-24 11:25:35 +1200913 } catch (CloneNotSupportedException e) {
914 log.warn("Clone failed", e);
915 continue;
916 }
917
918 forwardMatchSrc.setInputPort(flowEntry.inPort().value());
919 forwardFlowModSrc.setMatch(forwardMatchSrc);
920 ((OFActionOutput)forwardFlowModSrc.getActions().get(0))
921 .setPort(flowEntry.outPort().value());
922
923 forwardMatchDst.setInputPort(flowEntry.inPort().value());
924 forwardFlowModDst.setMatch(forwardMatchDst);
925 ((OFActionOutput)forwardFlowModDst.getActions().get(0))
926 .setPort(flowEntry.outPort().value());
927
928 reverseMatchSrc.setInputPort(flowEntry.outPort().value());
929 reverseFlowModSrc.setMatch(reverseMatchSrc);
930 ((OFActionOutput)reverseFlowModSrc.getActions().get(0))
931 .setPort(flowEntry.inPort().value());
932
933 reverseMatchDst.setInputPort(flowEntry.outPort().value());
934 reverseFlowModDst.setMatch(reverseMatchDst);
935 ((OFActionOutput)reverseFlowModDst.getActions().get(0))
936 .setPort(flowEntry.inPort().value());
937
Jonathan Hart38c84932013-08-10 17:49:27 +1200938 ((OFActionOutput)forwardIcmp.getActions().get(0))
939 .setPort(flowEntry.outPort().value());
940 forwardIcmp.setMatch(forwardIcmpMatch);
941
942 ((OFActionOutput)reverseIcmp.getActions().get(0))
943 .setPort(flowEntry.inPort().value());
944 reverseIcmp.setMatch(reverseIcmpMatch);
945
946
Jonathan Hart832a7cb2013-06-24 11:25:35 +1200947 IOFSwitch sw = floodlightProvider.getSwitches().get(flowEntry.dpid().value());
948
Jonathan Hart38c84932013-08-10 17:49:27 +1200949 if (sw == null) {
950 log.warn("Switch not found when pushing BGP paths");
951 return;
952 }
953
Jonathan Hart832a7cb2013-06-24 11:25:35 +1200954 List<OFMessage> msgList = new ArrayList<OFMessage>(2);
955 msgList.add(forwardFlowModSrc);
956 msgList.add(forwardFlowModDst);
957 msgList.add(reverseFlowModSrc);
958 msgList.add(reverseFlowModDst);
Jonathan Hart38c84932013-08-10 17:49:27 +1200959 msgList.add(forwardIcmp);
960 msgList.add(reverseIcmp);
Jonathan Hart832a7cb2013-06-24 11:25:35 +1200961
962 try {
963 sw.write(msgList, null);
964 sw.flush();
965 } catch (IOException e) {
966 log.error("Failure writing flow mod", e);
967 }
968 }
969 }
970 }
Jonathan Hart1236a9b2013-06-18 22:10:05 +1200971
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200972 @Override
973 public void arpResponse(InetAddress ipAddress, byte[] macAddress) {
974 log.debug("Received ARP response: {} => {}", ipAddress.getHostAddress(),
975 MACAddress.valueOf(macAddress).toString());
976
Jonathan Hart0a46fe42013-08-10 17:08:47 +1200977 /*
978 * We synchronize on this to prevent changes to the ptree while we're pushing
979 * flows to the switches. If the ptree changes, the ptree and switches
980 * could get out of sync.
981 */
982 synchronized (this) {
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200983 Path path = pathsWaitingOnArp.remove(ipAddress);
Jonathan Hart309889c2013-08-13 23:26:24 +1200984
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200985 if (path != null) {
Jonathan Hart309889c2013-08-13 23:26:24 +1200986 log.debug("Pushing path to {} at {} on {}", new Object[] {
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200987 path.getDstIpAddress().getHostAddress(),
Jonathan Hart309889c2013-08-13 23:26:24 +1200988 MACAddress.valueOf(macAddress),
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200989 path.getDstInterface().getSwitchPort()});
990 //These paths should always be to BGP peers. Paths to non-peers are
991 //handled once the first prefix is ready to push
992 if (pushedPaths.containsKey(path.getDstInterface())) {
Jonathan Hart309889c2013-08-13 23:26:24 +1200993 //A path already got pushed to this endpoint while we were waiting
994 //for ARP. We'll copy over the permanent attribute if it is set on this path.
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200995 if (path.isPermanent()) {
996 pushedPaths.get(path.getDstInterface()).setPermanent();
Jonathan Hart309889c2013-08-13 23:26:24 +1200997 }
998 }
999 else {
Jonathan Hart08ee8522013-09-22 17:34:43 +12001000 //setUpDataPath(path, MACAddress.valueOf(macAddress));
1001 calculateAndPushPath(path, MACAddress.valueOf(macAddress));
Jonathan Harta23ffdb2013-08-14 14:36:54 +12001002 pushedPaths.put(path.getDstIpAddress(), path);
Jonathan Hart309889c2013-08-13 23:26:24 +12001003 }
1004 }
1005
Jonathan Hart309889c2013-08-13 23:26:24 +12001006 Set<RibUpdate> prefixesToPush = prefixesWaitingOnArp.removeAll(ipAddress);
1007
Jonathan Hart0a46fe42013-08-10 17:08:47 +12001008 for (RibUpdate update : prefixesToPush) {
1009 //These will always be adds
1010
Jonathan Hartb39a67d2013-08-10 23:59:50 +12001011 RibEntry rib = ptree.lookup(update.getPrefix());
Jonathan Hart0a46fe42013-08-10 17:08:47 +12001012 if (rib != null && rib.equals(update.getRibEntry())) {
1013 log.debug("Pushing prefix {} next hop {}", update.getPrefix(),
Jonathan Hartb39a67d2013-08-10 23:59:50 +12001014 rib.getNextHop().getHostAddress());
Jonathan Hart0a46fe42013-08-10 17:08:47 +12001015 //We only push prefix flows if the prefix is still in the ptree
1016 //and the next hop is the same as our update. The prefix could
1017 //have been removed while we were waiting for the ARP, or the
1018 //next hop could have changed.
Jonathan Hart309889c2013-08-13 23:26:24 +12001019 _processRibAdd(update);
Jonathan Hart0a46fe42013-08-10 17:08:47 +12001020 } else {
1021 log.debug("Received ARP response, but {},{} is no longer in ptree",
1022 update.getPrefix(), update.getRibEntry());
1023 }
1024 }
Jonathan Hart0ee0f022013-08-03 22:21:54 +12001025 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +12001026 }
1027
Jonathan Hartc82051c2013-08-24 15:12:20 +12001028 private void setupArpFlows() {
1029 OFMatch match = new OFMatch();
1030 match.setDataLayerType(Ethernet.TYPE_ARP);
1031 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE);
1032
1033 OFFlowMod fm = new OFFlowMod();
1034 fm.setMatch(match);
1035
1036 OFActionOutput action = new OFActionOutput();
1037 action.setPort(OFPort.OFPP_CONTROLLER.getValue());
1038 action.setMaxLength((short)0xffff);
1039 List<OFAction> actions = new ArrayList<OFAction>(1);
1040 actions.add(action);
1041 fm.setActions(actions);
1042
1043 fm.setIdleTimeout((short)0)
1044 .setHardTimeout((short)0)
1045 .setBufferId(OFPacketOut.BUFFER_ID_NONE)
1046 .setCookie(0)
1047 .setCommand(OFFlowMod.OFPFC_ADD)
1048 .setPriority(ARP_PRIORITY)
1049 .setLengthU(OFFlowMod.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH);
1050
1051 for (String strdpid : switches){
1052 IOFSwitch sw = floodlightProvider.getSwitches().get(HexString.toLong(strdpid));
1053 if (sw == null) {
1054 log.debug("Couldn't find switch to push ARP flow");
1055 }
1056 else {
1057 try {
1058 sw.write(fm, null);
1059 } catch (IOException e) {
1060 log.warn("Failure writing ARP flow to switch", e);
1061 }
1062 }
1063 }
1064 }
1065
Jonathan Hartf886fa12013-09-18 14:46:29 +12001066 private void setupDefaultDropFlows() {
1067 OFFlowMod fm = new OFFlowMod();
1068 fm.setMatch(new OFMatch());
1069 //No action means drop
1070
1071 fm.setIdleTimeout((short)0)
1072 .setHardTimeout((short)0)
1073 .setBufferId(OFPacketOut.BUFFER_ID_NONE)
1074 .setCookie(0)
1075 .setCommand(OFFlowMod.OFPFC_ADD)
1076 .setPriority((short)0)
1077 .setLengthU(OFFlowMod.MINIMUM_LENGTH);
1078
1079 for (String strdpid : switches){
1080 IOFSwitch sw = floodlightProvider.getSwitches().get(HexString.toLong(strdpid));
1081 if (sw == null) {
1082 log.debug("Couldn't find switch to push default deny flow");
1083 }
1084 else {
1085 try {
1086 sw.write(fm, null);
1087 } catch (IOException e) {
1088 log.warn("Failure writing default deny flow to switch", e);
1089 }
1090 }
1091 }
1092 }
1093
Jonathan Hart1236a9b2013-06-18 22:10:05 +12001094 private void beginRouting(){
1095 log.debug("Topology is now ready, beginning routing function");
Jonathan Hartdefa44d2013-08-15 19:51:13 +12001096 topoRouteTopology = topoRouteService.prepareShortestPathTopo();
1097
Jonathan Hartc82051c2013-08-24 15:12:20 +12001098 setupArpFlows();
Jonathan Hartf886fa12013-09-18 14:46:29 +12001099 setupDefaultDropFlows();
Jonathan Hartc82051c2013-08-24 15:12:20 +12001100
Jonathan Hart832a7cb2013-06-24 11:25:35 +12001101 setupBgpPaths();
Jonathan Hart98957bf2013-07-01 14:49:24 +12001102 setupFullMesh();
Jonathan Harte7694532013-09-12 12:34:46 +12001103
1104 //Suppress link discovery on external-facing router ports
1105 for (Interface intf : interfaces.values()) {
1106 linkDiscoveryService.AddToSuppressLLDPs(intf.getDpid(), intf.getPort());
1107 }
1108
Jonathan Hart0a46fe42013-08-10 17:08:47 +12001109 bgpUpdatesExecutor.execute(new Runnable() {
1110 @Override
1111 public void run() {
1112 doUpdatesThread();
1113 }
1114 });
Jonathan Hart1236a9b2013-06-18 22:10:05 +12001115 }
1116
1117 private void checkSwitchesConnected(){
1118 for (String dpid : switches){
1119 if (floodlightProvider.getSwitches().get(HexString.toLong(dpid)) == null){
1120 log.debug("Not all switches are here yet");
1121 return;
1122 }
1123 }
1124 switchesConnected = true;
1125 }
1126
Jonathan Hartc824ad02013-07-03 15:58:45 +12001127 //Actually we only need to go half way round to verify full mesh connectivity
1128 //(n^2)/2
Jonathan Hart1236a9b2013-06-18 22:10:05 +12001129 private void checkTopologyReady(){
Jonathan Hartc824ad02013-07-03 15:58:45 +12001130 for (Interface dstInterface : interfaces.values()) {
1131 for (Interface srcInterface : interfaces.values()) {
1132 if (dstInterface == srcInterface) {
Jonathan Hart1236a9b2013-06-18 22:10:05 +12001133 continue;
1134 }
1135
Jonathan Hart1236a9b2013-06-18 22:10:05 +12001136 DataPath shortestPath = topoRouteService.getShortestPath(
Jonathan Hartc824ad02013-07-03 15:58:45 +12001137 srcInterface.getSwitchPort(), dstInterface.getSwitchPort());
Jonathan Hart1236a9b2013-06-18 22:10:05 +12001138
1139 if (shortestPath == null){
1140 log.debug("Shortest path between {} and {} not found",
Jonathan Hartc824ad02013-07-03 15:58:45 +12001141 srcInterface.getSwitchPort(), dstInterface.getSwitchPort());
Jonathan Hart1236a9b2013-06-18 22:10:05 +12001142 return;
1143 }
1144 }
1145 }
1146 topologyReady = true;
1147 }
1148
1149 private void checkStatus(){
Jonathan Hart1236a9b2013-06-18 22:10:05 +12001150 if (!switchesConnected){
1151 checkSwitchesConnected();
1152 }
1153 boolean oldTopologyReadyStatus = topologyReady;
1154 if (switchesConnected && !topologyReady){
1155 checkTopologyReady();
1156 }
1157 if (!oldTopologyReadyStatus && topologyReady){
1158 beginRouting();
1159 }
1160 }
Jonathan Harta23ffdb2013-08-14 14:36:54 +12001161
Jonathan Hart8b9349e2013-07-26 15:55:28 +12001162 private void doUpdatesThread() {
1163 boolean interrupted = false;
1164 try {
1165 while (true) {
1166 try {
1167 RibUpdate update = ribUpdates.take();
1168 switch (update.getOperation()){
1169 case UPDATE:
Jonathan Hart2f740782013-08-04 00:49:21 +12001170 processRibAdd(update);
Jonathan Hart8b9349e2013-07-26 15:55:28 +12001171 break;
1172 case DELETE:
Jonathan Hart2f740782013-08-04 00:49:21 +12001173 processRibDelete(update);
Jonathan Hart8b9349e2013-07-26 15:55:28 +12001174 break;
1175 }
1176 } catch (InterruptedException e) {
Jonathan Harta23ffdb2013-08-14 14:36:54 +12001177 log.debug("Interrupted while taking from updates queue", e);
Jonathan Hart8b9349e2013-07-26 15:55:28 +12001178 interrupted = true;
Jonathan Hart309889c2013-08-13 23:26:24 +12001179 } catch (Exception e) {
1180 log.debug("exception", e);
Jonathan Hart8b9349e2013-07-26 15:55:28 +12001181 }
1182 }
1183 } finally {
1184 if (interrupted) {
1185 Thread.currentThread().interrupt();
1186 }
1187 }
1188 }
pingping-lina2cbfad2013-03-07 08:39:21 +08001189
1190 @Override
Jonathan Hart64c0b202013-08-20 15:45:07 +12001191 public void topologyChanged() {
1192 if (topologyReady) {
1193 return;
1194 }
1195
Jonathan Hart1236a9b2013-06-18 22:10:05 +12001196 boolean refreshNeeded = false;
1197 for (LDUpdate ldu : topology.getLastLinkUpdates()){
1198 if (!ldu.getOperation().equals(ILinkDiscovery.UpdateOperation.LINK_UPDATED)){
1199 //We don't need to recalculate anything for just link updates
Jonathan Hartb39a67d2013-08-10 23:59:50 +12001200 //They happen very frequently
Jonathan Hart1236a9b2013-06-18 22:10:05 +12001201 refreshNeeded = true;
1202 }
Jonathan Hart98957bf2013-07-01 14:49:24 +12001203
Jonathan Hart1236a9b2013-06-18 22:10:05 +12001204 log.debug("Topo change {}", ldu.getOperation());
Jonathan Hartc824ad02013-07-03 15:58:45 +12001205
Jonathan Hart98957bf2013-07-01 14:49:24 +12001206 if (ldu.getOperation().equals(ILinkDiscovery.UpdateOperation.LINK_ADDED)){
1207 synchronized (linkUpdates) {
1208 linkUpdates.add(ldu);
1209 }
1210 }
Jonathan Hart1236a9b2013-06-18 22:10:05 +12001211 }
1212
Jonathan Hart64c0b202013-08-20 15:45:07 +12001213 if (refreshNeeded && !topologyReady){
Jonathan Hart98957bf2013-07-01 14:49:24 +12001214 topologyChangeDetectorTask.reschedule(TOPO_DETECTION_WAIT, TimeUnit.SECONDS);
pingping-lina2cbfad2013-03-07 08:39:21 +08001215 }
Jonathan Hart1236a9b2013-06-18 22:10:05 +12001216 }
Jonathan Hart64c0b202013-08-20 15:45:07 +12001217
1218 @Override
1219 public void addedSwitch(IOFSwitch sw) {
1220 if (!topologyReady) {
1221 sw.clearAllFlowMods();
1222 }
1223 }
1224
1225 @Override
Jonathan Hart08ee8522013-09-22 17:34:43 +12001226 public void removedSwitch(IOFSwitch sw) {}
Jonathan Hart64c0b202013-08-20 15:45:07 +12001227
1228 @Override
Jonathan Hart08ee8522013-09-22 17:34:43 +12001229 public void switchPortChanged(Long switchId) {}
Jonathan Hart64c0b202013-08-20 15:45:07 +12001230
1231 @Override
1232 public String getName() {
Jonathan Hart08ee8522013-09-22 17:34:43 +12001233 return "BgpRoute";
1234 }
1235
1236 /*
1237 * ILayer3InfoService methods
1238 */
1239
1240 @Override
1241 public boolean isInterfaceAddress(InetAddress address) {
1242 Interface intf = interfacePtrie.match(new Prefix(address.getAddress(), 32));
1243 return (intf != null && intf.getIpAddress().equals(address));
1244 }
1245
1246 @Override
1247 public boolean inConnectedNetwork(InetAddress address) {
1248 Interface intf = interfacePtrie.match(new Prefix(address.getAddress(), 32));
1249 return (intf != null && !intf.getIpAddress().equals(address));
1250 }
1251
1252 @Override
1253 public boolean fromExternalNetwork(long inDpid, short inPort) {
1254 for (Interface intf : interfaces.values()) {
1255 if (intf.getDpid() == inDpid && intf.getPort() == inPort) {
1256 return true;
1257 }
1258 }
1259 return false;
1260 }
1261
1262 @Override
1263 public Interface getOutgoingInterface(InetAddress dstIpAddress) {
1264 return interfacePtrie.match(new Prefix(dstIpAddress.getAddress(), 32));
1265 }
1266
1267 @Override
1268 public boolean hasLayer3Configuration() {
1269 return !interfaces.isEmpty();
1270 }
1271
1272 @Override
1273 public MACAddress getRouterMacAddress() {
1274 return bgpdMacAddress;
Jonathan Hart64c0b202013-08-20 15:45:07 +12001275 }
pingping-lina2cbfad2013-03-07 08:39:21 +08001276}