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