Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 1 | package org.onlab.onos.sdnip; |
| 2 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 3 | import org.onlab.onos.ApplicationId; |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 4 | import org.onlab.onos.net.ConnectPoint; |
| 5 | import org.onlab.onos.net.flow.DefaultTrafficSelector; |
| 6 | import org.onlab.onos.net.flow.DefaultTrafficTreatment; |
| 7 | import org.onlab.onos.net.flow.TrafficSelector; |
| 8 | import org.onlab.onos.net.flow.TrafficTreatment; |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 9 | import org.onlab.onos.net.intent.IntentService; |
| 10 | import org.onlab.onos.net.intent.PointToPointIntent; |
| 11 | import org.onlab.onos.sdnip.config.BgpPeer; |
| 12 | import org.onlab.onos.sdnip.config.BgpSpeaker; |
| 13 | import org.onlab.onos.sdnip.config.Interface; |
| 14 | import org.onlab.onos.sdnip.config.InterfaceAddress; |
| 15 | import org.onlab.onos.sdnip.config.SdnIpConfigService; |
| 16 | import org.onlab.packet.Ethernet; |
| 17 | import org.onlab.packet.IPv4; |
| 18 | import org.onlab.packet.IpAddress; |
| 19 | import org.onlab.packet.IpPrefix; |
| 20 | import org.slf4j.Logger; |
| 21 | import org.slf4j.LoggerFactory; |
| 22 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 23 | import java.util.List; |
| 24 | |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 25 | /** |
| 26 | * Manages the connectivity requirements between peers. |
| 27 | */ |
Jonathan Hart | ce430a4 | 2014-10-16 20:44:29 -0700 | [diff] [blame] | 28 | public class PeerConnectivityManager { |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 29 | |
| 30 | private static final Logger log = LoggerFactory.getLogger( |
Jonathan Hart | ce430a4 | 2014-10-16 20:44:29 -0700 | [diff] [blame] | 31 | PeerConnectivityManager.class); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 32 | |
| 33 | // TODO these shouldn't be defined here |
| 34 | private static final short BGP_PORT = 179; |
| 35 | private static final int IPV4_BIT_LENGTH = 32; |
| 36 | |
| 37 | private final SdnIpConfigService configInfoService; |
| 38 | private final InterfaceService interfaceService; |
| 39 | private final IntentService intentService; |
| 40 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 41 | private final ApplicationId appId; |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 42 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 43 | public PeerConnectivityManager(ApplicationId appId, |
| 44 | SdnIpConfigService configInfoService, |
| 45 | InterfaceService interfaceService, |
| 46 | IntentService intentService) { |
| 47 | this.appId = appId; |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 48 | this.configInfoService = configInfoService; |
| 49 | this.interfaceService = interfaceService; |
| 50 | this.intentService = intentService; |
| 51 | } |
| 52 | |
| 53 | public void start() { |
| 54 | // TODO are any of these errors? |
| 55 | if (interfaceService.getInterfaces().isEmpty()) { |
| 56 | |
| 57 | log.warn("The interface in configuration file is empty. " |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 58 | + "Thus, the SDN-IP application can not be started."); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 59 | } else if (configInfoService.getBgpPeers().isEmpty()) { |
| 60 | |
| 61 | log.warn("The BGP peer in configuration file is empty." |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 62 | + "Thus, the SDN-IP application can not be started."); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 63 | } else if (configInfoService.getBgpSpeakers() == null) { |
| 64 | |
| 65 | log.error("The BGP speaker in configuration file is empty. " |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 66 | + "Thus, the SDN-IP application can not be started."); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 67 | return; |
| 68 | } |
| 69 | |
| 70 | setupBgpPaths(); |
| 71 | setupIcmpPaths(); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Sets up paths for all {@link BgpSpeaker}s and all external peers. |
| 76 | * <p/> |
| 77 | * Run a loop for all BGP speakers and a loop for all BGP peers outside. |
| 78 | * Push intents for paths from each BGP speaker to all peers. Push intents |
| 79 | * for paths from all peers to each BGP speaker. |
| 80 | */ |
| 81 | private void setupBgpPaths() { |
| 82 | for (BgpSpeaker bgpSpeaker : configInfoService.getBgpSpeakers() |
| 83 | .values()) { |
| 84 | log.debug("Start to set up BGP paths for BGP speaker: {}", |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 85 | bgpSpeaker); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 86 | ConnectPoint bgpdConnectPoint = bgpSpeaker.connectPoint(); |
| 87 | |
| 88 | List<InterfaceAddress> interfaceAddresses = |
| 89 | bgpSpeaker.interfaceAddresses(); |
| 90 | |
| 91 | for (BgpPeer bgpPeer : configInfoService.getBgpPeers().values()) { |
| 92 | |
| 93 | log.debug("Start to set up BGP paths between BGP speaker: {} " |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 94 | + "to BGP peer: {}", bgpSpeaker, bgpPeer); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 95 | |
| 96 | Interface peerInterface = interfaceService.getInterface( |
| 97 | bgpPeer.connectPoint()); |
| 98 | if (peerInterface == null) { |
| 99 | log.error("Can not find the corresponding Interface from " |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 100 | + "configuration for BGP peer {}", |
| 101 | bgpPeer.ipAddress()); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 102 | continue; |
| 103 | } |
| 104 | |
| 105 | IpAddress bgpdAddress = null; |
| 106 | for (InterfaceAddress interfaceAddress : interfaceAddresses) { |
| 107 | if (interfaceAddress.connectPoint().equals( |
| 108 | peerInterface.connectPoint())) { |
| 109 | bgpdAddress = interfaceAddress.ipAddress(); |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | if (bgpdAddress == null) { |
| 114 | log.debug("There is no interface IP address for bgpPeer: {}" |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 115 | + " on interface {}", bgpPeer, bgpPeer.connectPoint()); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 116 | return; |
| 117 | } |
| 118 | |
| 119 | IpAddress bgpdPeerAddress = bgpPeer.ipAddress(); |
| 120 | ConnectPoint bgpdPeerConnectPoint = peerInterface.connectPoint(); |
| 121 | |
| 122 | // install intent for BGP path from BGPd to BGP peer matching |
| 123 | // destination TCP port 179 |
| 124 | |
| 125 | // TODO: The usage of PacketMatchBuilder will be improved, then we |
| 126 | // only need to new the PacketMatchBuilder once. |
| 127 | // By then, the code here will be improved accordingly. |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 128 | TrafficSelector selector = DefaultTrafficSelector.builder() |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 129 | .matchEthType(Ethernet.TYPE_IPV4) |
| 130 | .matchIPProtocol(IPv4.PROTOCOL_TCP) |
Jonathan Hart | bcae7bd | 2014-10-16 10:24:41 -0700 | [diff] [blame] | 131 | .matchIPSrc(IpPrefix.valueOf(bgpdAddress.toInt(), IPV4_BIT_LENGTH)) |
| 132 | .matchIPDst(IpPrefix.valueOf(bgpdPeerAddress.toInt(), IPV4_BIT_LENGTH)) |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 133 | .matchTcpDst(BGP_PORT) |
| 134 | .build(); |
| 135 | |
| 136 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 137 | .build(); |
| 138 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 139 | PointToPointIntent intentMatchDstTcpPort = |
| 140 | new PointToPointIntent(appId, selector, treatment, |
| 141 | bgpdConnectPoint, bgpdPeerConnectPoint); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 142 | intentService.submit(intentMatchDstTcpPort); |
| 143 | log.debug("Submitted BGP path intent matching dst TCP port 179 " |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 144 | + "from BGPd {} to peer {}: {}", |
| 145 | bgpdAddress, bgpdPeerAddress, intentMatchDstTcpPort); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 146 | |
| 147 | // install intent for BGP path from BGPd to BGP peer matching |
| 148 | // source TCP port 179 |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 149 | selector = DefaultTrafficSelector.builder() |
| 150 | .matchEthType(Ethernet.TYPE_IPV4) |
| 151 | .matchIPProtocol(IPv4.PROTOCOL_TCP) |
Jonathan Hart | bcae7bd | 2014-10-16 10:24:41 -0700 | [diff] [blame] | 152 | .matchIPSrc(IpPrefix.valueOf(bgpdAddress.toInt(), IPV4_BIT_LENGTH)) |
| 153 | .matchIPDst(IpPrefix.valueOf(bgpdPeerAddress.toInt(), IPV4_BIT_LENGTH)) |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 154 | .matchTcpSrc(BGP_PORT) |
| 155 | .build(); |
| 156 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 157 | PointToPointIntent intentMatchSrcTcpPort = |
| 158 | new PointToPointIntent(appId, selector, treatment, |
| 159 | bgpdConnectPoint, bgpdPeerConnectPoint); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 160 | intentService.submit(intentMatchSrcTcpPort); |
| 161 | log.debug("Submitted BGP path intent matching src TCP port 179" |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 162 | + "from BGPd {} to peer {}: {}", |
| 163 | bgpdAddress, bgpdPeerAddress, intentMatchSrcTcpPort); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 164 | |
| 165 | // install intent for reversed BGP path from BGP peer to BGPd |
| 166 | // matching destination TCP port 179 |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 167 | selector = DefaultTrafficSelector.builder() |
| 168 | .matchEthType(Ethernet.TYPE_IPV4) |
| 169 | .matchIPProtocol(IPv4.PROTOCOL_TCP) |
Jonathan Hart | bcae7bd | 2014-10-16 10:24:41 -0700 | [diff] [blame] | 170 | .matchIPSrc(IpPrefix.valueOf(bgpdPeerAddress.toInt(), IPV4_BIT_LENGTH)) |
| 171 | .matchIPDst(IpPrefix.valueOf(bgpdAddress.toInt(), IPV4_BIT_LENGTH)) |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 172 | .matchTcpDst(BGP_PORT) |
| 173 | .build(); |
| 174 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 175 | PointToPointIntent reversedIntentMatchDstTcpPort = |
| 176 | new PointToPointIntent(appId, selector, treatment, |
| 177 | bgpdPeerConnectPoint, bgpdConnectPoint); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 178 | intentService.submit(reversedIntentMatchDstTcpPort); |
| 179 | log.debug("Submitted BGP path intent matching dst TCP port 179" |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 180 | + "from BGP peer {} to BGPd {} : {}", |
| 181 | bgpdPeerAddress, bgpdAddress, reversedIntentMatchDstTcpPort); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 182 | |
| 183 | // install intent for reversed BGP path from BGP peer to BGPd |
| 184 | // matching source TCP port 179 |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 185 | selector = DefaultTrafficSelector.builder() |
| 186 | .matchEthType(Ethernet.TYPE_IPV4) |
| 187 | .matchIPProtocol(IPv4.PROTOCOL_TCP) |
Jonathan Hart | bcae7bd | 2014-10-16 10:24:41 -0700 | [diff] [blame] | 188 | .matchIPSrc(IpPrefix.valueOf(bgpdPeerAddress.toInt(), IPV4_BIT_LENGTH)) |
| 189 | .matchIPDst(IpPrefix.valueOf(bgpdAddress.toInt(), IPV4_BIT_LENGTH)) |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 190 | .matchTcpSrc(BGP_PORT) |
| 191 | .build(); |
| 192 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 193 | PointToPointIntent reversedIntentMatchSrcTcpPort = |
| 194 | new PointToPointIntent(appId, selector, treatment, |
| 195 | bgpdPeerConnectPoint, bgpdConnectPoint); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 196 | intentService.submit(reversedIntentMatchSrcTcpPort); |
| 197 | log.debug("Submitted BGP path intent matching src TCP port 179" |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 198 | + "from BGP peer {} to BGPd {} : {}", |
| 199 | bgpdPeerAddress, bgpdAddress, reversedIntentMatchSrcTcpPort); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 200 | |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Sets up ICMP paths between each {@link BgpSpeaker} and all BGP peers |
| 207 | * located in other external networks. |
| 208 | * <p/> |
| 209 | * Run a loop for all BGP speakers and a loop for all BGP Peers. Push |
| 210 | * intents for paths from each BGP speaker to all peers. Push intents |
| 211 | * for paths from all peers to each BGP speaker. |
| 212 | */ |
| 213 | private void setupIcmpPaths() { |
| 214 | for (BgpSpeaker bgpSpeaker : configInfoService.getBgpSpeakers() |
| 215 | .values()) { |
| 216 | log.debug("Start to set up ICMP paths for BGP speaker: {}", |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 217 | bgpSpeaker); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 218 | ConnectPoint bgpdConnectPoint = bgpSpeaker.connectPoint(); |
| 219 | List<InterfaceAddress> interfaceAddresses = bgpSpeaker |
| 220 | .interfaceAddresses(); |
| 221 | |
| 222 | for (BgpPeer bgpPeer : configInfoService.getBgpPeers().values()) { |
| 223 | |
| 224 | Interface peerInterface = interfaceService.getInterface( |
| 225 | bgpPeer.connectPoint()); |
| 226 | |
| 227 | if (peerInterface == null) { |
| 228 | log.error("Can not find the corresponding Interface from " |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 229 | + "configuration for BGP peer {}", |
| 230 | bgpPeer.ipAddress()); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 231 | continue; |
| 232 | } |
| 233 | IpAddress bgpdAddress = null; |
| 234 | for (InterfaceAddress interfaceAddress : interfaceAddresses) { |
| 235 | if (interfaceAddress.connectPoint().equals( |
| 236 | peerInterface.connectPoint())) { |
| 237 | bgpdAddress = interfaceAddress.ipAddress(); |
| 238 | break; |
| 239 | } |
| 240 | |
| 241 | } |
| 242 | if (bgpdAddress == null) { |
| 243 | log.debug("There is no IP address for bgpPeer: {} on " |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 244 | + "interface port: {}", bgpPeer, |
| 245 | bgpPeer.connectPoint()); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 246 | return; |
| 247 | } |
| 248 | |
| 249 | IpAddress bgpdPeerAddress = bgpPeer.ipAddress(); |
| 250 | ConnectPoint bgpdPeerConnectPoint = peerInterface.connectPoint(); |
| 251 | |
| 252 | // install intent for ICMP path from BGPd to BGP peer |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 253 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 254 | .matchEthType(Ethernet.TYPE_IPV4) |
| 255 | .matchIPProtocol(IPv4.PROTOCOL_ICMP) |
Jonathan Hart | bcae7bd | 2014-10-16 10:24:41 -0700 | [diff] [blame] | 256 | .matchIPSrc(IpPrefix.valueOf(bgpdAddress.toInt(), IPV4_BIT_LENGTH)) |
| 257 | .matchIPDst(IpPrefix.valueOf(bgpdPeerAddress.toInt(), IPV4_BIT_LENGTH)) |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 258 | .build(); |
| 259 | |
| 260 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 261 | .build(); |
| 262 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 263 | PointToPointIntent intent = |
| 264 | new PointToPointIntent(appId, selector, treatment, |
| 265 | bgpdConnectPoint, bgpdPeerConnectPoint); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 266 | intentService.submit(intent); |
| 267 | log.debug("Submitted ICMP path intent from BGPd {} to peer {} :" |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 268 | + " {}", bgpdAddress, bgpdPeerAddress, intent); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 269 | |
| 270 | // install intent for reversed ICMP path from BGP peer to BGPd |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 271 | selector = DefaultTrafficSelector.builder() |
| 272 | .matchEthType(Ethernet.TYPE_IPV4) |
| 273 | .matchIPProtocol(IPv4.PROTOCOL_ICMP) |
Jonathan Hart | bcae7bd | 2014-10-16 10:24:41 -0700 | [diff] [blame] | 274 | .matchIPSrc(IpPrefix.valueOf(bgpdPeerAddress.toInt(), IPV4_BIT_LENGTH)) |
| 275 | .matchIPDst(IpPrefix.valueOf(bgpdAddress.toInt(), IPV4_BIT_LENGTH)) |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 276 | .build(); |
| 277 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 278 | PointToPointIntent reversedIntent = |
| 279 | new PointToPointIntent(appId, selector, treatment, |
| 280 | bgpdPeerConnectPoint, bgpdConnectPoint); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 281 | intentService.submit(reversedIntent); |
| 282 | log.debug("Submitted ICMP path intent from BGP peer {} to BGPd" |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 283 | + " {} : {}", |
| 284 | bgpdPeerAddress, bgpdAddress, reversedIntent); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 289 | } |