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