Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.bgp.controller.impl; |
| 18 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 19 | import com.google.common.base.MoreObjects; |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 20 | import com.google.common.base.Preconditions; |
| 21 | |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 22 | import org.jboss.netty.channel.Channel; |
| 23 | import org.onlab.packet.IpAddress; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 24 | import org.onosproject.bgp.controller.BgpController; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 25 | import org.onosproject.bgp.controller.BgpLocalRib; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 26 | import org.onosproject.bgp.controller.BgpPeer; |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 27 | import org.onosproject.bgp.controller.BgpSessionInfo; |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 28 | import org.onosproject.bgpio.exceptions.BgpParseException; |
Mohammad Shahid | 30fedc5 | 2017-08-09 11:49:40 +0530 | [diff] [blame^] | 29 | import org.onosproject.bgpio.protocol.BgpEvpnNlri; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 30 | import org.onosproject.bgpio.protocol.BgpFactories; |
| 31 | import org.onosproject.bgpio.protocol.BgpFactory; |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 32 | import org.onosproject.bgpio.protocol.BgpLSNlri; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 33 | import org.onosproject.bgpio.protocol.BgpMessage; |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 34 | import org.onosproject.bgpio.protocol.flowspec.BgpFlowSpecNlri; |
| 35 | import org.onosproject.bgpio.protocol.flowspec.BgpFlowSpecRouteKey; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 36 | import org.onosproject.bgpio.protocol.linkstate.BgpLinkLsNlriVer4; |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 37 | import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4; |
| 38 | import org.onosproject.bgpio.protocol.linkstate.BgpPrefixIPv4LSNlriVer4; |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 39 | import org.onosproject.bgpio.protocol.linkstate.PathAttrNlriDetails; |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 40 | import org.onosproject.bgpio.types.As4Path; |
Mohammad Shahid | 30fedc5 | 2017-08-09 11:49:40 +0530 | [diff] [blame^] | 41 | import org.onosproject.bgpio.types.AsPath; |
Shashikanth VH | 5826066 | 2016-02-13 01:12:02 +0530 | [diff] [blame] | 42 | import org.onosproject.bgpio.types.BgpExtendedCommunity; |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 43 | import org.onosproject.bgpio.types.BgpValueType; |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 44 | import org.onosproject.bgpio.types.LocalPref; |
| 45 | import org.onosproject.bgpio.types.Med; |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 46 | import org.onosproject.bgpio.types.MpReachNlri; |
| 47 | import org.onosproject.bgpio.types.MpUnReachNlri; |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 48 | import org.onosproject.bgpio.types.MultiProtocolExtnCapabilityTlv; |
| 49 | import org.onosproject.bgpio.types.Origin; |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 50 | import org.onosproject.bgpio.types.RpdCapabilityTlv; |
Mohammad Shahid | 30fedc5 | 2017-08-09 11:49:40 +0530 | [diff] [blame^] | 51 | import org.onosproject.bgpio.types.attr.WideCommunity; |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 52 | import org.onosproject.bgpio.util.Constants; |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 53 | import org.slf4j.Logger; |
| 54 | import org.slf4j.LoggerFactory; |
| 55 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 56 | import java.net.InetSocketAddress; |
| 57 | import java.net.SocketAddress; |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 58 | import java.util.ArrayList; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 59 | import java.util.Collections; |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 60 | import java.util.LinkedList; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 61 | import java.util.List; |
| 62 | import java.util.ListIterator; |
| 63 | import java.util.concurrent.RejectedExecutionException; |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * BGPPeerImpl implements BGPPeer, maintains peer information and store updates in RIB . |
| 67 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 68 | public class BgpPeerImpl implements BgpPeer { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 69 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 70 | protected final Logger log = LoggerFactory.getLogger(BgpPeerImpl.class); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 71 | |
| 72 | private static final String SHUTDOWN_MSG = "Worker has already been shutdown"; |
| 73 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 74 | private BgpController bgpController; |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 75 | private Channel channel; |
| 76 | protected String channelId; |
| 77 | private boolean connected; |
| 78 | protected boolean isHandShakeComplete = false; |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 79 | private BgpSessionInfo sessionInfo; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 80 | private BgpPacketStatsImpl pktStats; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 81 | private BgpLocalRib bgplocalRib; |
| 82 | private BgpLocalRib bgplocalRibVpn; |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 83 | private AdjRibIn adjRib; |
| 84 | private VpnAdjRibIn vpnAdjRib; |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 85 | |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 86 | /** |
| 87 | * Return the adjacency RIB-IN. |
| 88 | * |
| 89 | * @return adjRib the adjacency RIB-IN |
| 90 | */ |
| 91 | public AdjRibIn adjacencyRib() { |
| 92 | return adjRib; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Return the adjacency RIB-IN with VPN. |
| 97 | * |
| 98 | * @return vpnAdjRib the adjacency RIB-IN with VPN |
| 99 | */ |
| 100 | public VpnAdjRibIn vpnAdjacencyRib() { |
| 101 | return vpnAdjRib; |
| 102 | } |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 103 | |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 104 | @Override |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 105 | public BgpSessionInfo sessionInfo() { |
| 106 | return sessionInfo; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Initialize peer. |
| 111 | * |
| 112 | *@param bgpController controller instance |
| 113 | *@param sessionInfo bgp session info |
| 114 | *@param pktStats packet statistics |
| 115 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 116 | public BgpPeerImpl(BgpController bgpController, BgpSessionInfo sessionInfo, BgpPacketStatsImpl pktStats) { |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 117 | this.bgpController = bgpController; |
| 118 | this.sessionInfo = sessionInfo; |
| 119 | this.pktStats = pktStats; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 120 | this.bgplocalRib = bgpController.bgpLocalRib(); |
| 121 | this.bgplocalRibVpn = bgpController.bgpLocalRibVpn(); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 122 | this.adjRib = new AdjRibIn(); |
| 123 | this.vpnAdjRib = new VpnAdjRibIn(); |
| 124 | } |
| 125 | |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 126 | /** |
| 127 | * Check if peer support capability. |
| 128 | * |
| 129 | * @param type capability type |
| 130 | * @param afi address family identifier |
| 131 | * @param sAfi subsequent address family identifier |
| 132 | * @return true if capability is supported, otherwise false |
| 133 | */ |
| 134 | public final boolean isCapabilitySupported(short type, short afi, byte sAfi) { |
| 135 | |
| 136 | List<BgpValueType> capability = sessionInfo.remoteBgpCapability(); |
| 137 | ListIterator<BgpValueType> listIterator = capability.listIterator(); |
| 138 | |
| 139 | while (listIterator.hasNext()) { |
| 140 | BgpValueType tlv = listIterator.next(); |
| 141 | |
| 142 | if (tlv.getType() == type) { |
| 143 | if (tlv.getType() == MultiProtocolExtnCapabilityTlv.TYPE) { |
| 144 | MultiProtocolExtnCapabilityTlv temp = (MultiProtocolExtnCapabilityTlv) tlv; |
| 145 | if ((temp.getAfi() == afi) && (temp.getSafi() == sAfi)) { |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 146 | log.debug("Multi prorotcol extension capabality TLV is true"); |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 147 | return true; |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 148 | |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 149 | } |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 150 | } else if (tlv.getType() == RpdCapabilityTlv.TYPE) { |
| 151 | RpdCapabilityTlv temp = (RpdCapabilityTlv) tlv; |
| 152 | if ((temp.getAfi() == afi) && (temp.getSafi() == sAfi)) { |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 153 | log.debug("RPD capabality TLV is true"); |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 154 | return true; |
| 155 | } |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | } |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 159 | log.debug("IS capabality is not supported "); |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 160 | return false; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Send flow specification update message to peer. |
| 165 | * |
| 166 | * @param operType operation type |
Shashikanth VH | 83f77e4 | 2016-05-26 20:34:56 +0530 | [diff] [blame] | 167 | * @param routeKey flow rule key |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 168 | * @param flowSpec flow specification details |
Shashikanth VH | 83f77e4 | 2016-05-26 20:34:56 +0530 | [diff] [blame] | 169 | * @param wideCommunity for route policy |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 170 | */ |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 171 | public final void sendFlowSpecUpdateMessageToPeer(FlowSpecOperation operType, BgpFlowSpecRouteKey routeKey, |
| 172 | BgpFlowSpecNlri flowSpec, WideCommunity wideCommunity) { |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 173 | |
| 174 | List<BgpValueType> attributesList = new LinkedList<>(); |
| 175 | byte sessionType = sessionInfo.isIbgpSession() ? (byte) 0 : (byte) 1; |
Shashikanth VH | e38c21e | 2016-05-26 21:37:25 +0530 | [diff] [blame] | 176 | byte sAfi = Constants.SAFI_FLOWSPEC_VALUE; |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 177 | |
| 178 | boolean isFsCapabilitySet = isCapabilitySupported(MultiProtocolExtnCapabilityTlv.TYPE, |
| 179 | Constants.AFI_FLOWSPEC_VALUE, |
| 180 | Constants.SAFI_FLOWSPEC_VALUE); |
| 181 | |
| 182 | boolean isVpnFsCapabilitySet = isCapabilitySupported(MultiProtocolExtnCapabilityTlv.TYPE, |
| 183 | Constants.AFI_FLOWSPEC_VALUE, |
| 184 | Constants.VPN_SAFI_FLOWSPEC_VALUE); |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 185 | |
| 186 | boolean isRpdCapabilitySet = isCapabilitySupported(RpdCapabilityTlv.TYPE, |
| 187 | Constants.AFI_FLOWSPEC_RPD_VALUE, |
| 188 | Constants.SAFI_FLOWSPEC_RPD_VALUE); |
| 189 | |
| 190 | boolean isVpnRpdCapabilitySet = isCapabilitySupported(RpdCapabilityTlv.TYPE, |
| 191 | Constants.AFI_FLOWSPEC_RPD_VALUE, |
| 192 | Constants.VPN_SAFI_FLOWSPEC_RDP_VALUE); |
| 193 | |
| 194 | if ((!isFsCapabilitySet) && (!isVpnFsCapabilitySet) && (!isRpdCapabilitySet) && (!isVpnRpdCapabilitySet)) { |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 195 | log.debug("Peer do not support BGP flow spec capability", channel.getRemoteAddress()); |
| 196 | return; |
| 197 | } |
| 198 | |
Shashikanth VH | e38c21e | 2016-05-26 21:37:25 +0530 | [diff] [blame] | 199 | if (isVpnFsCapabilitySet) { |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 200 | sAfi = Constants.VPN_SAFI_FLOWSPEC_VALUE; |
Shashikanth VH | e38c21e | 2016-05-26 21:37:25 +0530 | [diff] [blame] | 201 | } else if (isVpnRpdCapabilitySet) { |
| 202 | sAfi = Constants.VPN_SAFI_FLOWSPEC_RDP_VALUE; |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 203 | } |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 204 | attributesList.add(new Origin((byte) 0)); |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 205 | |
| 206 | if (sessionType != 0) { |
| 207 | // EBGP |
| 208 | if (!bgpController.getConfig().getLargeASCapability()) { |
| 209 | List<Short> aspathSet = new ArrayList<>(); |
| 210 | List<Short> aspathSeq = new ArrayList<>(); |
| 211 | aspathSeq.add((short) bgpController.getConfig().getAsNumber()); |
| 212 | |
| 213 | AsPath asPath = new AsPath(aspathSet, aspathSeq); |
| 214 | attributesList.add(asPath); |
| 215 | } else { |
| 216 | List<Integer> aspathSet = new ArrayList<>(); |
| 217 | List<Integer> aspathSeq = new ArrayList<>(); |
| 218 | aspathSeq.add(bgpController.getConfig().getAsNumber()); |
| 219 | |
| 220 | As4Path as4Path = new As4Path(aspathSet, aspathSeq); |
| 221 | attributesList.add(as4Path); |
| 222 | } |
| 223 | attributesList.add(new Med(0)); |
| 224 | } else { |
| 225 | attributesList.add(new AsPath()); |
| 226 | attributesList.add(new Med(0)); |
| 227 | attributesList.add(new LocalPref(100)); |
| 228 | } |
| 229 | |
Shashikanth VH | 5826066 | 2016-02-13 01:12:02 +0530 | [diff] [blame] | 230 | attributesList.add(new BgpExtendedCommunity(flowSpec.fsActionTlv())); |
Shashikanth VH | e38c21e | 2016-05-26 21:37:25 +0530 | [diff] [blame] | 231 | if (wideCommunity != null) { |
| 232 | attributesList.add(wideCommunity); |
| 233 | } |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 234 | |
| 235 | if (operType == FlowSpecOperation.ADD) { |
| 236 | attributesList.add(new MpReachNlri(flowSpec, Constants.AFI_FLOWSPEC_VALUE, sAfi)); |
| 237 | } else if (operType == FlowSpecOperation.DELETE) { |
| 238 | attributesList.add(new MpUnReachNlri(flowSpec, Constants.AFI_FLOWSPEC_VALUE, sAfi)); |
| 239 | } |
| 240 | |
| 241 | BgpMessage msg = Controller.getBgpMessageFactory4().updateMessageBuilder() |
| 242 | .setBgpPathAttributes(attributesList).build(); |
| 243 | |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 244 | log.debug("Sending flow spec update message to {}", channel.getRemoteAddress()); |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 245 | channel.write(Collections.singletonList(msg)); |
| 246 | } |
| 247 | |
| 248 | @Override |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 249 | public void updateFlowSpec(FlowSpecOperation operType, BgpFlowSpecRouteKey routeKey, |
| 250 | BgpFlowSpecNlri flowSpec, WideCommunity wideCommunity) { |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 251 | Preconditions.checkNotNull(operType, "flow specification operation type cannot be null"); |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 252 | Preconditions.checkNotNull(routeKey, "flow specification prefix cannot be null"); |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 253 | Preconditions.checkNotNull(flowSpec, "flow specification details cannot be null"); |
| 254 | Preconditions.checkNotNull(flowSpec.fsActionTlv(), "flow specification action cannot be null"); |
| 255 | |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 256 | sendFlowSpecUpdateMessageToPeer(operType, routeKey, flowSpec, wideCommunity); |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 257 | } |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 258 | |
| 259 | @Override |
Mohammad Shahid | 30fedc5 | 2017-08-09 11:49:40 +0530 | [diff] [blame^] | 260 | public void updateEvpnNlri(FlowSpecOperation operType, IpAddress nextHop, |
| 261 | List<BgpValueType> extcommunity, |
| 262 | List<BgpEvpnNlri> evpnNlris) { |
| 263 | Preconditions.checkNotNull(operType, "Operation type cannot be null"); |
| 264 | Preconditions.checkNotNull(evpnNlris, "Evpn nlri cannot be null"); |
| 265 | sendEvpnUpdateMessageToPeer(operType, nextHop, extcommunity, evpnNlris); |
| 266 | } |
| 267 | |
| 268 | private void sendEvpnUpdateMessageToPeer(FlowSpecOperation operType, |
| 269 | IpAddress nextHop, |
| 270 | List<BgpValueType> extcommunity, |
| 271 | List<BgpEvpnNlri> evpnNlris) { |
| 272 | List<BgpValueType> attributesList = new LinkedList<>(); |
| 273 | byte sessionType = sessionInfo.isIbgpSession() ? (byte) 0 : (byte) 1; |
| 274 | short afi = Constants.AFI_EVPN_VALUE; |
| 275 | byte safi = Constants.SAFI_EVPN_VALUE; |
| 276 | boolean isEvpnCapabilitySet = isCapabilitySupported(MultiProtocolExtnCapabilityTlv.TYPE, |
| 277 | afi, safi); |
| 278 | if (!isEvpnCapabilitySet) { |
| 279 | log.debug("Peer do not support BGP Evpn capability", |
| 280 | channel.getRemoteAddress()); |
| 281 | return; |
| 282 | } |
| 283 | attributesList.add(new Origin((byte) 0)); |
| 284 | |
| 285 | if (sessionType != 0) { |
| 286 | // EBGP |
| 287 | if (!bgpController.getConfig().getLargeASCapability()) { |
| 288 | List<Short> aspathSet = new ArrayList<>(); |
| 289 | List<Short> aspathSeq = new ArrayList<>(); |
| 290 | aspathSeq.add((short) bgpController.getConfig().getAsNumber()); |
| 291 | |
| 292 | AsPath asPath = new AsPath(aspathSet, aspathSeq); |
| 293 | attributesList.add(asPath); |
| 294 | } else { |
| 295 | List<Integer> aspathSet = new ArrayList<>(); |
| 296 | List<Integer> aspathSeq = new ArrayList<>(); |
| 297 | aspathSeq.add(bgpController.getConfig().getAsNumber()); |
| 298 | |
| 299 | As4Path as4Path = new As4Path(aspathSet, aspathSeq); |
| 300 | attributesList.add(as4Path); |
| 301 | } |
| 302 | } else { |
| 303 | attributesList.add(new AsPath()); |
| 304 | } |
| 305 | |
| 306 | if (!extcommunity.isEmpty()) { |
| 307 | attributesList.add(new BgpExtendedCommunity(extcommunity)); |
| 308 | } |
| 309 | if (operType == FlowSpecOperation.ADD || operType == FlowSpecOperation.UPDATE) { |
| 310 | attributesList |
| 311 | .add(new MpReachNlri(evpnNlris, afi, safi, nextHop.getIp4Address())); |
| 312 | } else if (operType == FlowSpecOperation.DELETE) { |
| 313 | attributesList.add(new MpUnReachNlri(evpnNlris, afi, safi)); |
| 314 | } |
| 315 | |
| 316 | BgpMessage msg = Controller.getBgpMessageFactory4() |
| 317 | .updateMessageBuilder().setBgpPathAttributes(attributesList) |
| 318 | .build(); |
| 319 | channel.write(Collections.singletonList(msg)); |
| 320 | } |
| 321 | |
| 322 | @Override |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 323 | public void buildAdjRibIn(List<BgpValueType> pathAttr) throws BgpParseException { |
| 324 | ListIterator<BgpValueType> iterator = pathAttr.listIterator(); |
| 325 | while (iterator.hasNext()) { |
| 326 | BgpValueType attr = iterator.next(); |
| 327 | if (attr instanceof MpReachNlri) { |
| 328 | List<BgpLSNlri> nlri = ((MpReachNlri) attr).mpReachNlri(); |
| 329 | callAdd(this, nlri, pathAttr); |
| 330 | } |
| 331 | if (attr instanceof MpUnReachNlri) { |
| 332 | List<BgpLSNlri> nlri = ((MpUnReachNlri) attr).mpUnReachNlri(); |
| 333 | callRemove(this, nlri); |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Updates NLRI identifier node in a tree separately based on afi and safi. |
| 340 | * |
| 341 | * @param peerImpl BGP peer instance |
| 342 | * @param nlri MpReachNlri path attribute |
| 343 | * @param pathAttr list of BGP path attributes |
| 344 | * @throws BgpParseException throws exception |
| 345 | */ |
| 346 | public void callAdd(BgpPeerImpl peerImpl, List<BgpLSNlri> nlri, List<BgpValueType> pathAttr) |
| 347 | throws BgpParseException { |
| 348 | ListIterator<BgpLSNlri> listIterator = nlri.listIterator(); |
| 349 | while (listIterator.hasNext()) { |
| 350 | BgpLSNlri nlriInfo = listIterator.next(); |
| 351 | if (nlriInfo instanceof BgpNodeLSNlriVer4) { |
| 352 | PathAttrNlriDetails details = setPathAttrDetails(nlriInfo, pathAttr); |
| 353 | if (!((BgpNodeLSNlriVer4) nlriInfo).isVpnPresent()) { |
| 354 | adjRib.add(nlriInfo, details); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 355 | bgplocalRib.add(sessionInfo(), nlriInfo, details); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 356 | } else { |
| 357 | vpnAdjRib.addVpn(nlriInfo, details, ((BgpNodeLSNlriVer4) nlriInfo).getRouteDistinguisher()); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 358 | bgplocalRibVpn.add(sessionInfo(), nlriInfo, details, |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 359 | ((BgpNodeLSNlriVer4) nlriInfo).getRouteDistinguisher()); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 360 | } |
| 361 | } else if (nlriInfo instanceof BgpLinkLsNlriVer4) { |
| 362 | PathAttrNlriDetails details = setPathAttrDetails(nlriInfo, pathAttr); |
| 363 | if (!((BgpLinkLsNlriVer4) nlriInfo).isVpnPresent()) { |
| 364 | adjRib.add(nlriInfo, details); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 365 | bgplocalRib.add(sessionInfo(), nlriInfo, details); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 366 | } else { |
| 367 | vpnAdjRib.addVpn(nlriInfo, details, ((BgpLinkLsNlriVer4) nlriInfo).getRouteDistinguisher()); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 368 | bgplocalRibVpn.add(sessionInfo(), nlriInfo, details, |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 369 | ((BgpLinkLsNlriVer4) nlriInfo).getRouteDistinguisher()); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 370 | } |
| 371 | } else if (nlriInfo instanceof BgpPrefixIPv4LSNlriVer4) { |
| 372 | PathAttrNlriDetails details = setPathAttrDetails(nlriInfo, pathAttr); |
| 373 | if (!((BgpPrefixIPv4LSNlriVer4) nlriInfo).isVpnPresent()) { |
| 374 | adjRib.add(nlriInfo, details); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 375 | bgplocalRib.add(sessionInfo(), nlriInfo, details); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 376 | } else { |
| 377 | vpnAdjRib.addVpn(nlriInfo, details, ((BgpPrefixIPv4LSNlriVer4) nlriInfo).getRouteDistinguisher()); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 378 | bgplocalRibVpn.add(sessionInfo(), nlriInfo, details, |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 379 | ((BgpPrefixIPv4LSNlriVer4) nlriInfo).getRouteDistinguisher()); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * Sets BGP path attribute and NLRI details. |
| 387 | * |
| 388 | * @param nlriInfo MpReachNlri path attribute |
| 389 | * @param pathAttr list of BGP path attributes |
| 390 | * @return details object of PathAttrNlriDetails |
| 391 | * @throws BgpParseException throw exception |
| 392 | */ |
| 393 | public PathAttrNlriDetails setPathAttrDetails(BgpLSNlri nlriInfo, List<BgpValueType> pathAttr) |
| 394 | throws BgpParseException { |
| 395 | PathAttrNlriDetails details = new PathAttrNlriDetails(); |
| 396 | details.setProtocolID(nlriInfo.getProtocolId()); |
| 397 | details.setIdentifier(nlriInfo.getIdentifier()); |
| 398 | details.setPathAttribute(pathAttr); |
| 399 | return details; |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * Removes NLRI identifier node in a tree separately based on afi and safi. |
| 404 | * |
| 405 | * @param peerImpl BGP peer instance |
| 406 | * @param nlri NLRI information |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 407 | * @throws BgpParseException BGP parse exception |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 408 | */ |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 409 | public void callRemove(BgpPeerImpl peerImpl, List<BgpLSNlri> nlri) throws BgpParseException { |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 410 | ListIterator<BgpLSNlri> listIterator = nlri.listIterator(); |
| 411 | while (listIterator.hasNext()) { |
| 412 | BgpLSNlri nlriInfo = listIterator.next(); |
| 413 | if (nlriInfo instanceof BgpNodeLSNlriVer4) { |
| 414 | if (!((BgpNodeLSNlriVer4) nlriInfo).isVpnPresent()) { |
| 415 | adjRib.remove(nlriInfo); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 416 | bgplocalRib.delete(nlriInfo); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 417 | } else { |
| 418 | vpnAdjRib.removeVpn(nlriInfo, ((BgpNodeLSNlriVer4) nlriInfo).getRouteDistinguisher()); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 419 | bgplocalRibVpn.delete(nlriInfo, ((BgpNodeLSNlriVer4) nlriInfo).getRouteDistinguisher()); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 420 | } |
| 421 | } else if (nlriInfo instanceof BgpLinkLsNlriVer4) { |
| 422 | if (!((BgpLinkLsNlriVer4) nlriInfo).isVpnPresent()) { |
| 423 | adjRib.remove(nlriInfo); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 424 | bgplocalRib.delete(nlriInfo); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 425 | } else { |
| 426 | vpnAdjRib.removeVpn(nlriInfo, ((BgpLinkLsNlriVer4) nlriInfo).getRouteDistinguisher()); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 427 | bgplocalRibVpn.delete(nlriInfo, ((BgpLinkLsNlriVer4) nlriInfo).getRouteDistinguisher()); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 428 | } |
| 429 | } else if (nlriInfo instanceof BgpPrefixIPv4LSNlriVer4) { |
| 430 | if (!((BgpPrefixIPv4LSNlriVer4) nlriInfo).isVpnPresent()) { |
| 431 | adjRib.remove(nlriInfo); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 432 | bgplocalRib.delete(nlriInfo); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 433 | } else { |
| 434 | vpnAdjRib.removeVpn(nlriInfo, ((BgpPrefixIPv4LSNlriVer4) nlriInfo).getRouteDistinguisher()); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 435 | bgplocalRibVpn.delete(nlriInfo, ((BgpPrefixIPv4LSNlriVer4) nlriInfo).getRouteDistinguisher()); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * Return the adjacency RIB-IN. |
| 443 | * |
| 444 | * @return adjRib the adjacency RIB-IN |
| 445 | */ |
| 446 | public AdjRibIn adjRib() { |
| 447 | return adjRib; |
| 448 | } |
| 449 | |
| 450 | /** |
| 451 | * Return the adjacency RIB-IN with VPN. |
| 452 | * |
| 453 | * @return vpnAdjRib the adjacency RIB-IN with VPN |
| 454 | */ |
| 455 | public VpnAdjRibIn vpnAdjRib() { |
| 456 | return vpnAdjRib; |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 457 | } |
| 458 | |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 459 | /** |
| 460 | * Update localRIB on peer disconnect. |
| 461 | * |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 462 | * @throws BgpParseException while updating local RIB |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 463 | */ |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 464 | public void updateLocalRibOnPeerDisconnect() throws BgpParseException { |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 465 | BgpLocalRibImpl localRib = (BgpLocalRibImpl) bgplocalRib; |
| 466 | BgpLocalRibImpl localRibVpn = (BgpLocalRibImpl) bgplocalRibVpn; |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 467 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 468 | localRib.localRibUpdate(adjacencyRib()); |
| 469 | localRibVpn.localRibUpdate(vpnAdjacencyRib()); |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 470 | } |
| 471 | |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 472 | // ************************ |
| 473 | // Channel related |
| 474 | // ************************ |
| 475 | |
| 476 | @Override |
| 477 | public final void disconnectPeer() { |
| 478 | this.channel.close(); |
| 479 | } |
| 480 | |
| 481 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 482 | public final void sendMessage(BgpMessage m) { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 483 | log.debug("Sending message to {}", channel.getRemoteAddress()); |
| 484 | try { |
| 485 | channel.write(Collections.singletonList(m)); |
| 486 | this.pktStats.addOutPacket(); |
| 487 | } catch (RejectedExecutionException e) { |
| 488 | log.warn(e.getMessage()); |
| 489 | if (!e.getMessage().contains(SHUTDOWN_MSG)) { |
| 490 | throw e; |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 496 | public final void sendMessage(List<BgpMessage> msgs) { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 497 | try { |
| 498 | channel.write(msgs); |
| 499 | this.pktStats.addOutPacket(msgs.size()); |
| 500 | } catch (RejectedExecutionException e) { |
| 501 | log.warn(e.getMessage()); |
| 502 | if (!e.getMessage().contains(SHUTDOWN_MSG)) { |
| 503 | throw e; |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | @Override |
| 509 | public final boolean isConnected() { |
| 510 | return this.connected; |
| 511 | } |
| 512 | |
| 513 | @Override |
| 514 | public final void setConnected(boolean connected) { |
| 515 | this.connected = connected; |
Jon Hall | 8c7b06a | 2017-02-22 13:37:33 -0800 | [diff] [blame] | 516 | } |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 517 | |
| 518 | @Override |
| 519 | public final void setChannel(Channel channel) { |
| 520 | this.channel = channel; |
| 521 | final SocketAddress address = channel.getRemoteAddress(); |
| 522 | if (address instanceof InetSocketAddress) { |
| 523 | final InetSocketAddress inetAddress = (InetSocketAddress) address; |
| 524 | final IpAddress ipAddress = IpAddress.valueOf(inetAddress.getAddress()); |
| 525 | if (ipAddress.isIp4()) { |
| 526 | channelId = ipAddress.toString() + ':' + inetAddress.getPort(); |
| 527 | } else { |
| 528 | channelId = '[' + ipAddress.toString() + "]:" + inetAddress.getPort(); |
| 529 | } |
| 530 | } |
Jon Hall | 8c7b06a | 2017-02-22 13:37:33 -0800 | [diff] [blame] | 531 | } |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 532 | |
| 533 | @Override |
| 534 | public final Channel getChannel() { |
| 535 | return this.channel; |
Jon Hall | 8c7b06a | 2017-02-22 13:37:33 -0800 | [diff] [blame] | 536 | } |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 537 | |
| 538 | @Override |
| 539 | public String channelId() { |
| 540 | return channelId; |
| 541 | } |
| 542 | |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 543 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 544 | public BgpFactory factory() { |
| 545 | return BgpFactories.getFactory(sessionInfo.remoteBgpVersion()); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | @Override |
| 549 | public boolean isHandshakeComplete() { |
| 550 | return isHandShakeComplete; |
| 551 | } |
| 552 | |
| 553 | @Override |
| 554 | public String toString() { |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 555 | return MoreObjects.toStringHelper(getClass()).omitNullValues() |
| 556 | .add("channel", channelId()) |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 557 | .add("BgpId", sessionInfo().remoteBgpId()).toString(); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 558 | } |
| 559 | } |