blob: 7d8ca7c2c2b69e85661d5352d94c7b3d06e51726 [file] [log] [blame]
Jonathan Hartf5829202015-02-12 09:37:02 -08001/*
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 */
16package org.onosproject.bgprouter;
17
Saurav Das3d038262015-04-23 12:36:58 -070018import static org.slf4j.LoggerFactory.getLogger;
19
Jonathan Hartf5829202015-02-12 09:37:02 -080020import org.onlab.packet.Ethernet;
21import org.onlab.packet.IPv4;
22import org.onlab.packet.IpAddress;
23import org.onlab.packet.TCP;
24import org.onosproject.core.ApplicationId;
25import org.onosproject.net.ConnectPoint;
Jonathan Hart936a7292015-03-06 18:02:57 -080026import org.onosproject.net.flow.DefaultTrafficSelector;
Jonathan Hartf5829202015-02-12 09:37:02 -080027import org.onosproject.net.flow.DefaultTrafficTreatment;
Jonathan Hart936a7292015-03-06 18:02:57 -080028import org.onosproject.net.flow.TrafficSelector;
Jonathan Hartf5829202015-02-12 09:37:02 -080029import org.onosproject.net.flow.TrafficTreatment;
Saurav Das3d038262015-04-23 12:36:58 -070030import org.onosproject.net.flowobjective.DefaultForwardingObjective;
31import org.onosproject.net.flowobjective.FlowObjectiveService;
32import org.onosproject.net.flowobjective.ForwardingObjective;
Jonathan Hartf5829202015-02-12 09:37:02 -080033import org.onosproject.net.packet.DefaultOutboundPacket;
34import org.onosproject.net.packet.OutboundPacket;
35import org.onosproject.net.packet.PacketContext;
Jonathan Hartf5829202015-02-12 09:37:02 -080036import org.onosproject.net.packet.PacketProcessor;
37import org.onosproject.net.packet.PacketService;
Jonathan Hart2da1e602015-02-18 19:09:24 -080038import org.onosproject.routing.config.BgpPeer;
39import org.onosproject.routing.config.BgpSpeaker;
40import org.onosproject.routing.config.InterfaceAddress;
41import org.onosproject.routing.config.RoutingConfigurationService;
Saurav Das3d038262015-04-23 12:36:58 -070042import org.slf4j.Logger;
Jonathan Hartf5829202015-02-12 09:37:02 -080043
44
45/**
46 * Manages connectivity between peers by tunnelling BGP traffic through
47 * OpenFlow packet-ins and packet-outs.
48 */
49public class TunnellingConnectivityManager {
50
51 private static final short BGP_PORT = 179;
Saurav Das3d038262015-04-23 12:36:58 -070052 private final Logger log = getLogger(getClass());
Jonathan Hartf5829202015-02-12 09:37:02 -080053 private final ApplicationId appId;
54
Jonathan Hart936a7292015-03-06 18:02:57 -080055 private final BgpSpeaker bgpSpeaker;
56
Jonathan Hartf5829202015-02-12 09:37:02 -080057 private final PacketService packetService;
58 private final RoutingConfigurationService configService;
Saurav Das3d038262015-04-23 12:36:58 -070059 private final FlowObjectiveService flowObjectiveService;
Jonathan Hartf5829202015-02-12 09:37:02 -080060
61 private final BgpProcessor processor = new BgpProcessor();
62
63 public TunnellingConnectivityManager(ApplicationId appId,
64 RoutingConfigurationService configService,
Jonathan Hart936a7292015-03-06 18:02:57 -080065 PacketService packetService,
Saurav Das3d038262015-04-23 12:36:58 -070066 FlowObjectiveService flowObjectiveService) {
Jonathan Hartf5829202015-02-12 09:37:02 -080067 this.appId = appId;
68 this.configService = configService;
69 this.packetService = packetService;
Saurav Das3d038262015-04-23 12:36:58 -070070 this.flowObjectiveService = flowObjectiveService;
Jonathan Hart936a7292015-03-06 18:02:57 -080071
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 Das3d038262015-04-23 12:36:58 -0700100 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 Hartf5829202015-02-12 09:37:02 -0800121 }
122
123 public void start() {
124 packetService.addProcessor(processor, PacketProcessor.ADVISOR_MAX + 3);
Jonathan Hartf5829202015-02-12 09:37:02 -0800125 }
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 Hartf5829202015-02-12 09:37:02 -0800138 ConnectPoint outputPort = null;
Jonathan Hartf5829202015-02-12 09:37:02 -0800139
140 IPv4 ipv4 = (IPv4) context.inPacket().parsed().getPayload();
141 IpAddress dstAddress = IpAddress.valueOf(ipv4.getDestinationAddress());
142
Jonathan Hart936a7292015-03-06 18:02:57 -0800143 if (context.inPacket().receivedFrom().equals(bgpSpeaker.connectPoint())) {
144 BgpPeer peer = configService.getBgpPeers().get(dstAddress);
145 if (peer != null) {
146 outputPort = peer.connectPoint();
Jonathan Hartf5829202015-02-12 09:37:02 -0800147 }
Jonathan Hart936a7292015-03-06 18:02:57 -0800148 }
149 for (InterfaceAddress addr : bgpSpeaker.interfaceAddresses()) {
150 if (addr.ipAddress().equals(dstAddress) && !context.inPacket()
151 .receivedFrom().equals(bgpSpeaker.connectPoint())) {
152 outputPort = bgpSpeaker.connectPoint();
Jonathan Hartf5829202015-02-12 09:37:02 -0800153 }
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}