Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
| 16 | package org.onosproject.bgprouter; |
| 17 | |
Saurav Das | 3d03826 | 2015-04-23 12:36:58 -0700 | [diff] [blame^] | 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 20 | import org.onlab.packet.Ethernet; |
| 21 | import org.onlab.packet.IPv4; |
| 22 | import org.onlab.packet.IpAddress; |
| 23 | import org.onlab.packet.TCP; |
| 24 | import org.onosproject.core.ApplicationId; |
| 25 | import org.onosproject.net.ConnectPoint; |
Jonathan Hart | 936a729 | 2015-03-06 18:02:57 -0800 | [diff] [blame] | 26 | import org.onosproject.net.flow.DefaultTrafficSelector; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 27 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
Jonathan Hart | 936a729 | 2015-03-06 18:02:57 -0800 | [diff] [blame] | 28 | import org.onosproject.net.flow.TrafficSelector; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 29 | import org.onosproject.net.flow.TrafficTreatment; |
Saurav Das | 3d03826 | 2015-04-23 12:36:58 -0700 | [diff] [blame^] | 30 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 31 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 32 | import org.onosproject.net.flowobjective.ForwardingObjective; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 33 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 34 | import org.onosproject.net.packet.OutboundPacket; |
| 35 | import org.onosproject.net.packet.PacketContext; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 36 | import org.onosproject.net.packet.PacketProcessor; |
| 37 | import org.onosproject.net.packet.PacketService; |
Jonathan Hart | 2da1e60 | 2015-02-18 19:09:24 -0800 | [diff] [blame] | 38 | import org.onosproject.routing.config.BgpPeer; |
| 39 | import org.onosproject.routing.config.BgpSpeaker; |
| 40 | import org.onosproject.routing.config.InterfaceAddress; |
| 41 | import org.onosproject.routing.config.RoutingConfigurationService; |
Saurav Das | 3d03826 | 2015-04-23 12:36:58 -0700 | [diff] [blame^] | 42 | import org.slf4j.Logger; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 43 | |
| 44 | |
| 45 | /** |
| 46 | * Manages connectivity between peers by tunnelling BGP traffic through |
| 47 | * OpenFlow packet-ins and packet-outs. |
| 48 | */ |
| 49 | public class TunnellingConnectivityManager { |
| 50 | |
| 51 | private static final short BGP_PORT = 179; |
Saurav Das | 3d03826 | 2015-04-23 12:36:58 -0700 | [diff] [blame^] | 52 | private final Logger log = getLogger(getClass()); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 53 | private final ApplicationId appId; |
| 54 | |
Jonathan Hart | 936a729 | 2015-03-06 18:02:57 -0800 | [diff] [blame] | 55 | private final BgpSpeaker bgpSpeaker; |
| 56 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 57 | private final PacketService packetService; |
| 58 | private final RoutingConfigurationService configService; |
Saurav Das | 3d03826 | 2015-04-23 12:36:58 -0700 | [diff] [blame^] | 59 | private final FlowObjectiveService flowObjectiveService; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 60 | |
| 61 | private final BgpProcessor processor = new BgpProcessor(); |
| 62 | |
| 63 | public TunnellingConnectivityManager(ApplicationId appId, |
| 64 | RoutingConfigurationService configService, |
Jonathan Hart | 936a729 | 2015-03-06 18:02:57 -0800 | [diff] [blame] | 65 | PacketService packetService, |
Saurav Das | 3d03826 | 2015-04-23 12:36:58 -0700 | [diff] [blame^] | 66 | FlowObjectiveService flowObjectiveService) { |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 67 | this.appId = appId; |
| 68 | this.configService = configService; |
| 69 | this.packetService = packetService; |
Saurav Das | 3d03826 | 2015-04-23 12:36:58 -0700 | [diff] [blame^] | 70 | this.flowObjectiveService = flowObjectiveService; |
Jonathan Hart | 936a729 | 2015-03-06 18:02:57 -0800 | [diff] [blame] | 71 | |
| 72 | BgpSpeaker bgpSpeaker = null; |
| 73 | for (BgpSpeaker speaker : configService.getBgpSpeakers().values()) { |
| 74 | bgpSpeaker = speaker; |
| 75 | break; |
| 76 | } |
| 77 | |
| 78 | if (bgpSpeaker == null) { |
| 79 | throw new IllegalArgumentException("Must have at least one BGP speaker configured"); |
| 80 | } |
| 81 | |
| 82 | this.bgpSpeaker = bgpSpeaker; |
| 83 | |
| 84 | TrafficSelector selectorDst = DefaultTrafficSelector.builder() |
| 85 | .matchEthType(Ethernet.TYPE_IPV4) |
| 86 | .matchIPProtocol(IPv4.PROTOCOL_TCP) |
| 87 | .matchTcpDst(BGP_PORT) |
| 88 | .build(); |
| 89 | |
| 90 | TrafficSelector selectorSrc = DefaultTrafficSelector.builder() |
| 91 | .matchEthType(Ethernet.TYPE_IPV4) |
| 92 | .matchIPProtocol(IPv4.PROTOCOL_TCP) |
| 93 | .matchTcpSrc(BGP_PORT) |
| 94 | .build(); |
| 95 | |
| 96 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 97 | .punt() |
| 98 | .build(); |
| 99 | |
Saurav Das | 3d03826 | 2015-04-23 12:36:58 -0700 | [diff] [blame^] | 100 | ForwardingObjective puntSrc = DefaultForwardingObjective.builder() |
| 101 | .fromApp(appId) |
| 102 | .makePermanent() |
| 103 | .withSelector(selectorSrc) |
| 104 | .withTreatment(treatment) |
| 105 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 106 | .add(); |
| 107 | flowObjectiveService.forward(bgpSpeaker.connectPoint().deviceId(), |
| 108 | puntSrc); |
| 109 | |
| 110 | ForwardingObjective puntDst = DefaultForwardingObjective.builder() |
| 111 | .fromApp(appId) |
| 112 | .makePermanent() |
| 113 | .withSelector(selectorDst) |
| 114 | .withTreatment(treatment) |
| 115 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 116 | .add(); |
| 117 | flowObjectiveService.forward(bgpSpeaker.connectPoint().deviceId(), |
| 118 | puntDst); |
| 119 | log.info("Sent punt forwarding objective to {}", bgpSpeaker.connectPoint().deviceId()); |
| 120 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | public void start() { |
| 124 | packetService.addProcessor(processor, PacketProcessor.ADVISOR_MAX + 3); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | public void stop() { |
| 128 | packetService.removeProcessor(processor); |
| 129 | // Should revoke packet requests in the future |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Forwards a BGP packet to another connect point. |
| 134 | * |
| 135 | * @param context the packet context of the incoming packet |
| 136 | */ |
| 137 | private void forward(PacketContext context) { |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 138 | ConnectPoint outputPort = null; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 139 | |
| 140 | IPv4 ipv4 = (IPv4) context.inPacket().parsed().getPayload(); |
| 141 | IpAddress dstAddress = IpAddress.valueOf(ipv4.getDestinationAddress()); |
| 142 | |
Jonathan Hart | 936a729 | 2015-03-06 18:02:57 -0800 | [diff] [blame] | 143 | if (context.inPacket().receivedFrom().equals(bgpSpeaker.connectPoint())) { |
| 144 | BgpPeer peer = configService.getBgpPeers().get(dstAddress); |
| 145 | if (peer != null) { |
| 146 | outputPort = peer.connectPoint(); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 147 | } |
Jonathan Hart | 936a729 | 2015-03-06 18:02:57 -0800 | [diff] [blame] | 148 | } |
| 149 | for (InterfaceAddress addr : bgpSpeaker.interfaceAddresses()) { |
| 150 | if (addr.ipAddress().equals(dstAddress) && !context.inPacket() |
| 151 | .receivedFrom().equals(bgpSpeaker.connectPoint())) { |
| 152 | outputPort = bgpSpeaker.connectPoint(); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
| 156 | if (outputPort != null) { |
| 157 | TrafficTreatment t = DefaultTrafficTreatment.builder() |
| 158 | .setOutput(outputPort.port()).build(); |
| 159 | OutboundPacket o = new DefaultOutboundPacket( |
| 160 | outputPort.deviceId(), t, context.inPacket().unparsed()); |
| 161 | packetService.emit(o); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Packet processor responsible receiving and filtering BGP packets. |
| 167 | */ |
| 168 | private class BgpProcessor implements PacketProcessor { |
| 169 | |
| 170 | @Override |
| 171 | public void process(PacketContext context) { |
| 172 | // Stop processing if the packet has been handled, since we |
| 173 | // can't do any more to it. |
| 174 | if (context.isHandled()) { |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | Ethernet packet = context.inPacket().parsed(); |
| 179 | |
| 180 | if (packet == null) { |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | if (packet.getEtherType() == Ethernet.TYPE_IPV4) { |
| 185 | IPv4 ipv4Packet = (IPv4) packet.getPayload(); |
| 186 | if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_TCP) { |
| 187 | TCP tcpPacket = (TCP) ipv4Packet.getPayload(); |
| 188 | |
| 189 | if (tcpPacket.getDestinationPort() == BGP_PORT || |
| 190 | tcpPacket.getSourcePort() == BGP_PORT) { |
| 191 | forward(context); |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | } |