Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 16 | package org.onlab.onos.sdnip; |
| 17 | |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 18 | import java.util.ArrayList; |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 19 | import java.util.List; |
| 20 | |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 21 | import org.onlab.onos.core.ApplicationId; |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 22 | import org.onlab.onos.net.ConnectPoint; |
| 23 | import org.onlab.onos.net.flow.DefaultTrafficSelector; |
| 24 | import org.onlab.onos.net.flow.DefaultTrafficTreatment; |
| 25 | import org.onlab.onos.net.flow.TrafficSelector; |
| 26 | import org.onlab.onos.net.flow.TrafficTreatment; |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 27 | import org.onlab.onos.net.intent.Intent; |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 28 | import org.onlab.onos.net.intent.IntentService; |
| 29 | import org.onlab.onos.net.intent.PointToPointIntent; |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 30 | import org.onlab.onos.sdnip.bgp.BgpConstants; |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 31 | import org.onlab.onos.sdnip.config.BgpPeer; |
| 32 | import org.onlab.onos.sdnip.config.BgpSpeaker; |
| 33 | import org.onlab.onos.sdnip.config.Interface; |
| 34 | import org.onlab.onos.sdnip.config.InterfaceAddress; |
| 35 | import org.onlab.onos.sdnip.config.SdnIpConfigService; |
| 36 | import org.onlab.packet.Ethernet; |
| 37 | import org.onlab.packet.IPv4; |
| 38 | import org.onlab.packet.IpAddress; |
| 39 | import org.onlab.packet.IpPrefix; |
| 40 | import org.slf4j.Logger; |
| 41 | import org.slf4j.LoggerFactory; |
| 42 | |
| 43 | /** |
| 44 | * Manages the connectivity requirements between peers. |
| 45 | */ |
Jonathan Hart | ce430a4 | 2014-10-16 20:44:29 -0700 | [diff] [blame] | 46 | public class PeerConnectivityManager { |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 47 | |
| 48 | private static final Logger log = LoggerFactory.getLogger( |
Jonathan Hart | ce430a4 | 2014-10-16 20:44:29 -0700 | [diff] [blame] | 49 | PeerConnectivityManager.class); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 50 | |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 51 | private final SdnIpConfigService configService; |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 52 | private final InterfaceService interfaceService; |
| 53 | private final IntentService intentService; |
| 54 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 55 | private final ApplicationId appId; |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 56 | |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 57 | /** |
| 58 | * Creates a new PeerConnectivityManager. |
| 59 | * |
| 60 | * @param appId the application ID |
| 61 | * @param configService the SDN-IP config service |
| 62 | * @param interfaceService the interface service |
| 63 | * @param intentService the intent service |
| 64 | */ |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 65 | public PeerConnectivityManager(ApplicationId appId, |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 66 | SdnIpConfigService configService, |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 67 | InterfaceService interfaceService, |
| 68 | IntentService intentService) { |
| 69 | this.appId = appId; |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 70 | this.configService = configService; |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 71 | this.interfaceService = interfaceService; |
| 72 | this.intentService = intentService; |
| 73 | } |
| 74 | |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 75 | /** |
| 76 | * Starts the peer connectivity manager. |
| 77 | */ |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 78 | public void start() { |
| 79 | // TODO are any of these errors? |
| 80 | if (interfaceService.getInterfaces().isEmpty()) { |
| 81 | |
| 82 | log.warn("The interface in configuration file is empty. " |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 83 | + "Thus, the SDN-IP application can not be started."); |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 84 | } else if (configService.getBgpPeers().isEmpty()) { |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 85 | |
| 86 | log.warn("The BGP peer in configuration file is empty." |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 87 | + "Thus, the SDN-IP application can not be started."); |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 88 | } else if (configService.getBgpSpeakers() == null) { |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 89 | |
| 90 | log.error("The BGP speaker in configuration file is empty. " |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 91 | + "Thus, the SDN-IP application can not be started."); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 92 | return; |
| 93 | } |
| 94 | |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 95 | setUpConnectivity(); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /** |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 99 | * Stops the peer connectivity manager. |
| 100 | */ |
| 101 | public void stop() { |
| 102 | // TODO: Implement it |
| 103 | } |
| 104 | |
| 105 | /** |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 106 | * Sets up paths to establish connectivity between all internal |
| 107 | * {@link BgpSpeaker}s and all external {@link BgpPeer}s. |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 108 | */ |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 109 | private void setUpConnectivity() { |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 110 | for (BgpSpeaker bgpSpeaker : configService.getBgpSpeakers() |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 111 | .values()) { |
| 112 | log.debug("Start to set up BGP paths for BGP speaker: {}", |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 113 | bgpSpeaker); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 114 | |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 115 | for (BgpPeer bgpPeer : configService.getBgpPeers().values()) { |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 116 | |
| 117 | log.debug("Start to set up BGP paths between BGP speaker: {} " |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 118 | + "to BGP peer: {}", bgpSpeaker, bgpPeer); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 119 | |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 120 | buildPeerIntents(bgpSpeaker, bgpPeer); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /** |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 126 | * Builds the required intents between a given internal BGP speaker and |
| 127 | * external BGP peer. |
| 128 | * |
| 129 | * @param bgpSpeaker the BGP speaker |
| 130 | * @param bgpPeer the BGP peer |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 131 | */ |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 132 | private void buildPeerIntents(BgpSpeaker bgpSpeaker, BgpPeer bgpPeer) { |
| 133 | List<Intent> intents = new ArrayList<Intent>(); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 134 | |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 135 | ConnectPoint bgpdConnectPoint = bgpSpeaker.connectPoint(); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 136 | |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 137 | List<InterfaceAddress> interfaceAddresses = |
| 138 | bgpSpeaker.interfaceAddresses(); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 139 | |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 140 | Interface peerInterface = interfaceService.getInterface( |
| 141 | bgpPeer.connectPoint()); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 142 | |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 143 | if (peerInterface == null) { |
| 144 | log.error("No interface found for peer {}", bgpPeer.ipAddress()); |
| 145 | return; |
| 146 | } |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 147 | |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 148 | IpAddress bgpdAddress = null; |
| 149 | for (InterfaceAddress interfaceAddress : interfaceAddresses) { |
| 150 | if (interfaceAddress.connectPoint().equals( |
| 151 | peerInterface.connectPoint())) { |
| 152 | bgpdAddress = interfaceAddress.ipAddress(); |
| 153 | break; |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 154 | } |
| 155 | } |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 156 | if (bgpdAddress == null) { |
| 157 | log.debug("No IP address found for peer {} on interface {}", |
| 158 | bgpPeer, bgpPeer.connectPoint()); |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | IpAddress bgpdPeerAddress = bgpPeer.ipAddress(); |
| 163 | ConnectPoint bgpdPeerConnectPoint = peerInterface.connectPoint(); |
| 164 | |
| 165 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 166 | .build(); |
| 167 | |
| 168 | TrafficSelector selector; |
| 169 | |
| 170 | // install intent for BGP path from BGPd to BGP peer matching |
| 171 | // destination TCP port 179 |
| 172 | selector = buildSelector(IPv4.PROTOCOL_TCP, |
| 173 | bgpdAddress, |
| 174 | bgpdPeerAddress, |
| 175 | null, |
| 176 | (short) BgpConstants.BGP_PORT); |
| 177 | |
| 178 | intents.add(new PointToPointIntent(appId, selector, treatment, |
| 179 | bgpdConnectPoint, bgpdPeerConnectPoint)); |
| 180 | |
| 181 | // install intent for BGP path from BGPd to BGP peer matching |
| 182 | // source TCP port 179 |
| 183 | selector = buildSelector(IPv4.PROTOCOL_TCP, |
| 184 | bgpdAddress, |
| 185 | bgpdPeerAddress, |
| 186 | (short) BgpConstants.BGP_PORT, |
| 187 | null); |
| 188 | |
| 189 | intents.add(new PointToPointIntent(appId, selector, treatment, |
| 190 | bgpdConnectPoint, bgpdPeerConnectPoint)); |
| 191 | |
| 192 | // install intent for reversed BGP path from BGP peer to BGPd |
| 193 | // matching destination TCP port 179 |
| 194 | selector = buildSelector(IPv4.PROTOCOL_TCP, |
| 195 | bgpdPeerAddress, |
| 196 | bgpdAddress, |
| 197 | null, |
| 198 | (short) BgpConstants.BGP_PORT); |
| 199 | |
| 200 | intents.add(new PointToPointIntent(appId, selector, treatment, |
| 201 | bgpdPeerConnectPoint, bgpdConnectPoint)); |
| 202 | |
| 203 | // install intent for reversed BGP path from BGP peer to BGPd |
| 204 | // matching source TCP port 179 |
| 205 | selector = buildSelector(IPv4.PROTOCOL_TCP, |
| 206 | bgpdPeerAddress, |
| 207 | bgpdAddress, |
| 208 | (short) BgpConstants.BGP_PORT, |
| 209 | null); |
| 210 | |
| 211 | intents.add(new PointToPointIntent(appId, selector, treatment, |
| 212 | bgpdPeerConnectPoint, bgpdConnectPoint)); |
| 213 | |
| 214 | // install intent for ICMP path from BGPd to BGP peer |
| 215 | selector = buildSelector(IPv4.PROTOCOL_ICMP, |
| 216 | bgpdAddress, |
| 217 | bgpdPeerAddress, |
| 218 | null, |
| 219 | null); |
| 220 | |
| 221 | intents.add(new PointToPointIntent(appId, selector, treatment, |
| 222 | bgpdConnectPoint, bgpdPeerConnectPoint)); |
| 223 | |
| 224 | // install intent for reversed ICMP path from BGP peer to BGPd |
| 225 | selector = buildSelector(IPv4.PROTOCOL_ICMP, |
| 226 | bgpdPeerAddress, |
| 227 | bgpdAddress, |
| 228 | null, |
| 229 | null); |
| 230 | |
| 231 | intents.add(new PointToPointIntent(appId, selector, treatment, |
| 232 | bgpdPeerConnectPoint, bgpdConnectPoint)); |
| 233 | |
| 234 | // Submit all the intents. |
| 235 | // TODO submit as a batch |
| 236 | for (Intent intent : intents) { |
| 237 | intentService.submit(intent); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Builds a traffic selector based on the set of input parameters. |
| 243 | * |
| 244 | * @param ipProto IP protocol |
| 245 | * @param srcIp source IP address |
| 246 | * @param dstIp destination IP address |
| 247 | * @param srcTcpPort source TCP port, or null if shouldn't be set |
| 248 | * @param dstTcpPort destination TCP port, or null if shouldn't be set |
| 249 | * @return the new traffic selector |
| 250 | */ |
| 251 | private TrafficSelector buildSelector(byte ipProto, IpAddress srcIp, |
| 252 | IpAddress dstIp, Short srcTcpPort, Short dstTcpPort) { |
| 253 | TrafficSelector.Builder builder = DefaultTrafficSelector.builder() |
| 254 | .matchEthType(Ethernet.TYPE_IPV4) |
| 255 | .matchIPProtocol(ipProto) |
| 256 | .matchIPSrc(IpPrefix.valueOf(srcIp, |
| 257 | IpPrefix.MAX_INET_MASK_LENGTH)) |
| 258 | .matchIPDst(IpPrefix.valueOf(dstIp, |
| 259 | IpPrefix.MAX_INET_MASK_LENGTH)); |
| 260 | |
| 261 | if (srcTcpPort != null) { |
| 262 | builder.matchTcpSrc(srcTcpPort); |
| 263 | } |
| 264 | |
| 265 | if (dstTcpPort != null) { |
| 266 | builder.matchTcpDst(dstTcpPort); |
| 267 | } |
| 268 | |
| 269 | return builder.build(); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 272 | } |