blob: 5bbaa6ce86e992cfb626a342a81d3970d612ed22 [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 Harta23ffdb2013-08-14 14:36:54 +1200310 }
311
312 @Override
313 public void startUp(FloodlightModuleContext context) {
314 restApi.addRestletRoutable(new BgpRouteWebRoutable());
315 topology.addListener(this);
Jonathan Hart64c0b202013-08-20 15:45:07 +1200316 floodlightProvider.addOFSwitchListener(this);
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200317
Jonathan Hart2f790d22013-08-15 14:01:24 +1200318 proxyArp.startUp();
319
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200320 floodlightProvider.addOFMessageListener(OFType.PACKET_IN, proxyArp);
321
322 //Retrieve the RIB from BGPd during startup
323 retrieveRib();
pingping-lina2cbfad2013-03-07 08:39:21 +0800324 }
325
Jonathan Hart29b972d2013-08-12 23:43:51 +1200326 public IPatriciaTrie<RibEntry> getPtree() {
pingping-lina2cbfad2013-03-07 08:39:21 +0800327 return ptree;
328 }
Jonathan Hart61ba9372013-05-19 20:10:29 -0700329
330 public void clearPtree() {
Jonathan Hart29b972d2013-08-12 23:43:51 +1200331 ptree = new PatriciaTrie<RibEntry>(32);
pingping-line2a09ca2013-03-23 09:33:58 +0800332 }
Jonathan Hart61ba9372013-05-19 20:10:29 -0700333
pingping-line2a09ca2013-03-23 09:33:58 +0800334 public String getBGPdRestIp() {
Jonathan Hart61ba9372013-05-19 20:10:29 -0700335 return bgpdRestIp;
pingping-line2a09ca2013-03-23 09:33:58 +0800336 }
Jonathan Hart61ba9372013-05-19 20:10:29 -0700337
pingping-line2a09ca2013-03-23 09:33:58 +0800338 public String getRouterId() {
Jonathan Hart61ba9372013-05-19 20:10:29 -0700339 return routerId;
pingping-line2a09ca2013-03-23 09:33:58 +0800340 }
pingping-lina2cbfad2013-03-07 08:39:21 +0800341
Jonathan Hart61ba9372013-05-19 20:10:29 -0700342 private void retrieveRib(){
343 String url = "http://" + bgpdRestIp + "/wm/bgp/" + routerId;
344 String response = RestClient.get(url);
345
346 if (response.equals("")){
347 return;
348 }
349
350 response = response.replaceAll("\"", "'");
351 JSONObject jsonObj = (JSONObject) JSONSerializer.toJSON(response);
352 JSONArray rib_json_array = jsonObj.getJSONArray("rib");
353 String router_id = jsonObj.getString("router-id");
354
355 int size = rib_json_array.size();
356
357 log.info("Retrived RIB of {} entries from BGPd", size);
358
359 for (int j = 0; j < size; j++) {
360 JSONObject second_json_object = rib_json_array.getJSONObject(j);
361 String prefix = second_json_object.getString("prefix");
362 String nexthop = second_json_object.getString("nexthop");
363
364 //insert each rib entry into the local rib;
365 String[] substring = prefix.split("/");
366 String prefix1 = substring[0];
367 String mask1 = substring[1];
368
369 Prefix p;
370 try {
371 p = new Prefix(prefix1, Integer.valueOf(mask1));
372 } catch (NumberFormatException e) {
373 log.warn("Wrong mask format in RIB JSON: {}", mask1);
374 continue;
Jonathan Hart32e18222013-08-07 22:05:42 +1200375 } catch (IllegalArgumentException e1) {
Jonathan Hart61ba9372013-05-19 20:10:29 -0700376 log.warn("Wrong prefix format in RIB JSON: {}", prefix1);
377 continue;
378 }
379
Jonathan Hartb39a67d2013-08-10 23:59:50 +1200380 RibEntry rib = new RibEntry(router_id, nexthop);
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200381
Jonathan Hart0a46fe42013-08-10 17:08:47 +1200382 try {
383 ribUpdates.put(new RibUpdate(Operation.UPDATE, p, rib));
384 } catch (InterruptedException e) {
385 log.debug("Interrupted while pushing onto update queue");
386 }
Jonathan Hart61ba9372013-05-19 20:10:29 -0700387 }
388 }
389
Jonathan Hart8b9349e2013-07-26 15:55:28 +1200390 @Override
391 public void newRibUpdate(RibUpdate update) {
Jonathan Hart9ea31212013-08-12 21:40:34 +1200392 try {
393 ribUpdates.put(update);
394 } catch (InterruptedException e) {
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200395 log.debug("Interrupted while putting on ribUpdates queue", e);
396 Thread.currentThread().interrupt();
Jonathan Hart9ea31212013-08-12 21:40:34 +1200397 }
Jonathan Hart8b9349e2013-07-26 15:55:28 +1200398 }
399
Jonathan Hart0a46fe42013-08-10 17:08:47 +1200400 public synchronized void processRibAdd(RibUpdate update) {
Jonathan Hart8b9349e2013-07-26 15:55:28 +1200401 Prefix prefix = update.getPrefix();
402
Jonathan Hart9ea31212013-08-12 21:40:34 +1200403 log.debug("Processing prefix add {}", prefix);
404
Jonathan Hartb39a67d2013-08-10 23:59:50 +1200405 RibEntry rib = ptree.put(prefix, update.getRibEntry());
Jonathan Hart8b9349e2013-07-26 15:55:28 +1200406
Jonathan Hart0a46fe42013-08-10 17:08:47 +1200407 if (rib != null && !rib.equals(update.getRibEntry())) {
Jonathan Hart2f740782013-08-04 00:49:21 +1200408 //There was an existing nexthop for this prefix. This update supersedes that,
409 //so we need to remove the old flows for this prefix from the switches
Jonathan Hart309889c2013-08-13 23:26:24 +1200410 _processDeletePrefix(prefix, rib);
Jonathan Hart8b9349e2013-07-26 15:55:28 +1200411 }
Jonathan Hart2f740782013-08-04 00:49:21 +1200412
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200413 if (update.getRibEntry().getNextHop().equals(
414 InetAddresses.forString("0.0.0.0"))) {
Jonathan Hart309889c2013-08-13 23:26:24 +1200415 //Route originated by SDN domain
416 //We don't handle these at the moment
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200417 log.debug("Own route {} to {}", prefix,
418 update.getRibEntry().getNextHop().getHostAddress());
Jonathan Hart309889c2013-08-13 23:26:24 +1200419 return;
420 }
421
422 _processRibAdd(update);
Jonathan Hart8b9349e2013-07-26 15:55:28 +1200423 }
424
Jonathan Hart309889c2013-08-13 23:26:24 +1200425 private void _processRibAdd(RibUpdate update) {
426 Prefix prefix = update.getPrefix();
427 RibEntry rib = update.getRibEntry();
428
429 InetAddress dstIpAddress = rib.getNextHop();
430
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200431 //See if we know the MAC address of the next hop
Jonathan Hart309889c2013-08-13 23:26:24 +1200432 byte[] nextHopMacAddress = proxyArp.getMacAddress(rib.getNextHop());
Jonathan Hart309889c2013-08-13 23:26:24 +1200433
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200434 //Find the attachment point (egress interface) of the next hop
435 Interface egressInterface = null;
Jonathan Hart309889c2013-08-13 23:26:24 +1200436 if (bgpPeers.containsKey(dstIpAddress)) {
437 //Route to a peer
438 log.debug("Route to peer {}", dstIpAddress);
439 BgpPeer peer = bgpPeers.get(dstIpAddress);
440 egressInterface = interfaces.get(peer.getInterfaceName());
Jonathan Hart309889c2013-08-13 23:26:24 +1200441 }
442 else {
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200443 //Route to non-peer
Jonathan Hart309889c2013-08-13 23:26:24 +1200444 log.debug("Route to non-peer {}", dstIpAddress);
445 egressInterface = interfacePtrie.match(
446 new Prefix(dstIpAddress.getAddress(), 32));
447 if (egressInterface == null) {
448 log.warn("No outgoing interface found for {}", dstIpAddress.getHostAddress());
449 return;
450 }
Jonathan Hart309889c2013-08-13 23:26:24 +1200451 }
452
453 if (nextHopMacAddress == null) {
454 prefixesWaitingOnArp.put(dstIpAddress,
455 new RibUpdate(Operation.UPDATE, prefix, rib));
456 proxyArp.sendArpRequest(dstIpAddress, this, true);
457 return;
458 }
459 else {
460 if (!bgpPeers.containsKey(dstIpAddress)) {
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200461 //If the prefix is for a non-peer we need to ensure there's a path,
462 //and push one if there isn't.
463 Path path = pushedPaths.get(dstIpAddress);
Jonathan Hart309889c2013-08-13 23:26:24 +1200464 if (path == null) {
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200465 path = new Path(egressInterface, dstIpAddress);
Jonathan Hart08ee8522013-09-22 17:34:43 +1200466 calculateAndPushPath(path, MACAddress.valueOf(nextHopMacAddress));
Jonathan Hart309889c2013-08-13 23:26:24 +1200467 pushedPaths.put(dstIpAddress, path);
468 }
Jonathan Hart309889c2013-08-13 23:26:24 +1200469
470 path.incrementUsers();
471 prefixToPath.put(prefix, path);
472 }
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200473
474 //For all prefixes we need to add the first-hop mac-rewriting flows
Jonathan Hart309889c2013-08-13 23:26:24 +1200475 addPrefixFlows(prefix, egressInterface, nextHopMacAddress);
476 }
477 }
478
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200479 private void addPrefixFlows(Prefix prefix, Interface egressInterface, byte[] nextHopMacAddress) {
480 log.debug("Adding flows for prefix {} added, next hop mac {}",
481 prefix, HexString.toHexString(nextHopMacAddress));
Jonathan Hartd5f2e952013-09-13 16:31:23 +1200482
483 //We only need one flow mod per switch, so pick one interface on each switch
484 Map<Long, Interface> srcInterfaces = new HashMap<Long, Interface>();
485 for (Interface intf : interfaces.values()) {
486 if (!srcInterfaces.containsKey(intf.getDpid())
487 && intf != egressInterface) {
488 srcInterfaces.put(intf.getDpid(), intf);
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200489 }
Jonathan Hartd5f2e952013-09-13 16:31:23 +1200490 }
491
492 //Add a flow to rewrite mac for this prefix to all other border switches
Jonathan Hartd5f2e952013-09-13 16:31:23 +1200493 for (Interface srcInterface : srcInterfaces.values()) {
Jonathan Hartdefa44d2013-08-15 19:51:13 +1200494 DataPath shortestPath;
495 if (topoRouteTopology == null) {
496 shortestPath = topoRouteService.getShortestPath(
497 srcInterface.getSwitchPort(),
498 egressInterface.getSwitchPort());
499 }
500 else {
501 shortestPath = topoRouteService.getTopoShortestPath(
502 topoRouteTopology, srcInterface.getSwitchPort(),
503 egressInterface.getSwitchPort());
504 }
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200505
506 if (shortestPath == null){
507 log.debug("Shortest path between {} and {} not found",
508 srcInterface.getSwitchPort(),
509 egressInterface.getSwitchPort());
510 return; // just quit here?
511 }
512
513 //Set up the flow mod
514 OFFlowMod fm = (OFFlowMod) floodlightProvider.getOFMessageFactory()
515 .getMessage(OFType.FLOW_MOD);
516
517 fm.setIdleTimeout((short)0)
518 .setHardTimeout((short)0)
519 .setBufferId(OFPacketOut.BUFFER_ID_NONE)
520 .setCookie(MAC_RW_COOKIE)
521 .setCommand(OFFlowMod.OFPFC_ADD)
522 .setPriority(SDNIP_PRIORITY)
523 .setLengthU(OFFlowMod.MINIMUM_LENGTH
524 + OFActionDataLayerDestination.MINIMUM_LENGTH
525 + OFActionOutput.MINIMUM_LENGTH);
526
527 OFMatch match = new OFMatch();
528 match.setDataLayerType(Ethernet.TYPE_IPv4);
529 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE);
530
531 match.setFromCIDR(prefix.toString(), OFMatch.STR_NW_DST);
532 fm.setMatch(match);
533
534 //Set up MAC rewrite action
535 OFActionDataLayerDestination macRewriteAction = new OFActionDataLayerDestination();
536 macRewriteAction.setDataLayerAddress(nextHopMacAddress);
537
538 //Set up output action
539 OFActionOutput outputAction = new OFActionOutput();
540 outputAction.setMaxLength((short)0xffff);
541 Port outputPort = shortestPath.flowEntries().get(0).outPort();
542 outputAction.setPort(outputPort.value());
543
544 List<OFAction> actions = new ArrayList<OFAction>();
545 actions.add(macRewriteAction);
546 actions.add(outputAction);
547 fm.setActions(actions);
548
549 //Write to switch
550 IOFSwitch sw = floodlightProvider.getSwitches()
551 .get(srcInterface.getDpid());
552
553 if (sw == null){
554 log.warn("Switch not found when pushing flow mod");
555 continue;
556 }
557
558 pushedFlows.put(prefix, new PushedFlowMod(sw.getId(), fm));
559
560 List<OFMessage> msglist = new ArrayList<OFMessage>();
561 msglist.add(fm);
562 try {
563 sw.write(msglist, null);
564 sw.flush();
Jonathan Hart08ee8522013-09-22 17:34:43 +1200565
566 /*
567 * XXX Rate limit hack!
568 * This should be solved properly by adding a rate limiting
569 * layer on top of the switches if we know they need it.
570 */
Jonathan Hart9971e1c2013-09-17 10:47:40 +1200571 Thread.sleep(1);
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200572 } catch (IOException e) {
573 log.error("Failure writing flow mod", e);
Jonathan Hart65139e42013-09-13 16:52:25 +1200574 } catch (InterruptedException e) {
575 // TODO handle this properly
576 log.error("Interrupted", e);
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200577 }
Jonathan Hart309889c2013-08-13 23:26:24 +1200578 }
579 }
Jonathan Hart309889c2013-08-13 23:26:24 +1200580
Jonathan Hart0a46fe42013-08-10 17:08:47 +1200581 public synchronized void processRibDelete(RibUpdate update) {
Jonathan Hart8b9349e2013-07-26 15:55:28 +1200582 Prefix prefix = update.getPrefix();
583
Jonathan Hartd7e158d2013-08-07 23:04:48 +1200584 if (ptree.remove(prefix, update.getRibEntry())) {
585 /*
586 * Only delete flows if an entry was actually removed from the trie.
587 * If no entry was removed, the <prefix, nexthop> wasn't there so
588 * it's probably already been removed and we don't need to do anything
589 */
Jonathan Hart309889c2013-08-13 23:26:24 +1200590 _processDeletePrefix(prefix, update.getRibEntry());
Jonathan Hart309889c2013-08-13 23:26:24 +1200591 }
592 }
593
594 private void _processDeletePrefix(Prefix prefix, RibEntry ribEntry) {
595 deletePrefixFlows(prefix);
596
597 log.debug("Deleting {} to {}", prefix, ribEntry.getNextHop());
Jonathan Hartd992f1a2013-09-13 13:46:44 +1200598
Jonathan Hart309889c2013-08-13 23:26:24 +1200599 if (!bgpPeers.containsKey(ribEntry.getNextHop())) {
600 log.debug("Getting path for route with non-peer nexthop");
Jonathan Hartfb1ebc52013-08-17 16:25:51 +1200601 Path path = prefixToPath.remove(prefix);
Jonathan Hart309889c2013-08-13 23:26:24 +1200602
Jonathan Hartd992f1a2013-09-13 13:46:44 +1200603 if (path != null) {
604 //path could be null if we added to the Ptree but didn't push
605 //flows yet because we were waiting to resolve ARP
Jonathan Hart309889c2013-08-13 23:26:24 +1200606
Jonathan Hartd992f1a2013-09-13 13:46:44 +1200607 path.decrementUsers();
Jonathan Hartd992f1a2013-09-13 13:46:44 +1200608 if (path.getUsers() <= 0 && !path.isPermanent()) {
609 deletePath(path);
610 pushedPaths.remove(path.getDstIpAddress());
611 }
Jonathan Hart309889c2013-08-13 23:26:24 +1200612 }
613 }
614 }
615
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200616 private void deletePrefixFlows(Prefix prefix) {
617 log.debug("Deleting flows for prefix {}", prefix);
618
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200619 Collection<PushedFlowMod> pushedFlowMods
620 = pushedFlows.removeAll(prefix);
621
622 for (PushedFlowMod pfm : pushedFlowMods) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200623 if (log.isTraceEnabled()) {
624 log.trace("Pushing a DELETE flow mod to {}, matches prefix {} with mac-rewrite {}",
625 new Object[] {HexString.toHexString(pfm.getDpid()),
626 pfm.getFlowMod().getMatch().getNetworkDestination() +
627 pfm.getFlowMod().getMatch().getNetworkDestinationMaskLen(),
628 HexString.toHexString(((OFActionDataLayerDestination)pfm.getFlowMod().getActions().get(0))
629 .getDataLayerAddress())});
630 }
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200631
632 sendDeleteFlowMod(pfm.getFlowMod(), pfm.getDpid());
633 }
634 }
635
636 private void deletePath(Path path) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200637 log.debug("Deleting flows for path to {}",
638 path.getDstIpAddress().getHostAddress());
639
Jonathan Hart309889c2013-08-13 23:26:24 +1200640 for (PushedFlowMod pfm : path.getFlowMods()) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200641 if (log.isTraceEnabled()) {
642 log.trace("Pushing a DELETE flow mod to {}, dst MAC {}",
643 new Object[] {HexString.toHexString(pfm.getDpid()),
644 HexString.toHexString(pfm.getFlowMod().getMatch().getDataLayerDestination())
645 });
646 }
Jonathan Hart309889c2013-08-13 23:26:24 +1200647
648 sendDeleteFlowMod(pfm.getFlowMod(), pfm.getDpid());
649 }
650 }
651
652 private void sendDeleteFlowMod(OFFlowMod addFlowMod, long dpid) {
653 addFlowMod.setCommand(OFFlowMod.OFPFC_DELETE_STRICT)
654 .setOutPort(OFPort.OFPP_NONE)
655 .setLengthU(OFFlowMod.MINIMUM_LENGTH);
656
657 addFlowMod.getActions().clear();
658
659 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
660 if (sw == null) {
661 log.warn("Switch not found when pushing delete flow mod");
662 return;
663 }
664
665 try {
666 sw.write(addFlowMod, null);
667 sw.flush();
668 } catch (IOException e) {
669 log.error("Failure writing flow mod", e);
Jonathan Hartd7e158d2013-08-07 23:04:48 +1200670 }
Jonathan Hart8b9349e2013-07-26 15:55:28 +1200671 }
672
Jonathan Hart0ee0f022013-08-03 22:21:54 +1200673 //TODO test next-hop changes
Jonathan Hart2f740782013-08-04 00:49:21 +1200674 //TODO check delete/add synchronization
Jonathan Hart0ee0f022013-08-03 22:21:54 +1200675
Jonathan Harte7e1c6e2013-06-04 20:50:23 -0700676 /*
677 * On startup we need to calculate a full mesh of paths between all gateway
678 * switches
679 */
Jonathan Hart1236a9b2013-06-18 22:10:05 +1200680 private void setupFullMesh(){
Jonathan Harte7e1c6e2013-06-04 20:50:23 -0700681 //For each border router, calculate and install a path from every other
682 //border switch to said border router. However, don't install the entry
683 //in to the first hop switch, as we need to install an entry to rewrite
684 //for each prefix received. This will be done later when prefixes have
685 //actually been received.
686
Jonathan Hartc824ad02013-07-03 15:58:45 +1200687 for (BgpPeer peer : bgpPeers.values()) {
688 Interface peerInterface = interfaces.get(peer.getInterfaceName());
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200689
Jonathan Hart309889c2013-08-13 23:26:24 +1200690 //We know there's not already a Path here pushed, because this is
691 //called before all other routing
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200692 Path path = new Path(peerInterface, peer.getIpAddress());
Jonathan Hart309889c2013-08-13 23:26:24 +1200693 path.setPermanent();
694
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200695 //See if we know the MAC address of the peer. If not we can't
696 //do anything until we learn it
697 byte[] mac = proxyArp.getMacAddress(peer.getIpAddress());
698 if (mac == null) {
699 log.debug("Don't know MAC for {}", peer.getIpAddress().getHostAddress());
700 //Put in the pending paths list first
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200701 pathsWaitingOnArp.put(peer.getIpAddress(), path);
Jonathan Harte7e1c6e2013-06-04 20:50:23 -0700702
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200703 proxyArp.sendArpRequest(peer.getIpAddress(), this, true);
704 continue;
Jonathan Harte7e1c6e2013-06-04 20:50:23 -0700705 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200706
707 //If we know the MAC, lets go ahead and push the paths to this peer
Jonathan Hart08ee8522013-09-22 17:34:43 +1200708 calculateAndPushPath(path, MACAddress.valueOf(mac));
Jonathan Harte7e1c6e2013-06-04 20:50:23 -0700709 }
710 }
711
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200712 private void calculateAndPushPath(Path path, MACAddress dstMacAddress) {
Jonathan Hart309889c2013-08-13 23:26:24 +1200713 Interface dstInterface = path.getDstInterface();
714
Jonathan Hartfb1ebc52013-08-17 16:25:51 +1200715 log.debug("Setting up path to {}, {}", path.getDstIpAddress().getHostAddress(),
716 dstMacAddress);
717
Jonathan Hart309889c2013-08-13 23:26:24 +1200718 List<PushedFlowMod> pushedFlows = new ArrayList<PushedFlowMod>();
719
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200720 for (Interface srcInterface : interfaces.values()) {
721 if (dstInterface.equals(srcInterface.getName())){
722 continue;
723 }
724
Jonathan Hartdefa44d2013-08-15 19:51:13 +1200725 DataPath shortestPath;
726 if (topoRouteTopology == null) {
Jonathan Hartdefa44d2013-08-15 19:51:13 +1200727 shortestPath = topoRouteService.getShortestPath(
728 srcInterface.getSwitchPort(), dstInterface.getSwitchPort());
729 }
730 else {
Jonathan Hartdefa44d2013-08-15 19:51:13 +1200731 shortestPath = topoRouteService.getTopoShortestPath(topoRouteTopology,
732 srcInterface.getSwitchPort(), dstInterface.getSwitchPort());
733 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200734
735 if (shortestPath == null){
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200736 log.warn("Shortest path between {} and {} not found",
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200737 srcInterface.getSwitchPort(), dstInterface.getSwitchPort());
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200738 return;
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200739 }
740
Jonathan Hart309889c2013-08-13 23:26:24 +1200741 pushedFlows.addAll(installPath(shortestPath.flowEntries(), dstMacAddress));
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200742 }
Jonathan Hart309889c2013-08-13 23:26:24 +1200743
744 path.setFlowMods(pushedFlows);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200745 }
746
Jonathan Hart309889c2013-08-13 23:26:24 +1200747 private List<PushedFlowMod> installPath(List<FlowEntry> flowEntries, MACAddress dstMacAddress){
748 List<PushedFlowMod> flowMods = new ArrayList<PushedFlowMod>();
749
Jonathan Harte7e1c6e2013-06-04 20:50:23 -0700750 //Set up the flow mod
751 OFFlowMod fm =
752 (OFFlowMod) floodlightProvider.getOFMessageFactory()
753 .getMessage(OFType.FLOW_MOD);
754
755 OFActionOutput action = new OFActionOutput();
756 action.setMaxLength((short)0xffff);
757 List<OFAction> actions = new ArrayList<OFAction>();
758 actions.add(action);
759
760 fm.setIdleTimeout((short)0)
761 .setHardTimeout((short)0)
762 .setBufferId(OFPacketOut.BUFFER_ID_NONE)
763 .setCookie(L2_FWD_COOKIE)
764 .setCommand(OFFlowMod.OFPFC_ADD)
Jonathan Harte7e1c6e2013-06-04 20:50:23 -0700765 .setActions(actions)
766 .setLengthU(OFFlowMod.MINIMUM_LENGTH+OFActionOutput.MINIMUM_LENGTH);
767
768 //Don't push the first flow entry. We need to push entries in the
769 //first switch based on IP prefix which we don't know yet.
770 for (int i = 1; i < flowEntries.size(); i++){
771 FlowEntry flowEntry = flowEntries.get(i);
772
773 OFMatch match = new OFMatch();
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200774 match.setDataLayerDestination(dstMacAddress.toBytes());
Jonathan Harte7e1c6e2013-06-04 20:50:23 -0700775 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_DST);
776 ((OFActionOutput) fm.getActions().get(0)).setPort(flowEntry.outPort().value());
777
778 fm.setMatch(match);
779
780 IOFSwitch sw = floodlightProvider.getSwitches().get(flowEntry.dpid().value());
781
782 if (sw == null){
783 log.warn("Switch not found when pushing flow mod");
784 continue;
785 }
786
Jonathan Hart309889c2013-08-13 23:26:24 +1200787 flowMods.add(new PushedFlowMod(sw.getId(), fm));
788
Jonathan Harte7e1c6e2013-06-04 20:50:23 -0700789 List<OFMessage> msglist = new ArrayList<OFMessage>();
790 msglist.add(fm);
791 try {
792 sw.write(msglist, null);
793 sw.flush();
794 } catch (IOException e) {
795 log.error("Failure writing flow mod", e);
796 }
797
798 try {
799 fm = fm.clone();
800 } catch (CloneNotSupportedException e1) {
801 log.error("Failure cloning flow mod", e1);
802 }
803 }
Jonathan Hart309889c2013-08-13 23:26:24 +1200804
805 return flowMods;
Jonathan Harte7e1c6e2013-06-04 20:50:23 -0700806 }
807
Jonathan Hart832a7cb2013-06-24 11:25:35 +1200808 private void setupBgpPaths(){
Jonathan Hartc824ad02013-07-03 15:58:45 +1200809 for (BgpPeer bgpPeer : bgpPeers.values()){
Jonathan Hart832a7cb2013-06-24 11:25:35 +1200810 Interface peerInterface = interfaces.get(bgpPeer.getInterfaceName());
811
812 DataPath path = topoRouteService.getShortestPath(
813 peerInterface.getSwitchPort(), bgpdAttachmentPoint);
814
815 if (path == null){
816 log.debug("Unable to compute path for BGP traffic for {}",
817 bgpPeer.getIpAddress());
818 continue;
819 }
820
821 //Set up the flow mod
822 OFFlowMod fm =
823 (OFFlowMod) floodlightProvider.getOFMessageFactory()
824 .getMessage(OFType.FLOW_MOD);
825
826 OFActionOutput action = new OFActionOutput();
827 action.setMaxLength((short)0xffff);
828 List<OFAction> actions = new ArrayList<OFAction>();
829 actions.add(action);
830
831 fm.setIdleTimeout((short)0)
832 .setHardTimeout((short)0)
833 .setBufferId(OFPacketOut.BUFFER_ID_NONE)
834 .setCookie(BGP_COOKIE)
835 .setCommand(OFFlowMod.OFPFC_ADD)
836 .setPriority(SDNIP_PRIORITY)
837 .setActions(actions)
838 .setLengthU(OFFlowMod.MINIMUM_LENGTH+OFActionOutput.MINIMUM_LENGTH);
839
840 //Forward = gateway -> bgpd, reverse = bgpd -> gateway
841 OFMatch forwardMatchSrc = new OFMatch();
842
Jonathan Hart832a7cb2013-06-24 11:25:35 +1200843 String interfaceCidrAddress = peerInterface.getIpAddress().getHostAddress()
844 + "/32";
845 String peerCidrAddress = bgpPeer.getIpAddress().getHostAddress()
846 + "/32";
Jonathan Hart38c84932013-08-10 17:49:27 +1200847
Jonathan Hart832a7cb2013-06-24 11:25:35 +1200848 //Common match fields
849 forwardMatchSrc.setDataLayerType(Ethernet.TYPE_IPv4);
Jonathan Hart832a7cb2013-06-24 11:25:35 +1200850 forwardMatchSrc.setNetworkProtocol(IPv4.PROTOCOL_TCP);
851 forwardMatchSrc.setTransportDestination(BGP_PORT);
852 forwardMatchSrc.setWildcards(forwardMatchSrc.getWildcards() & ~OFMatch.OFPFW_IN_PORT
853 & ~OFMatch.OFPFW_DL_TYPE & ~OFMatch.OFPFW_NW_PROTO);
854
855
856 OFMatch reverseMatchSrc = forwardMatchSrc.clone();
857
858 forwardMatchSrc.setFromCIDR(peerCidrAddress, OFMatch.STR_NW_SRC);
859 forwardMatchSrc.setFromCIDR(interfaceCidrAddress, OFMatch.STR_NW_DST);
860
861 OFMatch forwardMatchDst = forwardMatchSrc.clone();
862
863 forwardMatchSrc.setTransportSource(BGP_PORT);
864 forwardMatchSrc.setWildcards(forwardMatchSrc.getWildcards() & ~OFMatch.OFPFW_TP_SRC);
865 forwardMatchDst.setTransportDestination(BGP_PORT);
866 forwardMatchDst.setWildcards(forwardMatchDst.getWildcards() & ~OFMatch.OFPFW_TP_DST);
867
868 reverseMatchSrc.setFromCIDR(interfaceCidrAddress, OFMatch.STR_NW_SRC);
869 reverseMatchSrc.setFromCIDR(peerCidrAddress, OFMatch.STR_NW_DST);
870
871 OFMatch reverseMatchDst = reverseMatchSrc.clone();
872
873 reverseMatchSrc.setTransportSource(BGP_PORT);
874 reverseMatchSrc.setWildcards(forwardMatchSrc.getWildcards() & ~OFMatch.OFPFW_TP_SRC);
875 reverseMatchDst.setTransportDestination(BGP_PORT);
876 reverseMatchDst.setWildcards(forwardMatchDst.getWildcards() & ~OFMatch.OFPFW_TP_DST);
877
878 fm.setMatch(forwardMatchSrc);
879
Jonathan Hart38c84932013-08-10 17:49:27 +1200880 OFMatch forwardIcmpMatch = new OFMatch();
881 forwardIcmpMatch.setDataLayerType(Ethernet.TYPE_IPv4);
882 forwardIcmpMatch.setNetworkProtocol(IPv4.PROTOCOL_ICMP);
883 forwardIcmpMatch.setWildcards(forwardIcmpMatch.getWildcards() &
884 ~OFMatch.OFPFW_DL_TYPE & ~OFMatch.OFPFW_NW_PROTO);
885
886 OFMatch reverseIcmpMatch = forwardIcmpMatch.clone();
887 forwardIcmpMatch.setFromCIDR(interfaceCidrAddress, OFMatch.STR_NW_DST);
888 reverseIcmpMatch.setFromCIDR(interfaceCidrAddress, OFMatch.STR_NW_SRC);
889
Jonathan Hart832a7cb2013-06-24 11:25:35 +1200890 for (FlowEntry flowEntry : path.flowEntries()){
891 OFFlowMod forwardFlowModSrc, forwardFlowModDst;
892 OFFlowMod reverseFlowModSrc, reverseFlowModDst;
Jonathan Hart38c84932013-08-10 17:49:27 +1200893 OFFlowMod forwardIcmp, reverseIcmp;
Jonathan Hart832a7cb2013-06-24 11:25:35 +1200894 try {
895 forwardFlowModSrc = fm.clone();
896 forwardFlowModDst = fm.clone();
897 reverseFlowModSrc = fm.clone();
898 reverseFlowModDst = fm.clone();
Jonathan Hart38c84932013-08-10 17:49:27 +1200899 forwardIcmp = fm.clone();
900 reverseIcmp = fm.clone();
Jonathan Hart832a7cb2013-06-24 11:25:35 +1200901 } catch (CloneNotSupportedException e) {
902 log.warn("Clone failed", e);
903 continue;
904 }
905
906 forwardMatchSrc.setInputPort(flowEntry.inPort().value());
907 forwardFlowModSrc.setMatch(forwardMatchSrc);
908 ((OFActionOutput)forwardFlowModSrc.getActions().get(0))
909 .setPort(flowEntry.outPort().value());
910
911 forwardMatchDst.setInputPort(flowEntry.inPort().value());
912 forwardFlowModDst.setMatch(forwardMatchDst);
913 ((OFActionOutput)forwardFlowModDst.getActions().get(0))
914 .setPort(flowEntry.outPort().value());
915
916 reverseMatchSrc.setInputPort(flowEntry.outPort().value());
917 reverseFlowModSrc.setMatch(reverseMatchSrc);
918 ((OFActionOutput)reverseFlowModSrc.getActions().get(0))
919 .setPort(flowEntry.inPort().value());
920
921 reverseMatchDst.setInputPort(flowEntry.outPort().value());
922 reverseFlowModDst.setMatch(reverseMatchDst);
923 ((OFActionOutput)reverseFlowModDst.getActions().get(0))
924 .setPort(flowEntry.inPort().value());
925
Jonathan Hart38c84932013-08-10 17:49:27 +1200926 ((OFActionOutput)forwardIcmp.getActions().get(0))
927 .setPort(flowEntry.outPort().value());
928 forwardIcmp.setMatch(forwardIcmpMatch);
929
930 ((OFActionOutput)reverseIcmp.getActions().get(0))
931 .setPort(flowEntry.inPort().value());
932 reverseIcmp.setMatch(reverseIcmpMatch);
933
934
Jonathan Hart832a7cb2013-06-24 11:25:35 +1200935 IOFSwitch sw = floodlightProvider.getSwitches().get(flowEntry.dpid().value());
936
Jonathan Hart38c84932013-08-10 17:49:27 +1200937 if (sw == null) {
938 log.warn("Switch not found when pushing BGP paths");
939 return;
940 }
941
Jonathan Hart832a7cb2013-06-24 11:25:35 +1200942 List<OFMessage> msgList = new ArrayList<OFMessage>(2);
943 msgList.add(forwardFlowModSrc);
944 msgList.add(forwardFlowModDst);
945 msgList.add(reverseFlowModSrc);
946 msgList.add(reverseFlowModDst);
Jonathan Hart38c84932013-08-10 17:49:27 +1200947 msgList.add(forwardIcmp);
948 msgList.add(reverseIcmp);
Jonathan Hart832a7cb2013-06-24 11:25:35 +1200949
950 try {
951 sw.write(msgList, null);
952 sw.flush();
953 } catch (IOException e) {
954 log.error("Failure writing flow mod", e);
955 }
956 }
957 }
958 }
Jonathan Hart1236a9b2013-06-18 22:10:05 +1200959
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200960 @Override
961 public void arpResponse(InetAddress ipAddress, byte[] macAddress) {
962 log.debug("Received ARP response: {} => {}", ipAddress.getHostAddress(),
963 MACAddress.valueOf(macAddress).toString());
964
Jonathan Hart0a46fe42013-08-10 17:08:47 +1200965 /*
966 * We synchronize on this to prevent changes to the ptree while we're pushing
967 * flows to the switches. If the ptree changes, the ptree and switches
968 * could get out of sync.
969 */
970 synchronized (this) {
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200971 Path path = pathsWaitingOnArp.remove(ipAddress);
Jonathan Hart309889c2013-08-13 23:26:24 +1200972
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200973 if (path != null) {
Jonathan Hart309889c2013-08-13 23:26:24 +1200974 log.debug("Pushing path to {} at {} on {}", new Object[] {
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200975 path.getDstIpAddress().getHostAddress(),
Jonathan Hart309889c2013-08-13 23:26:24 +1200976 MACAddress.valueOf(macAddress),
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200977 path.getDstInterface().getSwitchPort()});
978 //These paths should always be to BGP peers. Paths to non-peers are
979 //handled once the first prefix is ready to push
980 if (pushedPaths.containsKey(path.getDstInterface())) {
Jonathan Hart309889c2013-08-13 23:26:24 +1200981 //A path already got pushed to this endpoint while we were waiting
982 //for ARP. We'll copy over the permanent attribute if it is set on this path.
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200983 if (path.isPermanent()) {
984 pushedPaths.get(path.getDstInterface()).setPermanent();
Jonathan Hart309889c2013-08-13 23:26:24 +1200985 }
986 }
987 else {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200988 calculateAndPushPath(path, MACAddress.valueOf(macAddress));
Jonathan Harta23ffdb2013-08-14 14:36:54 +1200989 pushedPaths.put(path.getDstIpAddress(), path);
Jonathan Hart309889c2013-08-13 23:26:24 +1200990 }
991 }
992
Jonathan Hart309889c2013-08-13 23:26:24 +1200993 Set<RibUpdate> prefixesToPush = prefixesWaitingOnArp.removeAll(ipAddress);
994
Jonathan Hart0a46fe42013-08-10 17:08:47 +1200995 for (RibUpdate update : prefixesToPush) {
996 //These will always be adds
997
Jonathan Hartb39a67d2013-08-10 23:59:50 +1200998 RibEntry rib = ptree.lookup(update.getPrefix());
Jonathan Hart0a46fe42013-08-10 17:08:47 +1200999 if (rib != null && rib.equals(update.getRibEntry())) {
1000 log.debug("Pushing prefix {} next hop {}", update.getPrefix(),
Jonathan Hartb39a67d2013-08-10 23:59:50 +12001001 rib.getNextHop().getHostAddress());
Jonathan Hart0a46fe42013-08-10 17:08:47 +12001002 //We only push prefix flows if the prefix is still in the ptree
1003 //and the next hop is the same as our update. The prefix could
1004 //have been removed while we were waiting for the ARP, or the
1005 //next hop could have changed.
Jonathan Hart309889c2013-08-13 23:26:24 +12001006 _processRibAdd(update);
Jonathan Hart0a46fe42013-08-10 17:08:47 +12001007 } else {
1008 log.debug("Received ARP response, but {},{} is no longer in ptree",
1009 update.getPrefix(), update.getRibEntry());
1010 }
1011 }
Jonathan Hart0ee0f022013-08-03 22:21:54 +12001012 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +12001013 }
1014
Jonathan Hartc82051c2013-08-24 15:12:20 +12001015 private void setupArpFlows() {
1016 OFMatch match = new OFMatch();
1017 match.setDataLayerType(Ethernet.TYPE_ARP);
1018 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE);
1019
1020 OFFlowMod fm = new OFFlowMod();
1021 fm.setMatch(match);
1022
1023 OFActionOutput action = new OFActionOutput();
1024 action.setPort(OFPort.OFPP_CONTROLLER.getValue());
1025 action.setMaxLength((short)0xffff);
1026 List<OFAction> actions = new ArrayList<OFAction>(1);
1027 actions.add(action);
1028 fm.setActions(actions);
1029
1030 fm.setIdleTimeout((short)0)
1031 .setHardTimeout((short)0)
1032 .setBufferId(OFPacketOut.BUFFER_ID_NONE)
1033 .setCookie(0)
1034 .setCommand(OFFlowMod.OFPFC_ADD)
1035 .setPriority(ARP_PRIORITY)
1036 .setLengthU(OFFlowMod.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH);
1037
1038 for (String strdpid : switches){
1039 IOFSwitch sw = floodlightProvider.getSwitches().get(HexString.toLong(strdpid));
1040 if (sw == null) {
1041 log.debug("Couldn't find switch to push ARP flow");
1042 }
1043 else {
1044 try {
1045 sw.write(fm, null);
1046 } catch (IOException e) {
1047 log.warn("Failure writing ARP flow to switch", e);
1048 }
1049 }
1050 }
1051 }
1052
Jonathan Hartf886fa12013-09-18 14:46:29 +12001053 private void setupDefaultDropFlows() {
1054 OFFlowMod fm = new OFFlowMod();
1055 fm.setMatch(new OFMatch());
1056 //No action means drop
1057
1058 fm.setIdleTimeout((short)0)
1059 .setHardTimeout((short)0)
1060 .setBufferId(OFPacketOut.BUFFER_ID_NONE)
1061 .setCookie(0)
1062 .setCommand(OFFlowMod.OFPFC_ADD)
1063 .setPriority((short)0)
1064 .setLengthU(OFFlowMod.MINIMUM_LENGTH);
1065
1066 for (String strdpid : switches){
1067 IOFSwitch sw = floodlightProvider.getSwitches().get(HexString.toLong(strdpid));
1068 if (sw == null) {
1069 log.debug("Couldn't find switch to push default deny flow");
1070 }
1071 else {
1072 try {
1073 sw.write(fm, null);
1074 } catch (IOException e) {
1075 log.warn("Failure writing default deny flow to switch", e);
1076 }
1077 }
1078 }
1079 }
1080
Jonathan Hart1236a9b2013-06-18 22:10:05 +12001081 private void beginRouting(){
1082 log.debug("Topology is now ready, beginning routing function");
Jonathan Hartdefa44d2013-08-15 19:51:13 +12001083 topoRouteTopology = topoRouteService.prepareShortestPathTopo();
1084
Jonathan Hartc82051c2013-08-24 15:12:20 +12001085 setupArpFlows();
Jonathan Hartf886fa12013-09-18 14:46:29 +12001086 setupDefaultDropFlows();
Jonathan Hartc82051c2013-08-24 15:12:20 +12001087
Jonathan Hart832a7cb2013-06-24 11:25:35 +12001088 setupBgpPaths();
Jonathan Hart98957bf2013-07-01 14:49:24 +12001089 setupFullMesh();
Jonathan Harte7694532013-09-12 12:34:46 +12001090
1091 //Suppress link discovery on external-facing router ports
1092 for (Interface intf : interfaces.values()) {
1093 linkDiscoveryService.AddToSuppressLLDPs(intf.getDpid(), intf.getPort());
1094 }
1095
Jonathan Hart0a46fe42013-08-10 17:08:47 +12001096 bgpUpdatesExecutor.execute(new Runnable() {
1097 @Override
1098 public void run() {
1099 doUpdatesThread();
1100 }
1101 });
Jonathan Hart1236a9b2013-06-18 22:10:05 +12001102 }
1103
1104 private void checkSwitchesConnected(){
1105 for (String dpid : switches){
1106 if (floodlightProvider.getSwitches().get(HexString.toLong(dpid)) == null){
1107 log.debug("Not all switches are here yet");
1108 return;
1109 }
1110 }
1111 switchesConnected = true;
1112 }
1113
Jonathan Hartc824ad02013-07-03 15:58:45 +12001114 //Actually we only need to go half way round to verify full mesh connectivity
1115 //(n^2)/2
Jonathan Hart1236a9b2013-06-18 22:10:05 +12001116 private void checkTopologyReady(){
Jonathan Hartc824ad02013-07-03 15:58:45 +12001117 for (Interface dstInterface : interfaces.values()) {
1118 for (Interface srcInterface : interfaces.values()) {
1119 if (dstInterface == srcInterface) {
Jonathan Hart1236a9b2013-06-18 22:10:05 +12001120 continue;
1121 }
1122
Jonathan Hart1236a9b2013-06-18 22:10:05 +12001123 DataPath shortestPath = topoRouteService.getShortestPath(
Jonathan Hartc824ad02013-07-03 15:58:45 +12001124 srcInterface.getSwitchPort(), dstInterface.getSwitchPort());
Jonathan Hart1236a9b2013-06-18 22:10:05 +12001125
1126 if (shortestPath == null){
1127 log.debug("Shortest path between {} and {} not found",
Jonathan Hartc824ad02013-07-03 15:58:45 +12001128 srcInterface.getSwitchPort(), dstInterface.getSwitchPort());
Jonathan Hart1236a9b2013-06-18 22:10:05 +12001129 return;
1130 }
1131 }
1132 }
1133 topologyReady = true;
1134 }
1135
1136 private void checkStatus(){
Jonathan Hart1236a9b2013-06-18 22:10:05 +12001137 if (!switchesConnected){
1138 checkSwitchesConnected();
1139 }
1140 boolean oldTopologyReadyStatus = topologyReady;
1141 if (switchesConnected && !topologyReady){
1142 checkTopologyReady();
1143 }
1144 if (!oldTopologyReadyStatus && topologyReady){
1145 beginRouting();
1146 }
1147 }
Jonathan Harta23ffdb2013-08-14 14:36:54 +12001148
Jonathan Hart8b9349e2013-07-26 15:55:28 +12001149 private void doUpdatesThread() {
1150 boolean interrupted = false;
1151 try {
1152 while (true) {
1153 try {
1154 RibUpdate update = ribUpdates.take();
1155 switch (update.getOperation()){
1156 case UPDATE:
Jonathan Hart2f740782013-08-04 00:49:21 +12001157 processRibAdd(update);
Jonathan Hart8b9349e2013-07-26 15:55:28 +12001158 break;
1159 case DELETE:
Jonathan Hart2f740782013-08-04 00:49:21 +12001160 processRibDelete(update);
Jonathan Hart8b9349e2013-07-26 15:55:28 +12001161 break;
1162 }
1163 } catch (InterruptedException e) {
Jonathan Harta23ffdb2013-08-14 14:36:54 +12001164 log.debug("Interrupted while taking from updates queue", e);
Jonathan Hart8b9349e2013-07-26 15:55:28 +12001165 interrupted = true;
Jonathan Hart309889c2013-08-13 23:26:24 +12001166 } catch (Exception e) {
1167 log.debug("exception", e);
Jonathan Hart8b9349e2013-07-26 15:55:28 +12001168 }
1169 }
1170 } finally {
1171 if (interrupted) {
1172 Thread.currentThread().interrupt();
1173 }
1174 }
1175 }
pingping-lina2cbfad2013-03-07 08:39:21 +08001176
1177 @Override
Jonathan Hart64c0b202013-08-20 15:45:07 +12001178 public void topologyChanged() {
1179 if (topologyReady) {
1180 return;
1181 }
1182
Jonathan Hart1236a9b2013-06-18 22:10:05 +12001183 boolean refreshNeeded = false;
1184 for (LDUpdate ldu : topology.getLastLinkUpdates()){
1185 if (!ldu.getOperation().equals(ILinkDiscovery.UpdateOperation.LINK_UPDATED)){
1186 //We don't need to recalculate anything for just link updates
Jonathan Hartb39a67d2013-08-10 23:59:50 +12001187 //They happen very frequently
Jonathan Hart1236a9b2013-06-18 22:10:05 +12001188 refreshNeeded = true;
1189 }
Jonathan Hart98957bf2013-07-01 14:49:24 +12001190
Jonathan Hart1236a9b2013-06-18 22:10:05 +12001191 log.debug("Topo change {}", ldu.getOperation());
Jonathan Hartc824ad02013-07-03 15:58:45 +12001192
Jonathan Hart98957bf2013-07-01 14:49:24 +12001193 if (ldu.getOperation().equals(ILinkDiscovery.UpdateOperation.LINK_ADDED)){
1194 synchronized (linkUpdates) {
1195 linkUpdates.add(ldu);
1196 }
1197 }
Jonathan Hart1236a9b2013-06-18 22:10:05 +12001198 }
1199
Jonathan Hart64c0b202013-08-20 15:45:07 +12001200 if (refreshNeeded && !topologyReady){
Jonathan Hart98957bf2013-07-01 14:49:24 +12001201 topologyChangeDetectorTask.reschedule(TOPO_DETECTION_WAIT, TimeUnit.SECONDS);
pingping-lina2cbfad2013-03-07 08:39:21 +08001202 }
Jonathan Hart1236a9b2013-06-18 22:10:05 +12001203 }
Jonathan Hart64c0b202013-08-20 15:45:07 +12001204
1205 @Override
1206 public void addedSwitch(IOFSwitch sw) {
1207 if (!topologyReady) {
1208 sw.clearAllFlowMods();
1209 }
1210 }
1211
1212 @Override
Jonathan Hart08ee8522013-09-22 17:34:43 +12001213 public void removedSwitch(IOFSwitch sw) {}
Jonathan Hart64c0b202013-08-20 15:45:07 +12001214
1215 @Override
Jonathan Hart08ee8522013-09-22 17:34:43 +12001216 public void switchPortChanged(Long switchId) {}
Jonathan Hart64c0b202013-08-20 15:45:07 +12001217
1218 @Override
1219 public String getName() {
Jonathan Hart08ee8522013-09-22 17:34:43 +12001220 return "BgpRoute";
1221 }
1222
1223 /*
1224 * ILayer3InfoService methods
1225 */
1226
1227 @Override
1228 public boolean isInterfaceAddress(InetAddress address) {
1229 Interface intf = interfacePtrie.match(new Prefix(address.getAddress(), 32));
1230 return (intf != null && intf.getIpAddress().equals(address));
1231 }
1232
1233 @Override
1234 public boolean inConnectedNetwork(InetAddress address) {
1235 Interface intf = interfacePtrie.match(new Prefix(address.getAddress(), 32));
1236 return (intf != null && !intf.getIpAddress().equals(address));
1237 }
1238
1239 @Override
1240 public boolean fromExternalNetwork(long inDpid, short inPort) {
1241 for (Interface intf : interfaces.values()) {
1242 if (intf.getDpid() == inDpid && intf.getPort() == inPort) {
1243 return true;
1244 }
1245 }
1246 return false;
1247 }
1248
1249 @Override
1250 public Interface getOutgoingInterface(InetAddress dstIpAddress) {
1251 return interfacePtrie.match(new Prefix(dstIpAddress.getAddress(), 32));
1252 }
1253
1254 @Override
1255 public boolean hasLayer3Configuration() {
1256 return !interfaces.isEmpty();
1257 }
1258
1259 @Override
1260 public MACAddress getRouterMacAddress() {
1261 return bgpdMacAddress;
Jonathan Hart64c0b202013-08-20 15:45:07 +12001262 }
pingping-lina2cbfad2013-03-07 08:39:21 +08001263}