Jonathan Hart | 8f6dc09 | 2014-04-18 15:56:43 -0700 | [diff] [blame] | 1 | package net.onrc.onos.apps.sdnip; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 2 | |
Jonathan Hart | d1f2325 | 2013-06-13 15:17:05 +1200 | [diff] [blame] | 3 | import java.io.File; |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 4 | import java.io.IOException; |
| 5 | import java.net.InetAddress; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 6 | import java.util.ArrayList; |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 7 | import java.util.Collection; |
pingping-lin | 0426dee | 2014-08-27 15:03:17 -0700 | [diff] [blame] | 8 | import java.util.Collections; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 9 | import java.util.HashMap; |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 10 | import java.util.HashSet; |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 11 | import java.util.Iterator; |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 12 | import java.util.List; |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 13 | import java.util.Map; |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 14 | import java.util.Set; |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 15 | import java.util.concurrent.BlockingQueue; |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 16 | import java.util.concurrent.ConcurrentHashMap; |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 17 | import java.util.concurrent.ExecutorService; |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 18 | import java.util.concurrent.Executors; |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 19 | import java.util.concurrent.LinkedBlockingQueue; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 20 | |
| 21 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 22 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 23 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 24 | import net.floodlightcontroller.core.module.IFloodlightService; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 25 | import net.floodlightcontroller.restserver.IRestApiService; |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 26 | import net.floodlightcontroller.util.MACAddress; |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 27 | import net.onrc.onos.api.newintent.IntentService; |
| 28 | import net.onrc.onos.api.newintent.MultiPointToSinglePointIntent; |
pingping-lin | aea385b | 2014-09-04 18:15:19 -0700 | [diff] [blame] | 29 | import net.onrc.onos.api.newintent.PointToPointIntent; |
Jonathan Hart | 0961fe8 | 2014-04-03 09:56:25 -0700 | [diff] [blame] | 30 | import net.onrc.onos.apps.proxyarp.IArpRequester; |
| 31 | import net.onrc.onos.apps.proxyarp.IProxyArpService; |
Jonathan Hart | 8f6dc09 | 2014-04-18 15:56:43 -0700 | [diff] [blame] | 32 | import net.onrc.onos.apps.sdnip.RibUpdate.Operation; |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 33 | import net.onrc.onos.apps.sdnip.web.SdnIpWebRoutable; |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 34 | import net.onrc.onos.apps.sdnip.web.SdnIpWebRoutableNew; |
| 35 | import net.onrc.onos.core.intent.IntentOperation; |
| 36 | import net.onrc.onos.core.intent.IntentOperationList; |
| 37 | import net.onrc.onos.core.intent.ShortestPathIntent; |
| 38 | import net.onrc.onos.core.intent.runtime.IPathCalcRuntimeService; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 39 | import net.onrc.onos.core.linkdiscovery.ILinkDiscoveryService; |
Jonathan Hart | 51f6f5b | 2014-04-03 10:32:10 -0700 | [diff] [blame] | 40 | import net.onrc.onos.core.main.config.IConfigInfoService; |
pingping-lin | aea385b | 2014-09-04 18:15:19 -0700 | [diff] [blame] | 41 | import net.onrc.onos.core.matchaction.action.Actions; |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 42 | import net.onrc.onos.core.matchaction.action.ModifyDstMacAction; |
| 43 | import net.onrc.onos.core.matchaction.match.PacketMatch; |
| 44 | import net.onrc.onos.core.matchaction.match.PacketMatchBuilder; |
| 45 | import net.onrc.onos.core.newintent.IdBlockAllocatorBasedIntentIdGenerator; |
pingping-lin | aea385b | 2014-09-04 18:15:19 -0700 | [diff] [blame] | 46 | import net.onrc.onos.core.packet.Ethernet; |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 47 | import net.onrc.onos.core.registry.IControllerRegistryService; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 48 | import net.onrc.onos.core.util.Dpid; |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 49 | import net.onrc.onos.core.util.IPv4; |
Yuta HIGUCHI | fb56450 | 2014-06-16 21:29:00 -0700 | [diff] [blame] | 50 | import net.onrc.onos.core.util.PortNumber; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 51 | import net.onrc.onos.core.util.SwitchPort; |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 52 | import net.sf.json.JSONArray; |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 53 | import net.sf.json.JSONException; |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 54 | import net.sf.json.JSONObject; |
| 55 | import net.sf.json.JSONSerializer; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 56 | |
Jonathan Hart | ec9ee2e | 2014-04-08 22:45:44 -0700 | [diff] [blame] | 57 | import org.apache.commons.configuration.ConfigurationRuntimeException; |
Jonathan Hart | d1f2325 | 2013-06-13 15:17:05 +1200 | [diff] [blame] | 58 | import org.codehaus.jackson.JsonParseException; |
| 59 | import org.codehaus.jackson.map.JsonMappingException; |
| 60 | import org.codehaus.jackson.map.ObjectMapper; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 61 | import org.slf4j.Logger; |
| 62 | import org.slf4j.LoggerFactory; |
| 63 | |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 64 | import com.google.common.collect.HashMultimap; |
| 65 | import com.google.common.collect.Multimaps; |
| 66 | import com.google.common.collect.SetMultimap; |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 67 | import com.google.common.net.InetAddresses; |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 68 | import com.google.common.util.concurrent.ThreadFactoryBuilder; |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 69 | import com.googlecode.concurrenttrees.radix.RadixTree; |
| 70 | import com.googlecode.concurrenttrees.radix.node.concrete.DefaultByteArrayNodeFactory; |
| 71 | import com.googlecode.concurrenttrees.radixinverted.ConcurrentInvertedRadixTree; |
| 72 | import com.googlecode.concurrenttrees.radixinverted.InvertedRadixTree; |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 73 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 74 | /** |
| 75 | * This class sets up BGP paths, handles RIB updates and relative intents. |
| 76 | * TODO: Thread-safe. |
| 77 | */ |
Jonathan Hart | 8f6dc09 | 2014-04-18 15:56:43 -0700 | [diff] [blame] | 78 | public class SdnIp implements IFloodlightModule, ISdnIpService, |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 79 | IArpRequester, IConfigInfoService { |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 80 | |
Jonathan Hart | 8f6dc09 | 2014-04-18 15:56:43 -0700 | [diff] [blame] | 81 | private static final Logger log = LoggerFactory.getLogger(SdnIp.class); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 82 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 83 | private ILinkDiscoveryService linkDiscoveryService; |
| 84 | private IRestApiService restApi; |
| 85 | private IProxyArpService proxyArp; |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 86 | |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 87 | private InvertedRadixTree<RibEntry> bgpRoutes; |
| 88 | private InvertedRadixTree<Interface> interfaceRoutes; |
| 89 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 90 | private BlockingQueue<RibUpdate> ribUpdates; |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 91 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 92 | private String bgpdRestIp; |
| 93 | private String routerId; |
Ray Milkey | 5df613b | 2014-04-15 10:50:56 -0700 | [diff] [blame] | 94 | private static final String DEFAULT_CONFIG_FILENAME = "config.json"; |
| 95 | private String currentConfigFilename = DEFAULT_CONFIG_FILENAME; |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 96 | |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 97 | /* ShortestPath Intent Variables */ |
Sho SHIMIZU | 9ec3982 | 2014-07-10 17:15:12 -0700 | [diff] [blame] | 98 | private final String caller = "SdnIp"; |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 99 | private IControllerRegistryService controllerRegistryService; |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 100 | private IntentService intentService; |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 101 | private IPathCalcRuntimeService pathRuntime; |
| 102 | /* Shortest Intent Path Variables */ |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 103 | private IdBlockAllocatorBasedIntentIdGenerator intentIdGenerator; |
| 104 | //private static final short ARP_PRIORITY = 20; |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 105 | |
pingping-lin | aea385b | 2014-09-04 18:15:19 -0700 | [diff] [blame] | 106 | private static final short BGP_PORT = 179; |
| 107 | public static final byte PROTOCOL_ICMP = 0x1; |
| 108 | public static final byte PROTOCOL_TCP = 0x6; |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 109 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 110 | // Configuration stuff |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 111 | private List<String> switches; |
| 112 | private Map<String, Interface> interfaces; |
| 113 | private Map<InetAddress, BgpPeer> bgpPeers; |
| 114 | private SwitchPort bgpdAttachmentPoint; |
| 115 | private MACAddress bgpdMacAddress; |
| 116 | private short vlan; |
pingping-lin | 0426dee | 2014-08-27 15:03:17 -0700 | [diff] [blame] | 117 | private Set<SwitchPort> externalNetworkSwitchPorts; |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 118 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 119 | private SetMultimap<InetAddress, RibUpdate> prefixesWaitingOnArp; |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 120 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 121 | private ExecutorService bgpUpdatesExecutor; |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 122 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 123 | private ConcurrentHashMap<Prefix, MultiPointToSinglePointIntent> pushedRouteIntents; |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 124 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 125 | /** |
| 126 | * SDN-IP application has a configuration file. This method is to read all |
| 127 | * the info from this file. |
| 128 | * |
| 129 | * @param configFilename the name of configuration file for SDN-IP application |
| 130 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 131 | private void readConfiguration(String configFilename) { |
| 132 | File gatewaysFile = new File(configFilename); |
| 133 | ObjectMapper mapper = new ObjectMapper(); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 134 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 135 | try { |
| 136 | Configuration config = mapper.readValue(gatewaysFile, Configuration.class); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 137 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 138 | switches = config.getSwitches(); |
Sho SHIMIZU | 84a72de | 2014-07-09 07:56:40 -0700 | [diff] [blame] | 139 | interfaces = new HashMap<>(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 140 | for (Interface intf : config.getInterfaces()) { |
| 141 | interfaces.put(intf.getName(), intf); |
pingping-lin | 0426dee | 2014-08-27 15:03:17 -0700 | [diff] [blame] | 142 | externalNetworkSwitchPorts.add(new SwitchPort(intf.getDpid(), |
| 143 | intf.getPort())); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 144 | } |
Sho SHIMIZU | 84a72de | 2014-07-09 07:56:40 -0700 | [diff] [blame] | 145 | bgpPeers = new HashMap<>(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 146 | for (BgpPeer peer : config.getPeers()) { |
| 147 | bgpPeers.put(peer.getIpAddress(), peer); |
| 148 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 149 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 150 | bgpdAttachmentPoint = new SwitchPort( |
| 151 | new Dpid(config.getBgpdAttachmentDpid()), |
Yuta HIGUCHI | a507baf | 2014-08-22 13:42:40 -0700 | [diff] [blame] | 152 | PortNumber.uint16(config.getBgpdAttachmentPort())); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 153 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 154 | bgpdMacAddress = config.getBgpdMacAddress(); |
| 155 | vlan = config.getVlan(); |
Sho SHIMIZU | dde5a0b | 2014-07-09 10:42:23 -0700 | [diff] [blame] | 156 | } catch (JsonParseException | JsonMappingException e) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 157 | log.error("Error in JSON file", e); |
Jonathan Hart | ec9ee2e | 2014-04-08 22:45:44 -0700 | [diff] [blame] | 158 | throw new ConfigurationRuntimeException("Error in JSON file", e); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 159 | } catch (IOException e) { |
| 160 | log.error("Error reading JSON file", e); |
Jonathan Hart | ec9ee2e | 2014-04-08 22:45:44 -0700 | [diff] [blame] | 161 | throw new ConfigurationRuntimeException("Error in JSON file", e); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 162 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 163 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 164 | // Populate the interface InvertedRadixTree |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 165 | for (Interface intf : interfaces.values()) { |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 166 | Prefix prefix = new Prefix(intf.getIpAddress().getAddress(), |
| 167 | intf.getPrefixLength()); |
| 168 | interfaceRoutes.put(prefix.toBinaryString(), intf); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 169 | } |
| 170 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 171 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 172 | @Override |
| 173 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 174 | Collection<Class<? extends IFloodlightService>> l = new ArrayList<>(); |
Jonathan Hart | 8f6dc09 | 2014-04-18 15:56:43 -0700 | [diff] [blame] | 175 | l.add(ISdnIpService.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 176 | l.add(IConfigInfoService.class); |
| 177 | return l; |
| 178 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 179 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 180 | @Override |
| 181 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 182 | Map<Class<? extends IFloodlightService>, IFloodlightService> m = new HashMap<>(); |
Jonathan Hart | 8f6dc09 | 2014-04-18 15:56:43 -0700 | [diff] [blame] | 183 | m.put(ISdnIpService.class, this); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 184 | m.put(IConfigInfoService.class, this); |
| 185 | return m; |
| 186 | } |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 187 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 188 | @Override |
| 189 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 190 | Collection<Class<? extends IFloodlightService>> l = new ArrayList<>(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 191 | l.add(IRestApiService.class); |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 192 | l.add(IControllerRegistryService.class); |
| 193 | l.add(IPathCalcRuntimeService.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 194 | return l; |
| 195 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 196 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 197 | @Override |
| 198 | public void init(FloodlightModuleContext context) |
| 199 | throws FloodlightModuleException { |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 200 | |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 201 | bgpRoutes = new ConcurrentInvertedRadixTree<>( |
| 202 | new DefaultByteArrayNodeFactory()); |
| 203 | interfaceRoutes = new ConcurrentInvertedRadixTree<>( |
| 204 | new DefaultByteArrayNodeFactory()); |
pingping-lin | 0426dee | 2014-08-27 15:03:17 -0700 | [diff] [blame] | 205 | externalNetworkSwitchPorts = new HashSet<SwitchPort>(); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 206 | |
Sho SHIMIZU | 84a72de | 2014-07-09 07:56:40 -0700 | [diff] [blame] | 207 | ribUpdates = new LinkedBlockingQueue<>(); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 208 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 209 | // Register REST handler. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 210 | restApi = context.getServiceImpl(IRestApiService.class); |
| 211 | proxyArp = context.getServiceImpl(IProxyArpService.class); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 212 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 213 | controllerRegistryService = context |
| 214 | .getServiceImpl(IControllerRegistryService.class); |
pingping-lin | bec31fc | 2014-08-28 11:48:41 -0700 | [diff] [blame] | 215 | pathRuntime = context.getServiceImpl(IPathCalcRuntimeService.class); |
| 216 | linkDiscoveryService = context.getServiceImpl(ILinkDiscoveryService.class); |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 217 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 218 | intentIdGenerator = new IdBlockAllocatorBasedIntentIdGenerator( |
| 219 | controllerRegistryService); |
Pavlin Radoslavov | ddd01ba | 2013-07-03 15:40:44 -0700 | [diff] [blame] | 220 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 221 | // TODO: initialize intentService |
| 222 | |
| 223 | pushedRouteIntents = new ConcurrentHashMap<>(); |
| 224 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 225 | prefixesWaitingOnArp = Multimaps.synchronizedSetMultimap( |
| 226 | HashMultimap.<InetAddress, RibUpdate>create()); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 227 | |
Jonathan Hart | c78b8f6 | 2014-08-07 22:31:09 -0700 | [diff] [blame] | 228 | //flowCache = new FlowCache(floodlightProvider); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 229 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 230 | bgpUpdatesExecutor = Executors.newSingleThreadExecutor( |
| 231 | new ThreadFactoryBuilder().setNameFormat("bgp-updates-%d").build()); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 232 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 233 | // Read in config values |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 234 | bgpdRestIp = context.getConfigParams(this).get("BgpdRestIp"); |
| 235 | if (bgpdRestIp == null) { |
| 236 | log.error("BgpdRestIp property not found in config file"); |
Jonathan Hart | ec9ee2e | 2014-04-08 22:45:44 -0700 | [diff] [blame] | 237 | throw new ConfigurationRuntimeException( |
| 238 | "BgpdRestIp property not found in config file"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 239 | } else { |
| 240 | log.info("BgpdRestIp set to {}", bgpdRestIp); |
| 241 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 242 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 243 | routerId = context.getConfigParams(this).get("RouterId"); |
| 244 | if (routerId == null) { |
| 245 | log.error("RouterId property not found in config file"); |
Jonathan Hart | ec9ee2e | 2014-04-08 22:45:44 -0700 | [diff] [blame] | 246 | throw new ConfigurationRuntimeException( |
| 247 | "RouterId property not found in config file"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 248 | } else { |
| 249 | log.info("RouterId set to {}", routerId); |
| 250 | } |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 251 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 252 | String configFilenameParameter = context.getConfigParams(this).get("configfile"); |
| 253 | if (configFilenameParameter != null) { |
Ray Milkey | 5df613b | 2014-04-15 10:50:56 -0700 | [diff] [blame] | 254 | currentConfigFilename = configFilenameParameter; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 255 | } |
Ray Milkey | 5df613b | 2014-04-15 10:50:56 -0700 | [diff] [blame] | 256 | log.debug("Config file set to {}", currentConfigFilename); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 257 | |
Ray Milkey | 5df613b | 2014-04-15 10:50:56 -0700 | [diff] [blame] | 258 | readConfiguration(currentConfigFilename); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 259 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 260 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 261 | @Override |
| 262 | public void startUp(FloodlightModuleContext context) { |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 263 | restApi.addRestletRoutable(new SdnIpWebRoutable()); |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 264 | restApi.addRestletRoutable(new SdnIpWebRoutableNew()); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 265 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 266 | // Retrieve the RIB from BGPd during startup |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 267 | retrieveRib(); |
| 268 | } |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 269 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 270 | @Override |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 271 | public RadixTree<RibEntry> getPtree() { |
| 272 | return bgpRoutes; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 273 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 274 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 275 | @Override |
| 276 | public void clearPtree() { |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 277 | log.warn("Clear table operation not supported"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 278 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 279 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 280 | @Override |
Jonathan Hart | 31e15f1 | 2014-04-10 10:33:00 -0700 | [diff] [blame] | 281 | public String getBgpdRestIp() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 282 | return bgpdRestIp; |
| 283 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 284 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 285 | @Override |
| 286 | public String getRouterId() { |
| 287 | return routerId; |
| 288 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 289 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 290 | /** |
| 291 | * SDN-IP application will fetch all rib entries from BGPd when it starts. |
| 292 | * Especially when we restart SDN-IP application while the BGPd has been |
| 293 | * running all the time. Then before SDN-IP application re-connects to BGPd, |
| 294 | * there are already lots of rib entries in BGPd. |
| 295 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 296 | private void retrieveRib() { |
| 297 | String url = "http://" + bgpdRestIp + "/wm/bgp/" + routerId; |
| 298 | String response = RestClient.get(url); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 299 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 300 | if ("".equals(response)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 301 | return; |
| 302 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 303 | |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 304 | try { |
| 305 | response = response.replaceAll("\"", "'"); |
| 306 | JSONObject jsonObj = (JSONObject) JSONSerializer.toJSON(response); |
| 307 | JSONArray ribArray = jsonObj.getJSONArray("rib"); |
| 308 | String inboundRouterId = jsonObj.getString("router-id"); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 309 | |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 310 | int size = ribArray.size(); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 311 | |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 312 | log.info("Retrived RIB of {} entries from BGPd", size); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 313 | |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 314 | for (int j = 0; j < size; j++) { |
| 315 | JSONObject ribEntry = ribArray.getJSONObject(j); |
| 316 | String prefix = ribEntry.getString("prefix"); |
| 317 | String nexthop = ribEntry.getString("nexthop"); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 318 | |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 319 | // Insert each rib entry into the local rib |
| 320 | String[] substring = prefix.split("/"); |
| 321 | String prefix1 = substring[0]; |
| 322 | String mask1 = substring[1]; |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 323 | |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 324 | Prefix p; |
| 325 | try { |
| 326 | p = new Prefix(prefix1, Integer.parseInt(mask1)); |
| 327 | } catch (NumberFormatException e) { |
| 328 | log.warn("Wrong mask format in RIB JSON: {}", mask1); |
| 329 | continue; |
| 330 | } catch (IllegalArgumentException e1) { |
| 331 | log.warn("Wrong prefix format in RIB JSON: {}", prefix1); |
| 332 | continue; |
| 333 | } |
| 334 | |
| 335 | RibEntry rib = new RibEntry(inboundRouterId, nexthop); |
| 336 | |
| 337 | try { |
| 338 | ribUpdates.put(new RibUpdate(Operation.UPDATE, p, rib)); |
| 339 | } catch (InterruptedException e) { |
| 340 | log.debug("Interrupted while pushing onto update queue"); |
| 341 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 342 | } |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 343 | } catch (JSONException e) { |
| 344 | // TODO don't parse JSON manually |
| 345 | log.error("Error parsing inital route table JSON:", e); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 346 | } |
| 347 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 348 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 349 | /** |
| 350 | * Put RIB update to RIB update queue. |
| 351 | * |
| 352 | * @param update RIB update |
| 353 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 354 | @Override |
| 355 | public void newRibUpdate(RibUpdate update) { |
| 356 | try { |
| 357 | ribUpdates.put(update); |
| 358 | } catch (InterruptedException e) { |
| 359 | log.debug("Interrupted while putting on ribUpdates queue", e); |
| 360 | Thread.currentThread().interrupt(); |
| 361 | } |
| 362 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 363 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 364 | /** |
| 365 | * Process adding RIB update. |
| 366 | * Put new RIB update into InvertedRadixTree. If there was an existing nexthop |
| 367 | * for this prefix, but the next hop was different, then execute deleting old |
| 368 | * RIB update. If the next hop is the SDN domain, we do not handle it at the |
| 369 | * moment. Otherwise, execute adding RIB. |
| 370 | * |
| 371 | * @param update RIB update |
| 372 | */ |
Jonathan Hart | 43f2874 | 2014-08-22 16:34:05 -0700 | [diff] [blame] | 373 | protected void processRibAdd(RibUpdate update) { |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 374 | synchronized (this) { |
| 375 | Prefix prefix = update.getPrefix(); |
Ray Milkey | 5d40601 | 2014-04-08 14:44:41 -0700 | [diff] [blame] | 376 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 377 | log.debug("Processing prefix add {}", prefix); |
Ray Milkey | 5d40601 | 2014-04-08 14:44:41 -0700 | [diff] [blame] | 378 | |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 379 | RibEntry rib = bgpRoutes.put(prefix.toBinaryString(), update.getRibEntry()); |
Ray Milkey | 5d40601 | 2014-04-08 14:44:41 -0700 | [diff] [blame] | 380 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 381 | if (rib != null && !rib.equals(update.getRibEntry())) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 382 | // There was an existing nexthop for this prefix. This update |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 383 | // supersedes that, so we need to remove the old flows for this |
| 384 | // prefix from the switches |
| 385 | executeDeleteRoute(prefix, rib); |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 386 | } |
Ray Milkey | 5d40601 | 2014-04-08 14:44:41 -0700 | [diff] [blame] | 387 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 388 | if (update.getRibEntry().getNextHop().equals( |
| 389 | InetAddresses.forString("0.0.0.0"))) { |
| 390 | // Route originated by SDN domain |
| 391 | // We don't handle these at the moment |
| 392 | log.debug("Own route {} to {}", prefix, |
| 393 | update.getRibEntry().getNextHop().getHostAddress()); |
| 394 | return; |
| 395 | } |
Ray Milkey | 5d40601 | 2014-04-08 14:44:41 -0700 | [diff] [blame] | 396 | |
Ray Milkey | 7531a34 | 2014-04-11 15:08:12 -0700 | [diff] [blame] | 397 | executeRibAdd(update); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 398 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 399 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 400 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 401 | /** |
| 402 | * Execute adding RIB update. |
| 403 | * Find out the egress Interface and MAC address of next hop router for this |
| 404 | * RIB update. If the MAC address can not be found in ARP cache, then this |
| 405 | * prefix will be put in prefixesWaitingOnArp queue. Otherwise, new flow |
| 406 | * intent will be created and installed. |
| 407 | * |
| 408 | * @param update RIB update |
| 409 | */ |
Ray Milkey | 7531a34 | 2014-04-11 15:08:12 -0700 | [diff] [blame] | 410 | private void executeRibAdd(RibUpdate update) { |
Pavlin Radoslavov | 9fc535a | 2014-04-11 13:00:12 -0700 | [diff] [blame] | 411 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 412 | Prefix prefix = update.getPrefix(); |
| 413 | RibEntry rib = update.getRibEntry(); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 414 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 415 | InetAddress nextHopIpAddress = rib.getNextHop(); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 416 | |
Jonathan Hart | 43f2874 | 2014-08-22 16:34:05 -0700 | [diff] [blame] | 417 | // See if we know the MAC address of the next hop |
| 418 | MACAddress nextHopMacAddress = proxyArp.getMacAddress(nextHopIpAddress); |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 419 | |
Jonathan Hart | 43f2874 | 2014-08-22 16:34:05 -0700 | [diff] [blame] | 420 | if (nextHopMacAddress == null) { |
| 421 | prefixesWaitingOnArp.put(nextHopIpAddress, |
| 422 | new RibUpdate(Operation.UPDATE, prefix, rib)); |
| 423 | proxyArp.sendArpRequest(nextHopIpAddress, this, true); |
| 424 | return; |
| 425 | } |
| 426 | |
| 427 | addRouteIntentToNextHop(prefix, nextHopIpAddress, nextHopMacAddress); |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * Adds a route intent given a prefix and a next hop IP address. This |
| 432 | * method will find the egress interface for the intent. |
| 433 | * |
| 434 | * @param prefix IP prefix of the route to add |
| 435 | * @param nextHopIpAddress IP address of the next hop |
| 436 | * @param nextHopMacAddress MAC address of the next hop |
| 437 | */ |
| 438 | private void addRouteIntentToNextHop(Prefix prefix, InetAddress nextHopIpAddress, |
| 439 | MACAddress nextHopMacAddress) { |
| 440 | |
| 441 | // Find the attachment point (egress interface) of the next hop |
| 442 | Interface egressInterface; |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 443 | if (bgpPeers.containsKey(nextHopIpAddress)) { |
| 444 | // Route to a peer |
| 445 | log.debug("Route to peer {}", nextHopIpAddress); |
| 446 | BgpPeer peer = bgpPeers.get(nextHopIpAddress); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 447 | egressInterface = interfaces.get(peer.getInterfaceName()); |
| 448 | } else { |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 449 | // Route to non-peer |
| 450 | log.debug("Route to non-peer {}", nextHopIpAddress); |
| 451 | egressInterface = getOutgoingInterface(nextHopIpAddress); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 452 | if (egressInterface == null) { |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 453 | log.warn("No outgoing interface found for {}", nextHopIpAddress |
| 454 | .getHostAddress()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 455 | return; |
| 456 | } |
| 457 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 458 | |
Jonathan Hart | 43f2874 | 2014-08-22 16:34:05 -0700 | [diff] [blame] | 459 | doAddRouteIntent(prefix, egressInterface, nextHopMacAddress); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 460 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 461 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 462 | /** |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 463 | * Install a flow intent for a prefix. |
| 464 | * Intent will match dst IP prefix and rewrite dst MAC address at all other |
| 465 | * border switches, then forward packets according to dst MAC address. |
| 466 | * |
| 467 | * @param prefix IP prefix from BGP route |
| 468 | * @param egressInterface egress Interface connected to next hop router |
| 469 | * @param nextHopMacAddress MAC address of next hop router |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 470 | */ |
Jonathan Hart | 43f2874 | 2014-08-22 16:34:05 -0700 | [diff] [blame] | 471 | private void doAddRouteIntent(Prefix prefix, Interface egressInterface, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 472 | MACAddress nextHopMacAddress) { |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 473 | log.debug("Adding intent for prefix {}, next hop mac {}", |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 474 | prefix, nextHopMacAddress); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 475 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 476 | MultiPointToSinglePointIntent pushedIntent = pushedRouteIntents.get(prefix); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 477 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 478 | // Just for testing. |
| 479 | if (pushedIntent != null) { |
| 480 | log.error("There should not be a pushed intent: {}", pushedIntent); |
| 481 | } |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 482 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 483 | SwitchPort egressPort = egressInterface.getSwitchPort(); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 484 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 485 | Set<SwitchPort> ingressPorts = new HashSet<SwitchPort>(); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 486 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 487 | for (Interface intf : interfaces.values()) { |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 488 | if (!intf.equals(egressInterface)) { |
| 489 | SwitchPort srcPort = intf.getSwitchPort(); |
| 490 | ingressPorts.add(srcPort); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 491 | } |
| 492 | } |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 493 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 494 | // Match the destination IP prefix at the first hop |
| 495 | PacketMatchBuilder builder = new PacketMatchBuilder(); |
| 496 | builder.setDstIp(new IPv4(InetAddresses |
| 497 | .coerceToInteger(prefix.getInetAddress())), |
| 498 | (short) prefix.getPrefixLength()); |
| 499 | PacketMatch packetMatch = builder.build(); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 500 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 501 | // Rewrite the destination MAC address |
| 502 | ModifyDstMacAction modifyDstMacAction = |
| 503 | new ModifyDstMacAction(nextHopMacAddress); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 504 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 505 | MultiPointToSinglePointIntent intent = |
| 506 | new MultiPointToSinglePointIntent(intentIdGenerator.getNewId(), |
| 507 | packetMatch, modifyDstMacAction, ingressPorts, egressPort); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 508 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 509 | intentService.submit(intent); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 510 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 511 | // Maintain the Intent |
| 512 | pushedRouteIntents.put(prefix, intent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 513 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 514 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 515 | /** |
| 516 | * Remove prefix from InvertedRadixTree, if success, then try to delete the |
| 517 | * relative intent. |
| 518 | * |
| 519 | * @param update RIB update |
| 520 | */ |
Jonathan Hart | 6e01c85 | 2014-08-21 18:53:46 -0700 | [diff] [blame] | 521 | protected void processRibDelete(RibUpdate update) { |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 522 | synchronized (this) { |
| 523 | Prefix prefix = update.getPrefix(); |
Ray Milkey | 5d40601 | 2014-04-08 14:44:41 -0700 | [diff] [blame] | 524 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 525 | // if (ptree.remove(prefix, update.getRibEntry())) { |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 526 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 527 | // TODO check the change of logic here - remove doesn't check that |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 528 | // the rib entry was what we expected (and we can't do this |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 529 | // concurrently) |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 530 | |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 531 | if (bgpRoutes.remove(prefix.toBinaryString())) { |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 532 | /* |
Jonathan Hart | 5e54f2e | 2014-04-17 13:43:40 -0700 | [diff] [blame] | 533 | * Only delete flows if an entry was actually removed from the tree. |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 534 | * If no entry was removed, the <prefix, nexthop> wasn't there so |
| 535 | * it's probably already been removed and we don't need to do anything |
| 536 | */ |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 537 | executeDeleteRoute(prefix, update.getRibEntry()); |
| 538 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 539 | } |
Jonathan Hart | 43f2874 | 2014-08-22 16:34:05 -0700 | [diff] [blame] | 540 | |
| 541 | prefixesWaitingOnArp.removeAll(prefix.getInetAddress()); |
| 542 | // TODO cancel the request in the ARP manager as well |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 543 | } |
| 544 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 545 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 546 | /** |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 547 | * Delete prefix intent installed. |
| 548 | * |
| 549 | * @param prefix IP prefix withdrew in a rib update announcement |
| 550 | * @param ribEntry next hop information |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 551 | */ |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 552 | private void executeDeleteRoute(Prefix prefix, RibEntry ribEntry) { |
| 553 | log.debug("Deleting {} to {}", prefix, ribEntry.getNextHop()); |
Pavlin Radoslavov | 9fc535a | 2014-04-11 13:00:12 -0700 | [diff] [blame] | 554 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 555 | MultiPointToSinglePointIntent intent = pushedRouteIntents.remove(prefix); |
Pavlin Radoslavov | 9fc535a | 2014-04-11 13:00:12 -0700 | [diff] [blame] | 556 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 557 | if (intent == null) { |
| 558 | log.debug("There is no intent in pushedRouteIntents to delete for " + |
| 559 | "prefix: {}", prefix); |
| 560 | } else { |
| 561 | intentService.withdraw(intent); |
| 562 | log.debug("Deleted the pushedRouteIntent for prefix: {}", prefix); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 563 | } |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 564 | } |
| 565 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 566 | /** |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 567 | * Setup the Paths to the BGP Daemon. Run a loop for all of the bgpPeers |
| 568 | * Push flow from BGPd to the peer Push flow from peer to BGPd Parameters to |
| 569 | * pass to the intent are as follows: String id, long srcSwitch, long |
| 570 | * srcPort, long srcMac, int srcIP, long dstSwitch, long dstPort, long |
| 571 | * dstMac, int dstIP |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 572 | */ |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 573 | private void setupBgpPaths() { |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 574 | IntentOperationList operations = new IntentOperationList(); |
| 575 | for (BgpPeer bgpPeer : bgpPeers.values()) { |
| 576 | Interface peerInterface = interfaces.get(bgpPeer.getInterfaceName()); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 577 | // Inet4Address. |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 578 | int srcIP = InetAddresses.coerceToInteger(peerInterface.getIpAddress()); |
| 579 | int dstIP = InetAddresses.coerceToInteger(bgpPeer.getIpAddress()); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 580 | String fwdIntentId = caller + ":" |
| 581 | + controllerRegistryService.getNextUniqueId(); |
| 582 | String bwdIntentId = caller + ":" |
| 583 | + controllerRegistryService.getNextUniqueId(); |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 584 | SwitchPort srcPort = |
Pavlin Radoslavov | 3d322f4 | 2014-08-18 14:58:55 -0700 | [diff] [blame] | 585 | new SwitchPort(bgpdAttachmentPoint.getDpid(), |
| 586 | bgpdAttachmentPoint.getPortNumber()); |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 587 | // TODO: replace the code below with peerInterface.getSwitchPort() |
| 588 | // when using poingToPointIntent |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 589 | SwitchPort dstPort = |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 590 | new SwitchPort(new Dpid(peerInterface.getDpid()), |
Pavlin Radoslavov | 3d322f4 | 2014-08-18 14:58:55 -0700 | [diff] [blame] | 591 | new PortNumber(peerInterface.getSwitchPort().getPortNumber())); |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 592 | |
| 593 | // TODO: add TCP port number 179 into intent for BGP |
| 594 | |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 595 | ShortestPathIntent fwdIntent = new ShortestPathIntent(fwdIntentId, |
Pavlin Radoslavov | 3d322f4 | 2014-08-18 14:58:55 -0700 | [diff] [blame] | 596 | srcPort.getDpid().value(), srcPort.getPortNumber().value(), |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 597 | ShortestPathIntent.EMPTYMACADDRESS, srcIP, |
Pavlin Radoslavov | 3d322f4 | 2014-08-18 14:58:55 -0700 | [diff] [blame] | 598 | dstPort.getDpid().value(), dstPort.getPortNumber().value(), |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 599 | ShortestPathIntent.EMPTYMACADDRESS, dstIP); |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 600 | ShortestPathIntent bwdIntent = new ShortestPathIntent(bwdIntentId, |
Pavlin Radoslavov | 3d322f4 | 2014-08-18 14:58:55 -0700 | [diff] [blame] | 601 | dstPort.getDpid().value(), dstPort.getPortNumber().value(), |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 602 | ShortestPathIntent.EMPTYMACADDRESS, dstIP, |
Pavlin Radoslavov | 3d322f4 | 2014-08-18 14:58:55 -0700 | [diff] [blame] | 603 | srcPort.getDpid().value(), srcPort.getPortNumber().value(), |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 604 | ShortestPathIntent.EMPTYMACADDRESS, srcIP); |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 605 | IntentOperation.Operator operator = IntentOperation.Operator.ADD; |
| 606 | operations.add(operator, fwdIntent); |
| 607 | operations.add(operator, bwdIntent); |
| 608 | } |
| 609 | pathRuntime.executeIntentOperations(operations); |
| 610 | } |
| 611 | |
pingping-lin | aea385b | 2014-09-04 18:15:19 -0700 | [diff] [blame] | 612 | |
| 613 | /** |
| 614 | * Setup paths for BGP Daemon and its peers. Run a loop for all the bgpPeers. |
| 615 | * Push intent for path from BGPd to the peer. Push intent for path from peer |
| 616 | * to BGPd. |
| 617 | */ |
| 618 | private void setupBgpPathsWithNewIntent() { |
| 619 | for (BgpPeer bgpPeer : bgpPeers.values()) { |
| 620 | |
| 621 | log.debug("Start to set up BGP paths."); |
| 622 | |
| 623 | Interface peerInterface = interfaces.get(bgpPeer.getInterfaceName()); |
| 624 | |
| 625 | IPv4 bgpdAddress = new IPv4(InetAddresses |
| 626 | .coerceToInteger(peerInterface.getIpAddress())); |
| 627 | IPv4 bgpdPeerAddress = new IPv4(InetAddresses |
| 628 | .coerceToInteger(bgpPeer.getIpAddress())); |
| 629 | |
| 630 | SwitchPort bgpdSwitchPort = bgpdAttachmentPoint; |
| 631 | SwitchPort bgpdPeerSwitchPort = peerInterface.getSwitchPort(); |
| 632 | |
| 633 | // install intent for BGP path from BGPd to BGP peer matching |
| 634 | // destination TCP port 179 |
| 635 | |
| 636 | // TODO: The usage of PacketMatchBuilder will be improved, then we |
| 637 | // only need to new the PacketMatchBuilder once. |
| 638 | // By then, the code here will be improved accordingly. |
| 639 | PacketMatchBuilder builderMatchDstTcpPort = new PacketMatchBuilder(); |
| 640 | builderMatchDstTcpPort.setEtherType(Ethernet.TYPE_IPV4); |
| 641 | builderMatchDstTcpPort.setIpProto(PROTOCOL_TCP); |
| 642 | builderMatchDstTcpPort.setSrcIp(bgpdAddress); |
| 643 | builderMatchDstTcpPort.setDstIp(bgpdPeerAddress); |
| 644 | builderMatchDstTcpPort.setDstTcpPort(BGP_PORT); |
| 645 | |
| 646 | PacketMatch packetMatch = builderMatchDstTcpPort.build(); |
| 647 | |
| 648 | PointToPointIntent intentMatchDstTcpPort = new PointToPointIntent( |
| 649 | intentIdGenerator.getNewId(), packetMatch, Actions |
| 650 | .nullAction(), bgpdSwitchPort, bgpdPeerSwitchPort); |
| 651 | intentService.submit(intentMatchDstTcpPort); |
| 652 | log.debug("Submitted BGP path intent matching dst TCP port 179 " |
| 653 | + "from BGPd to peer {}: {}", |
| 654 | bgpdPeerAddress, intentMatchDstTcpPort); |
| 655 | |
| 656 | // install intent for BGP path from BGPd to BGP peer matching |
| 657 | // source TCP port 179 |
| 658 | PacketMatchBuilder builderMatchSrcTcpPort = new PacketMatchBuilder(); |
| 659 | builderMatchSrcTcpPort.setEtherType(Ethernet.TYPE_IPV4); |
| 660 | builderMatchSrcTcpPort.setIpProto(PROTOCOL_TCP); |
| 661 | builderMatchSrcTcpPort.setSrcIp(bgpdAddress); |
| 662 | builderMatchSrcTcpPort.setDstIp(bgpdPeerAddress); |
| 663 | builderMatchSrcTcpPort.setSrcTcpPort(BGP_PORT); |
| 664 | |
| 665 | packetMatch = builderMatchSrcTcpPort.build(); |
| 666 | |
| 667 | PointToPointIntent intentMatchSrcTcpPort = new PointToPointIntent( |
| 668 | intentIdGenerator.getNewId(), packetMatch, Actions |
| 669 | .nullAction(), bgpdSwitchPort, bgpdPeerSwitchPort); |
| 670 | intentService.submit(intentMatchSrcTcpPort); |
| 671 | log.debug("Submitted BGP path intent matching src TCP port 179" |
| 672 | + "from BGPd to peer {}: {}", |
| 673 | bgpdPeerAddress, intentMatchSrcTcpPort); |
| 674 | |
| 675 | // install intent for reversed BGP path from BGP peer to BGPd |
| 676 | // matching destination TCP port 179 |
| 677 | PacketMatchBuilder reversedBuilderMatchDstTcpPort = new PacketMatchBuilder(); |
| 678 | reversedBuilderMatchDstTcpPort.setEtherType(Ethernet.TYPE_IPV4); |
| 679 | reversedBuilderMatchDstTcpPort.setIpProto(PROTOCOL_TCP); |
| 680 | reversedBuilderMatchDstTcpPort.setSrcIp(bgpdPeerAddress); |
| 681 | reversedBuilderMatchDstTcpPort.setDstIp(bgpdAddress); |
| 682 | reversedBuilderMatchDstTcpPort.setDstTcpPort(BGP_PORT); |
| 683 | |
| 684 | packetMatch = reversedBuilderMatchDstTcpPort.build(); |
| 685 | |
| 686 | PointToPointIntent reversedIntentMatchDstTcpPort = new PointToPointIntent( |
| 687 | intentIdGenerator.getNewId(), packetMatch, Actions |
| 688 | .nullAction(), bgpdPeerSwitchPort, bgpdSwitchPort); |
| 689 | intentService.submit(reversedIntentMatchDstTcpPort); |
| 690 | log.debug("Submitted BGP path intent matching dst TCP port 179" |
| 691 | + "from BGP peer {} to BGPd: {}", |
| 692 | bgpdPeerAddress, reversedIntentMatchDstTcpPort); |
| 693 | |
| 694 | // install intent for reversed BGP path from BGP peer to BGPd |
| 695 | // matching source TCP port 179 |
| 696 | PacketMatchBuilder reversedBuilderMatchSrcTcpPort = new PacketMatchBuilder(); |
| 697 | reversedBuilderMatchSrcTcpPort.setEtherType(Ethernet.TYPE_IPV4); |
| 698 | reversedBuilderMatchSrcTcpPort.setIpProto(PROTOCOL_TCP); |
| 699 | reversedBuilderMatchSrcTcpPort.setSrcIp(bgpdPeerAddress); |
| 700 | reversedBuilderMatchSrcTcpPort.setDstIp(bgpdAddress); |
| 701 | reversedBuilderMatchSrcTcpPort.setSrcTcpPort(BGP_PORT); |
| 702 | |
| 703 | packetMatch = reversedBuilderMatchSrcTcpPort.build(); |
| 704 | |
| 705 | PointToPointIntent reversedIntentMatchSrcTcpPort = new PointToPointIntent( |
| 706 | intentIdGenerator.getNewId(), packetMatch, Actions |
| 707 | .nullAction(), bgpdPeerSwitchPort, bgpdSwitchPort); |
| 708 | intentService.submit(reversedIntentMatchSrcTcpPort); |
| 709 | log.debug("Submitted BGP path intent matching src TCP port 179" |
| 710 | + "from BGP peer {} to BGPd: {}", |
| 711 | bgpdPeerAddress, reversedIntentMatchSrcTcpPort); |
| 712 | |
| 713 | } |
| 714 | |
| 715 | } |
| 716 | |
| 717 | /** |
| 718 | * Setup ICMP paths between BGP Daemon and its peers. Run a loop for all the |
| 719 | * bgpPeers. Push intent for path from BGPd to the peer. Push intent for path |
| 720 | * from peer to BGPd. |
| 721 | */ |
| 722 | private void setupIcmpPaths() { |
| 723 | for (BgpPeer bgpPeer : bgpPeers.values()) { |
| 724 | |
| 725 | Interface peerInterface = interfaces.get(bgpPeer.getInterfaceName()); |
| 726 | |
| 727 | PacketMatchBuilder builder = new PacketMatchBuilder(); |
| 728 | builder.setEtherType(Ethernet.TYPE_IPV4); |
| 729 | builder.setIpProto(PROTOCOL_ICMP); |
| 730 | |
| 731 | IPv4 bgpdAddress = new IPv4(InetAddresses |
| 732 | .coerceToInteger(peerInterface.getIpAddress())); |
| 733 | IPv4 bgpdPeerAddress = new IPv4(InetAddresses |
| 734 | .coerceToInteger(bgpPeer.getIpAddress())); |
| 735 | |
| 736 | SwitchPort bgpdSwitchPort = bgpdAttachmentPoint; |
| 737 | SwitchPort bgpdPeerSwitchPort = peerInterface.getSwitchPort(); |
| 738 | |
| 739 | // install intent for ICMP path from BGPd to BGP peer |
| 740 | builder.setSrcIp(bgpdAddress); |
| 741 | builder.setDstIp(bgpdPeerAddress); |
| 742 | PacketMatch packetMatch = builder.build(); |
| 743 | |
| 744 | PointToPointIntent intent = new PointToPointIntent( |
| 745 | intentIdGenerator.getNewId(), packetMatch, Actions |
| 746 | .nullAction(), bgpdSwitchPort, bgpdPeerSwitchPort); |
| 747 | intentService.submit(intent); |
| 748 | log.debug("Submitted ICMP path intent from BGPd to peer {}: {}", |
| 749 | bgpdPeerAddress, intent); |
| 750 | |
| 751 | // install intent for reversed ICMP path from BGP peer to BGPd |
| 752 | builder.setSrcIp(bgpdPeerAddress); |
| 753 | builder.setDstIp(bgpdAddress); |
| 754 | packetMatch = builder.build(); |
| 755 | |
| 756 | PointToPointIntent reversedIntent = new PointToPointIntent( |
| 757 | intentIdGenerator.getNewId(), packetMatch, Actions |
| 758 | .nullAction(), bgpdPeerSwitchPort, bgpdSwitchPort); |
| 759 | intentService.submit(reversedIntent); |
| 760 | log.debug("Submitted ICMP path intent from BGP peer {} to BGPd: {}", |
| 761 | bgpdPeerAddress, reversedIntent); |
| 762 | } |
| 763 | |
| 764 | } |
| 765 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 766 | /** |
| 767 | * This method handles the prefixes which are waiting for ARP replies for |
| 768 | * MAC addresses of next hops. |
| 769 | * |
| 770 | * @param ipAddress next hop router IP address, for which we sent ARP request out |
| 771 | * @param macAddress MAC address which is relative to the ipAddress |
| 772 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 773 | @Override |
| 774 | public void arpResponse(InetAddress ipAddress, MACAddress macAddress) { |
| 775 | log.debug("Received ARP response: {} => {}", |
| 776 | ipAddress.getHostAddress(), macAddress); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 777 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 778 | /* |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 779 | * We synchronize on this to prevent changes to the InvertedRadixTree |
| 780 | * while we're pushing intent. If the InvertedRadixTree changes, the |
| 781 | * InvertedRadixTree and intent could get out of sync. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 782 | */ |
| 783 | synchronized (this) { |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 784 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 785 | Set<RibUpdate> prefixesToPush = prefixesWaitingOnArp.removeAll(ipAddress); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 786 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 787 | for (RibUpdate update : prefixesToPush) { |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 788 | // These will always be adds |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 789 | |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 790 | RibEntry rib = bgpRoutes.getValueForExactKey( |
| 791 | update.getPrefix().toBinaryString()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 792 | if (rib != null && rib.equals(update.getRibEntry())) { |
| 793 | log.debug("Pushing prefix {} next hop {}", update.getPrefix(), |
| 794 | rib.getNextHop().getHostAddress()); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 795 | // We only push prefix flows if the prefix is still in the |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 796 | // InvertedRadixTree and the next hop is the same as our update. |
| 797 | // The prefix could have been removed while we were waiting |
| 798 | // for the ARP, or the next hop could have changed. |
Jonathan Hart | 43f2874 | 2014-08-22 16:34:05 -0700 | [diff] [blame] | 799 | addRouteIntentToNextHop(update.getPrefix(), ipAddress, |
| 800 | macAddress); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 801 | } else { |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 802 | log.debug("Received ARP response, but {},{} is no longer in " + |
| 803 | "InvertedRadixTree", update.getPrefix(), |
| 804 | update.getRibEntry()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 805 | } |
| 806 | } |
| 807 | } |
| 808 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 809 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 810 | |
Jonathan Hart | c78b8f6 | 2014-08-07 22:31:09 -0700 | [diff] [blame] | 811 | /*private void setupArpFlows() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 812 | OFMatch match = new OFMatch(); |
| 813 | match.setDataLayerType(Ethernet.TYPE_ARP); |
| 814 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 815 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 816 | OFFlowMod fm = new OFFlowMod(); |
| 817 | fm.setMatch(match); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 818 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 819 | OFActionOutput action = new OFActionOutput(); |
| 820 | action.setPort(OFPort.OFPP_CONTROLLER.getValue()); |
| 821 | action.setMaxLength((short) 0xffff); |
Sho SHIMIZU | 84a72de | 2014-07-09 07:56:40 -0700 | [diff] [blame] | 822 | List<OFAction> actions = new ArrayList<>(1); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 823 | actions.add(action); |
| 824 | fm.setActions(actions); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 825 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 826 | fm.setIdleTimeout((short) 0) |
| 827 | .setHardTimeout((short) 0) |
| 828 | .setBufferId(OFPacketOut.BUFFER_ID_NONE) |
| 829 | .setCookie(0) |
| 830 | .setCommand(OFFlowMod.OFPFC_ADD) |
| 831 | .setPriority(ARP_PRIORITY) |
| 832 | .setLengthU(OFFlowMod.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 833 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 834 | for (String strdpid : switches) { |
| 835 | flowCache.write(HexString.toLong(strdpid), fm); |
| 836 | } |
| 837 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 838 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 839 | private void setupDefaultDropFlows() { |
| 840 | OFFlowMod fm = new OFFlowMod(); |
| 841 | fm.setMatch(new OFMatch()); |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 842 | fm.setActions(new ArrayList<OFAction>()); // No action means drop |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 843 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 844 | fm.setIdleTimeout((short) 0) |
| 845 | .setHardTimeout((short) 0) |
| 846 | .setBufferId(OFPacketOut.BUFFER_ID_NONE) |
| 847 | .setCookie(0) |
| 848 | .setCommand(OFFlowMod.OFPFC_ADD) |
| 849 | .setPriority((short) 0) |
| 850 | .setLengthU(OFFlowMod.MINIMUM_LENGTH); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 851 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 852 | OFFlowMod fmLLDP; |
| 853 | OFFlowMod fmBDDP; |
| 854 | try { |
| 855 | fmLLDP = fm.clone(); |
| 856 | fmBDDP = fm.clone(); |
| 857 | } catch (CloneNotSupportedException e1) { |
| 858 | log.error("Error cloning flow mod", e1); |
| 859 | return; |
| 860 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 861 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 862 | OFMatch matchLLDP = new OFMatch(); |
| 863 | matchLLDP.setDataLayerType((short) 0x88cc); |
| 864 | matchLLDP.setWildcards(matchLLDP.getWildcards() & ~OFMatch.OFPFW_DL_TYPE); |
| 865 | fmLLDP.setMatch(matchLLDP); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 866 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 867 | OFMatch matchBDDP = new OFMatch(); |
| 868 | matchBDDP.setDataLayerType((short) 0x8942); |
| 869 | matchBDDP.setWildcards(matchBDDP.getWildcards() & ~OFMatch.OFPFW_DL_TYPE); |
| 870 | fmBDDP.setMatch(matchBDDP); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 871 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 872 | OFActionOutput action = new OFActionOutput(); |
| 873 | action.setPort(OFPort.OFPP_CONTROLLER.getValue()); |
| 874 | action.setMaxLength((short) 0xffff); |
Sho SHIMIZU | 84a72de | 2014-07-09 07:56:40 -0700 | [diff] [blame] | 875 | List<OFAction> actions = new ArrayList<>(1); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 876 | actions.add(action); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 877 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 878 | fmLLDP.setActions(actions); |
| 879 | fmBDDP.setActions(actions); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 880 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 881 | fmLLDP.setPriority(ARP_PRIORITY); |
| 882 | fmLLDP.setLengthU(OFFlowMod.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH); |
| 883 | fmBDDP.setPriority(ARP_PRIORITY); |
| 884 | fmBDDP.setLengthU(OFFlowMod.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 885 | |
Sho SHIMIZU | 84a72de | 2014-07-09 07:56:40 -0700 | [diff] [blame] | 886 | List<OFFlowMod> flowModList = new ArrayList<>(3); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 887 | flowModList.add(fm); |
| 888 | flowModList.add(fmLLDP); |
| 889 | flowModList.add(fmBDDP); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 890 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 891 | for (String strdpid : switches) { |
| 892 | flowCache.write(HexString.toLong(strdpid), flowModList); |
| 893 | } |
Jonathan Hart | c78b8f6 | 2014-08-07 22:31:09 -0700 | [diff] [blame] | 894 | }*/ |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 895 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 896 | /** |
| 897 | * The SDN-IP application is started from this method. |
| 898 | */ |
| 899 | @Override |
| 900 | public void beginRouting() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 901 | log.debug("Topology is now ready, beginning routing function"); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 902 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 903 | // TODO |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 904 | /*setupArpFlows(); |
| 905 | setupDefaultDropFlows();*/ |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 906 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 907 | setupBgpPaths(); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 908 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 909 | // Suppress link discovery on external-facing router ports |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 910 | for (Interface intf : interfaces.values()) { |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 911 | linkDiscoveryService.disableDiscoveryOnPort(intf.getDpid(), intf.getPort()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 912 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 913 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 914 | bgpUpdatesExecutor.execute(new Runnable() { |
| 915 | @Override |
| 916 | public void run() { |
| 917 | doUpdatesThread(); |
| 918 | } |
| 919 | }); |
| 920 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 921 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 922 | /** |
pingping-lin | aea385b | 2014-09-04 18:15:19 -0700 | [diff] [blame] | 923 | * The SDN-IP application is started from this method. |
| 924 | * Before intent framework is ready, we need two methods to start the |
| 925 | * application. |
| 926 | */ |
| 927 | @Override |
| 928 | public void beginRoutingWithNewIntent() { |
| 929 | log.debug("Topology is now ready, beginning routing function"); |
| 930 | |
| 931 | // TODO |
| 932 | /*setupArpFlows(); |
| 933 | setupDefaultDropFlows();*/ |
| 934 | |
| 935 | setupBgpPathsWithNewIntent(); |
| 936 | setupIcmpPaths(); |
| 937 | |
| 938 | // Suppress link discovery on external-facing router ports |
| 939 | for (Interface intf : interfaces.values()) { |
| 940 | linkDiscoveryService.disableDiscoveryOnPort(intf.getDpid(), intf.getPort()); |
| 941 | } |
| 942 | |
| 943 | bgpUpdatesExecutor.execute(new Runnable() { |
| 944 | @Override |
| 945 | public void run() { |
| 946 | doUpdatesThread(); |
| 947 | } |
| 948 | }); |
| 949 | } |
| 950 | |
| 951 | /** |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 952 | * Thread for handling RIB updates. |
| 953 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 954 | private void doUpdatesThread() { |
| 955 | boolean interrupted = false; |
| 956 | try { |
| 957 | while (true) { |
| 958 | try { |
| 959 | RibUpdate update = ribUpdates.take(); |
| 960 | switch (update.getOperation()) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 961 | case UPDATE: |
| 962 | if (validateUpdate(update)) { |
| 963 | processRibAdd(update); |
| 964 | } else { |
| 965 | log.debug("Rib UPDATE out of order: {} via {}", |
| 966 | update.getPrefix(), update.getRibEntry().getNextHop()); |
| 967 | } |
| 968 | break; |
| 969 | case DELETE: |
| 970 | if (validateUpdate(update)) { |
| 971 | processRibDelete(update); |
| 972 | } else { |
| 973 | log.debug("Rib DELETE out of order: {} via {}", |
| 974 | update.getPrefix(), update.getRibEntry().getNextHop()); |
| 975 | } |
| 976 | break; |
| 977 | default: |
| 978 | log.error("Unknown operation {}", update.getOperation()); |
| 979 | break; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 980 | } |
| 981 | } catch (InterruptedException e) { |
| 982 | log.debug("Interrupted while taking from updates queue", e); |
| 983 | interrupted = true; |
| 984 | } catch (Exception e) { |
| 985 | log.debug("exception", e); |
| 986 | } |
| 987 | } |
| 988 | } finally { |
| 989 | if (interrupted) { |
| 990 | Thread.currentThread().interrupt(); |
| 991 | } |
| 992 | } |
| 993 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 994 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 995 | /** |
| 996 | * Judge whether a RIB update is in correct order. |
| 997 | * |
| 998 | * @param update RIB update |
| 999 | * @return boolean whether the RIB update is in in correct order |
| 1000 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1001 | private boolean validateUpdate(RibUpdate update) { |
| 1002 | RibEntry newEntry = update.getRibEntry(); |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 1003 | RibEntry oldEntry = bgpRoutes.getValueForExactKey( |
| 1004 | update.getPrefix().toBinaryString()); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1005 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1006 | // If there is no existing entry we must assume this is the most recent |
| 1007 | // update. However this might not always be the case as we might have a |
| 1008 | // POST then DELETE reordering. |
| 1009 | // if (oldEntry == null || |
| 1010 | // !newEntry.getNextHop().equals(oldEntry.getNextHop())) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1011 | if (oldEntry == null) { |
| 1012 | return true; |
| 1013 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1014 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1015 | // This handles the case where routes are gathered in the initial |
| 1016 | // request because they don't have sequence number info |
| 1017 | if (newEntry.getSysUpTime() == -1 && newEntry.getSequenceNum() == -1) { |
| 1018 | return true; |
| 1019 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1020 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1021 | if (newEntry.getSysUpTime() > oldEntry.getSysUpTime()) { |
| 1022 | return true; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1023 | } |
Ray Milkey | 4985f21 | 2014-04-10 16:57:05 -0700 | [diff] [blame] | 1024 | |
| 1025 | return newEntry.getSysUpTime() == oldEntry.getSysUpTime() && |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1026 | newEntry.getSequenceNum() > oldEntry.getSequenceNum(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1027 | } |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 1028 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 1029 | /** |
| 1030 | * To find the Interface which has longest matchable IP prefix (sub-network |
| 1031 | * prefix) to next hop IP address. |
| 1032 | * |
| 1033 | * @param address the IP address of next hop router |
| 1034 | * @return Interface the Interface which has longest matchable IP prefix |
| 1035 | */ |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 1036 | private Interface longestInterfacePrefixMatch(InetAddress address) { |
| 1037 | Prefix prefixToSearchFor = new Prefix(address.getAddress(), |
| 1038 | Prefix.MAX_PREFIX_LENGTH); |
| 1039 | Iterator<Interface> it = |
| 1040 | interfaceRoutes.getValuesForKeysPrefixing( |
| 1041 | prefixToSearchFor.toBinaryString()).iterator(); |
| 1042 | Interface intf = null; |
| 1043 | // Find the last prefix, which will be the longest prefix |
| 1044 | while (it.hasNext()) { |
| 1045 | intf = it.next(); |
| 1046 | } |
| 1047 | |
| 1048 | return intf; |
| 1049 | } |
| 1050 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1051 | /* |
| 1052 | * IConfigInfoService methods |
| 1053 | */ |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1054 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1055 | @Override |
| 1056 | public boolean isInterfaceAddress(InetAddress address) { |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 1057 | Interface intf = longestInterfacePrefixMatch(address); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1058 | return (intf != null && intf.getIpAddress().equals(address)); |
| 1059 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1060 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1061 | @Override |
| 1062 | public boolean inConnectedNetwork(InetAddress address) { |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 1063 | Interface intf = longestInterfacePrefixMatch(address); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1064 | return (intf != null && !intf.getIpAddress().equals(address)); |
| 1065 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1066 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1067 | @Override |
| 1068 | public boolean fromExternalNetwork(long inDpid, short inPort) { |
| 1069 | for (Interface intf : interfaces.values()) { |
| 1070 | if (intf.getDpid() == inDpid && intf.getPort() == inPort) { |
| 1071 | return true; |
| 1072 | } |
| 1073 | } |
| 1074 | return false; |
| 1075 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1076 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 1077 | /** |
| 1078 | * To find the relative egress Interface for a next hop IP address. |
| 1079 | * |
| 1080 | * @param dstIpAddress the IP address of next hop router |
| 1081 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1082 | @Override |
| 1083 | public Interface getOutgoingInterface(InetAddress dstIpAddress) { |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 1084 | return longestInterfacePrefixMatch(dstIpAddress); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1085 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1086 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1087 | @Override |
| 1088 | public boolean hasLayer3Configuration() { |
| 1089 | return !interfaces.isEmpty(); |
| 1090 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1091 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1092 | @Override |
| 1093 | public MACAddress getRouterMacAddress() { |
| 1094 | return bgpdMacAddress; |
| 1095 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1096 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1097 | @Override |
| 1098 | public short getVlan() { |
| 1099 | return vlan; |
| 1100 | } |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 1101 | |
pingping-lin | 0426dee | 2014-08-27 15:03:17 -0700 | [diff] [blame] | 1102 | @Override |
| 1103 | public Set<SwitchPort> getExternalSwitchPorts() { |
| 1104 | return Collections.unmodifiableSet(externalNetworkSwitchPorts); |
| 1105 | } |
| 1106 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 1107 | } |