Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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.routing.fpm; |
| 18 | |
Andrea Campanella | 4310f6e | 2018-03-27 16:35:39 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableMap; |
Charles Chan | 035ed1f | 2018-01-30 16:00:32 -0800 | [diff] [blame] | 20 | import com.google.common.collect.Lists; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 21 | import org.jboss.netty.bootstrap.ServerBootstrap; |
| 22 | import org.jboss.netty.channel.Channel; |
| 23 | import org.jboss.netty.channel.ChannelException; |
| 24 | import org.jboss.netty.channel.ChannelFactory; |
| 25 | import org.jboss.netty.channel.ChannelPipeline; |
| 26 | import org.jboss.netty.channel.ChannelPipelineFactory; |
| 27 | import org.jboss.netty.channel.Channels; |
| 28 | import org.jboss.netty.channel.group.ChannelGroup; |
| 29 | import org.jboss.netty.channel.group.DefaultChannelGroup; |
| 30 | import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; |
| 31 | import org.jboss.netty.handler.timeout.IdleStateHandler; |
| 32 | import org.jboss.netty.util.HashedWheelTimer; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 33 | import org.onlab.packet.Ip4Address; |
| 34 | import org.onlab.packet.Ip6Address; |
Andrea Campanella | 4310f6e | 2018-03-27 16:35:39 -0700 | [diff] [blame] | 35 | import org.onlab.packet.IpAddress; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 36 | import org.onlab.packet.IpPrefix; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 37 | import org.onlab.util.KryoNamespace; |
| 38 | import org.onlab.util.Tools; |
| 39 | import org.onosproject.cfg.ComponentConfigService; |
Charles Chan | 035ed1f | 2018-01-30 16:00:32 -0800 | [diff] [blame] | 40 | import org.onosproject.cluster.ClusterEvent; |
| 41 | import org.onosproject.cluster.ClusterEventListener; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 42 | import org.onosproject.cluster.ClusterService; |
| 43 | import org.onosproject.cluster.NodeId; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 44 | import org.onosproject.core.ApplicationId; |
Andrea Campanella | 4310f6e | 2018-03-27 16:35:39 -0700 | [diff] [blame] | 45 | import org.onosproject.core.CoreService; |
| 46 | import org.onosproject.net.host.InterfaceIpAddress; |
| 47 | import org.onosproject.net.intf.Interface; |
| 48 | import org.onosproject.net.intf.InterfaceService; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 49 | import org.onosproject.routeservice.Route; |
| 50 | import org.onosproject.routeservice.RouteAdminService; |
Andrea Campanella | 4310f6e | 2018-03-27 16:35:39 -0700 | [diff] [blame] | 51 | import org.onosproject.routing.fpm.api.FpmPrefixStore; |
| 52 | import org.onosproject.routing.fpm.api.FpmPrefixStoreEvent; |
| 53 | import org.onosproject.routing.fpm.api.FpmRecord; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 54 | import org.onosproject.routing.fpm.protocol.FpmHeader; |
| 55 | import org.onosproject.routing.fpm.protocol.Netlink; |
| 56 | import org.onosproject.routing.fpm.protocol.NetlinkMessageType; |
| 57 | import org.onosproject.routing.fpm.protocol.RouteAttribute; |
| 58 | import org.onosproject.routing.fpm.protocol.RouteAttributeDst; |
| 59 | import org.onosproject.routing.fpm.protocol.RouteAttributeGateway; |
| 60 | import org.onosproject.routing.fpm.protocol.RtNetlink; |
| 61 | import org.onosproject.routing.fpm.protocol.RtProtocol; |
Andrea Campanella | 4310f6e | 2018-03-27 16:35:39 -0700 | [diff] [blame] | 62 | import org.onosproject.store.StoreDelegate; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 63 | import org.onosproject.store.serializers.KryoNamespaces; |
Charles Chan | 035ed1f | 2018-01-30 16:00:32 -0800 | [diff] [blame] | 64 | import org.onosproject.store.service.AsyncDistributedLock; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 65 | import org.onosproject.store.service.ConsistentMap; |
| 66 | import org.onosproject.store.service.Serializer; |
| 67 | import org.onosproject.store.service.StorageService; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 68 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 69 | import org.osgi.service.component.annotations.Activate; |
| 70 | import org.osgi.service.component.annotations.Component; |
| 71 | import org.osgi.service.component.annotations.Deactivate; |
| 72 | import org.osgi.service.component.annotations.Modified; |
| 73 | import org.osgi.service.component.annotations.Reference; |
| 74 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 75 | import org.osgi.service.component.annotations.ReferencePolicy; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 76 | import org.slf4j.Logger; |
| 77 | import org.slf4j.LoggerFactory; |
| 78 | |
| 79 | import java.net.InetSocketAddress; |
Charles Chan | 035ed1f | 2018-01-30 16:00:32 -0800 | [diff] [blame] | 80 | import java.time.Duration; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 81 | import java.util.ArrayList; |
| 82 | import java.util.Arrays; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 83 | import java.util.Collection; |
| 84 | import java.util.Collections; |
| 85 | import java.util.Dictionary; |
| 86 | import java.util.HashSet; |
| 87 | import java.util.LinkedList; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 88 | import java.util.List; |
| 89 | import java.util.Map; |
| 90 | import java.util.Set; |
William Davies | 5bae505 | 2019-10-19 01:25:01 -0700 | [diff] [blame^] | 91 | import java.util.Iterator; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 92 | import java.util.concurrent.ConcurrentHashMap; |
Jordan Halterman | aa2faca | 2018-08-13 02:41:50 -0700 | [diff] [blame] | 93 | import java.util.concurrent.ExecutorService; |
| 94 | import java.util.concurrent.Executors; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 95 | import java.util.stream.Collectors; |
| 96 | |
| 97 | import static java.util.concurrent.Executors.newCachedThreadPool; |
| 98 | import static org.onlab.util.Tools.groupedThreads; |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 99 | import static org.onosproject.routing.fpm.OsgiPropertyConstants.CLEAR_ROUTES; |
| 100 | import static org.onosproject.routing.fpm.OsgiPropertyConstants.CLEAR_ROUTES_DEFAULT; |
| 101 | import static org.onosproject.routing.fpm.OsgiPropertyConstants.PD_PUSH_ENABLED; |
| 102 | import static org.onosproject.routing.fpm.OsgiPropertyConstants.PD_PUSH_ENABLED_DEFAULT; |
| 103 | import static org.onosproject.routing.fpm.OsgiPropertyConstants.PD_PUSH_NEXT_HOP_IPV4; |
| 104 | import static org.onosproject.routing.fpm.OsgiPropertyConstants.PD_PUSH_NEXT_HOP_IPV4_DEFAULT; |
| 105 | import static org.onosproject.routing.fpm.OsgiPropertyConstants.PD_PUSH_NEXT_HOP_IPV6; |
| 106 | import static org.onosproject.routing.fpm.OsgiPropertyConstants.PD_PUSH_NEXT_HOP_IPV6_DEFAULT; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 107 | |
| 108 | /** |
| 109 | * Forwarding Plane Manager (FPM) route source. |
| 110 | */ |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 111 | @Component( |
| 112 | immediate = true, |
| 113 | service = FpmInfoService.class, |
| 114 | property = { |
| 115 | CLEAR_ROUTES + ":Boolean=" + CLEAR_ROUTES_DEFAULT, |
| 116 | PD_PUSH_ENABLED + ":Boolean=" + PD_PUSH_ENABLED_DEFAULT, |
| 117 | PD_PUSH_NEXT_HOP_IPV4 + "=" + PD_PUSH_NEXT_HOP_IPV4_DEFAULT, |
| 118 | PD_PUSH_NEXT_HOP_IPV6 + "=" + PD_PUSH_NEXT_HOP_IPV6_DEFAULT, |
| 119 | } |
| 120 | ) |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 121 | public class FpmManager implements FpmInfoService { |
| 122 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 123 | |
| 124 | private static final int FPM_PORT = 2620; |
| 125 | private static final String APP_NAME = "org.onosproject.fpm"; |
| 126 | private static final int IDLE_TIMEOUT_SECS = 5; |
Charles Chan | 035ed1f | 2018-01-30 16:00:32 -0800 | [diff] [blame] | 127 | private static final String LOCK_NAME = "fpm-manager-lock"; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 128 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 129 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 130 | protected CoreService coreService; |
| 131 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 132 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 133 | protected ComponentConfigService componentConfigService; |
| 134 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 135 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 136 | protected RouteAdminService routeService; |
| 137 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 138 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 139 | protected ClusterService clusterService; |
| 140 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 141 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 142 | protected StorageService storageService; |
| 143 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 144 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 145 | protected InterfaceService interfaceService; |
| 146 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 147 | @Reference(cardinality = ReferenceCardinality.OPTIONAL, |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 148 | bind = "bindRipStore", |
| 149 | unbind = "unbindRipStore", |
| 150 | policy = ReferencePolicy.DYNAMIC, |
Ray Milkey | 5504bd2 | 2019-03-22 16:24:38 -0700 | [diff] [blame] | 151 | target = "(_fpm_type=RIP)") |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 152 | protected volatile FpmPrefixStore ripStore; |
| 153 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 154 | @Reference(cardinality = ReferenceCardinality.OPTIONAL, |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 155 | bind = "bindDhcpStore", |
| 156 | unbind = "unbindDhcpStore", |
| 157 | policy = ReferencePolicy.DYNAMIC, |
Ray Milkey | 5504bd2 | 2019-03-22 16:24:38 -0700 | [diff] [blame] | 158 | target = "(_fpm_type=DHCP)") |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 159 | protected volatile FpmPrefixStore dhcpStore; |
| 160 | |
| 161 | private final StoreDelegate<FpmPrefixStoreEvent> fpmPrefixStoreDelegate |
| 162 | = new FpmPrefixStoreDelegate(); |
| 163 | |
| 164 | private ApplicationId appId; |
| 165 | private ServerBootstrap serverBootstrap; |
| 166 | private Channel serverChannel; |
| 167 | private ChannelGroup allChannels = new DefaultChannelGroup(); |
Charles Chan | 035ed1f | 2018-01-30 16:00:32 -0800 | [diff] [blame] | 168 | private final InternalClusterListener clusterListener = new InternalClusterListener(); |
| 169 | private AsyncDistributedLock asyncLock; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 170 | |
Jordan Halterman | aa2faca | 2018-08-13 02:41:50 -0700 | [diff] [blame] | 171 | private ExecutorService clusterEventExecutor; |
| 172 | |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 173 | private ConsistentMap<FpmPeer, Set<FpmConnectionInfo>> peers; |
| 174 | |
| 175 | private Map<FpmPeer, Map<IpPrefix, Route>> fpmRoutes = new ConcurrentHashMap<>(); |
| 176 | |
Andrea Campanella | 4310f6e | 2018-03-27 16:35:39 -0700 | [diff] [blame] | 177 | //Local cache for peers to be used in case of cluster partition. |
| 178 | private Map<FpmPeer, Set<FpmConnectionInfo>> localPeers = new ConcurrentHashMap<>(); |
| 179 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 180 | /** Whether to clear routes when the FPM connection goes down. */ |
| 181 | private boolean clearRoutes = CLEAR_ROUTES_DEFAULT; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 182 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 183 | /** Whether to push prefixes to Quagga over fpm connection. */ |
| 184 | private boolean pdPushEnabled = PD_PUSH_ENABLED_DEFAULT; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 185 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 186 | /** IPv4 next-hop address for PD Pushing. */ |
shalde064 | 280feec | 2018-06-15 19:01:29 -0400 | [diff] [blame] | 187 | private List<Ip4Address> pdPushNextHopIPv4 = null; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 188 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 189 | /** IPv6 next-hop address for PD Pushing. */ |
shalde064 | 280feec | 2018-06-15 19:01:29 -0400 | [diff] [blame] | 190 | private List<Ip6Address> pdPushNextHopIPv6 = null; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 191 | |
| 192 | protected void bindRipStore(FpmPrefixStore store) { |
| 193 | if ((ripStore == null) && (store != null)) { |
| 194 | ripStore = store; |
| 195 | ripStore.setDelegate(fpmPrefixStoreDelegate); |
| 196 | for (Channel ch : allChannels) { |
| 197 | processRipStaticRoutes(ch); |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | protected void unbindRipStore(FpmPrefixStore store) { |
| 203 | if (ripStore == store) { |
| 204 | ripStore.unsetDelegate(fpmPrefixStoreDelegate); |
| 205 | ripStore = null; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | protected void bindDhcpStore(FpmPrefixStore store) { |
| 210 | if ((dhcpStore == null) && (store != null)) { |
| 211 | dhcpStore = store; |
| 212 | dhcpStore.setDelegate(fpmPrefixStoreDelegate); |
| 213 | for (Channel ch : allChannels) { |
| 214 | processDhcpStaticRoutes(ch); |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | protected void unbindDhcpStore(FpmPrefixStore store) { |
| 220 | if (dhcpStore == store) { |
| 221 | dhcpStore.unsetDelegate(fpmPrefixStoreDelegate); |
| 222 | dhcpStore = null; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | @Activate |
| 227 | protected void activate(ComponentContext context) { |
| 228 | componentConfigService.preSetProperty( |
| 229 | "org.onosproject.incubator.store.routing.impl.RouteStoreImpl", |
| 230 | "distributed", "true"); |
| 231 | |
| 232 | componentConfigService.registerProperties(getClass()); |
| 233 | |
| 234 | KryoNamespace serializer = KryoNamespace.newBuilder() |
| 235 | .register(KryoNamespaces.API) |
| 236 | .register(FpmPeer.class) |
| 237 | .register(FpmConnectionInfo.class) |
| 238 | .build(); |
| 239 | peers = storageService.<FpmPeer, Set<FpmConnectionInfo>>consistentMapBuilder() |
| 240 | .withName("fpm-connections") |
| 241 | .withSerializer(Serializer.using(serializer)) |
| 242 | .build(); |
| 243 | |
| 244 | modified(context); |
| 245 | startServer(); |
| 246 | |
| 247 | appId = coreService.registerApplication(APP_NAME, peers::destroy); |
| 248 | |
Charles Chan | 035ed1f | 2018-01-30 16:00:32 -0800 | [diff] [blame] | 249 | asyncLock = storageService.lockBuilder().withName(LOCK_NAME).build(); |
Jordan Halterman | aa2faca | 2018-08-13 02:41:50 -0700 | [diff] [blame] | 250 | |
| 251 | clusterEventExecutor = Executors.newSingleThreadExecutor(groupedThreads("fpm-event-main", "%d", log)); |
Saurav Das | e7f5101 | 2018-02-09 17:26:45 -0800 | [diff] [blame] | 252 | clusterService.addListener(clusterListener); |
Charles Chan | 035ed1f | 2018-01-30 16:00:32 -0800 | [diff] [blame] | 253 | |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 254 | log.info("Started"); |
| 255 | } |
| 256 | |
| 257 | @Deactivate |
| 258 | protected void deactivate() { |
| 259 | componentConfigService.preSetProperty( |
| 260 | "org.onosproject.incubator.store.routing.impl.RouteStoreImpl", |
| 261 | "distributed", "false"); |
| 262 | |
| 263 | stopServer(); |
| 264 | fpmRoutes.clear(); |
| 265 | componentConfigService.unregisterProperties(getClass(), false); |
Charles Chan | 035ed1f | 2018-01-30 16:00:32 -0800 | [diff] [blame] | 266 | |
| 267 | clusterService.removeListener(clusterListener); |
Jordan Halterman | aa2faca | 2018-08-13 02:41:50 -0700 | [diff] [blame] | 268 | clusterEventExecutor.shutdown(); |
Charles Chan | 035ed1f | 2018-01-30 16:00:32 -0800 | [diff] [blame] | 269 | asyncLock.unlock(); |
| 270 | |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 271 | log.info("Stopped"); |
| 272 | } |
| 273 | |
| 274 | @Modified |
| 275 | protected void modified(ComponentContext context) { |
shalde064 | 280feec | 2018-06-15 19:01:29 -0400 | [diff] [blame] | 276 | Ip4Address rurIPv4Address; |
| 277 | Ip6Address rurIPv6Address; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 278 | Dictionary<?, ?> properties = context.getProperties(); |
| 279 | if (properties == null) { |
| 280 | return; |
| 281 | } |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 282 | String strClearRoutes = Tools.get(properties, CLEAR_ROUTES); |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 283 | if (strClearRoutes != null) { |
| 284 | clearRoutes = Boolean.parseBoolean(strClearRoutes); |
| 285 | log.info("clearRoutes is {}", clearRoutes); |
| 286 | } |
| 287 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 288 | String strPdPushEnabled = Tools.get(properties, PD_PUSH_ENABLED); |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 289 | if (strPdPushEnabled != null) { |
| 290 | boolean oldValue = pdPushEnabled; |
| 291 | pdPushEnabled = Boolean.parseBoolean(strPdPushEnabled); |
| 292 | if (pdPushEnabled) { |
| 293 | |
shalde064 | 280feec | 2018-06-15 19:01:29 -0400 | [diff] [blame] | 294 | pdPushNextHopIPv4 = new ArrayList<Ip4Address>(); |
| 295 | pdPushNextHopIPv6 = new ArrayList<Ip6Address>(); |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 296 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 297 | String strPdPushNextHopIPv4 = Tools.get(properties, PD_PUSH_NEXT_HOP_IPV4); |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 298 | if (strPdPushNextHopIPv4 != null) { |
shalde064 | 280feec | 2018-06-15 19:01:29 -0400 | [diff] [blame] | 299 | List<String> strPdPushNextHopIPv4List = Arrays.asList(strPdPushNextHopIPv4.split(",")); |
| 300 | for (String nextHop : strPdPushNextHopIPv4List) { |
Mayank Tiwari | 2d3a308 | 2018-11-23 16:18:50 -0500 | [diff] [blame] | 301 | log.trace("IPv4 next hop added is:" + nextHop); |
shalde064 | 280feec | 2018-06-15 19:01:29 -0400 | [diff] [blame] | 302 | pdPushNextHopIPv4.add(Ip4Address.valueOf(nextHop)); |
| 303 | } |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 304 | } |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 305 | String strPdPushNextHopIPv6 = Tools.get(properties, PD_PUSH_NEXT_HOP_IPV6); |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 306 | if (strPdPushNextHopIPv6 != null) { |
shalde064 | 280feec | 2018-06-15 19:01:29 -0400 | [diff] [blame] | 307 | List<String> strPdPushNextHopIPv6List = Arrays.asList(strPdPushNextHopIPv6.split(",")); |
| 308 | for (String nextHop : strPdPushNextHopIPv6List) { |
Mayank Tiwari | 2d3a308 | 2018-11-23 16:18:50 -0500 | [diff] [blame] | 309 | log.trace("IPv6 next hop added is:" + nextHop); |
shalde064 | 280feec | 2018-06-15 19:01:29 -0400 | [diff] [blame] | 310 | pdPushNextHopIPv6.add(Ip6Address.valueOf(nextHop)); |
| 311 | } |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Ray Milkey | 032b964 | 2018-06-21 08:28:12 -0700 | [diff] [blame] | 314 | if (pdPushNextHopIPv4.size() == 0) { |
shalde064 | 280feec | 2018-06-15 19:01:29 -0400 | [diff] [blame] | 315 | rurIPv4Address = interfaceService.getInterfaces() |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 316 | .stream() |
| 317 | .filter(iface -> iface.name().contains("RUR")) |
| 318 | .map(Interface::ipAddressesList) |
| 319 | .flatMap(Collection::stream) |
| 320 | .map(InterfaceIpAddress::ipAddress) |
| 321 | .filter(IpAddress::isIp4) |
| 322 | .map(IpAddress::getIp4Address) |
| 323 | .findFirst() |
| 324 | .orElse(null); |
Mayank Tiwari | c679a02 | 2018-06-23 11:19:08 -0400 | [diff] [blame] | 325 | log.debug("RUR IPv4 address extracted from netcfg is: {}", rurIPv4Address); |
shalde064 | 280feec | 2018-06-15 19:01:29 -0400 | [diff] [blame] | 326 | if (rurIPv4Address != null) { |
| 327 | pdPushNextHopIPv4.add(rurIPv4Address); |
Mayank Tiwari | c679a02 | 2018-06-23 11:19:08 -0400 | [diff] [blame] | 328 | } else { |
| 329 | log.debug("Unable to extract RUR IPv4 address from netcfg"); |
shalde064 | 280feec | 2018-06-15 19:01:29 -0400 | [diff] [blame] | 330 | } |
| 331 | |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Mayank Tiwari | c679a02 | 2018-06-23 11:19:08 -0400 | [diff] [blame] | 334 | if (pdPushNextHopIPv6 == null || pdPushNextHopIPv6.size() == 0) { |
shalde064 | 280feec | 2018-06-15 19:01:29 -0400 | [diff] [blame] | 335 | rurIPv6Address = interfaceService.getInterfaces() |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 336 | .stream() |
| 337 | .filter(iface -> iface.name().contains("RUR")) |
| 338 | .map(Interface::ipAddressesList) |
| 339 | .flatMap(Collection::stream) |
| 340 | .map(InterfaceIpAddress::ipAddress) |
| 341 | .filter(IpAddress::isIp6) |
| 342 | .map(IpAddress::getIp6Address) |
| 343 | .findFirst() |
| 344 | .orElse(null); |
Mayank Tiwari | c679a02 | 2018-06-23 11:19:08 -0400 | [diff] [blame] | 345 | log.debug("RUR IPv6 address extracted from netcfg is: {}", rurIPv6Address); |
shalde064 | 280feec | 2018-06-15 19:01:29 -0400 | [diff] [blame] | 346 | if (rurIPv6Address != null) { |
| 347 | pdPushNextHopIPv6.add(rurIPv6Address); |
Mayank Tiwari | c679a02 | 2018-06-23 11:19:08 -0400 | [diff] [blame] | 348 | } else { |
| 349 | log.debug("Unable to extract RUR IPv6 address from netcfg"); |
shalde064 | 280feec | 2018-06-15 19:01:29 -0400 | [diff] [blame] | 350 | } |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | log.info("PD pushing is enabled."); |
Ray Milkey | 032b964 | 2018-06-21 08:28:12 -0700 | [diff] [blame] | 354 | if (pdPushNextHopIPv4.size() != 0) { |
shalde064 | 280feec | 2018-06-15 19:01:29 -0400 | [diff] [blame] | 355 | log.info("ipv4 next-hop {} with {} items", pdPushNextHopIPv4.toString(), pdPushNextHopIPv4.size()); |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 356 | } else { |
| 357 | log.info("ipv4 next-hop is null"); |
| 358 | } |
Ray Milkey | 032b964 | 2018-06-21 08:28:12 -0700 | [diff] [blame] | 359 | if (pdPushNextHopIPv6.size() != 0) { |
shalde064 | 280feec | 2018-06-15 19:01:29 -0400 | [diff] [blame] | 360 | log.info("ipv6 next-hop={} with {} items", pdPushNextHopIPv6.toString(), pdPushNextHopIPv6.size()); |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 361 | } else { |
| 362 | log.info("ipv6 next-hop is null"); |
| 363 | } |
shalde064 | 280feec | 2018-06-15 19:01:29 -0400 | [diff] [blame] | 364 | processStaticRoutes(); |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 365 | } else { |
| 366 | log.info("PD pushing is disabled."); |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | private void startServer() { |
| 372 | HashedWheelTimer timer = new HashedWheelTimer( |
| 373 | groupedThreads("onos/fpm", "fpm-timer-%d", log)); |
| 374 | |
| 375 | ChannelFactory channelFactory = new NioServerSocketChannelFactory( |
| 376 | newCachedThreadPool(groupedThreads("onos/fpm", "sm-boss-%d", log)), |
| 377 | newCachedThreadPool(groupedThreads("onos/fpm", "sm-worker-%d", log))); |
| 378 | ChannelPipelineFactory pipelineFactory = () -> { |
| 379 | // Allocate a new session per connection |
| 380 | IdleStateHandler idleHandler = |
| 381 | new IdleStateHandler(timer, IDLE_TIMEOUT_SECS, 0, 0); |
| 382 | FpmSessionHandler fpmSessionHandler = |
| 383 | new FpmSessionHandler(this, new InternalFpmListener()); |
| 384 | FpmFrameDecoder fpmFrameDecoder = new FpmFrameDecoder(); |
| 385 | |
| 386 | // Setup the processing pipeline |
| 387 | ChannelPipeline pipeline = Channels.pipeline(); |
| 388 | pipeline.addLast("FpmFrameDecoder", fpmFrameDecoder); |
| 389 | pipeline.addLast("idle", idleHandler); |
| 390 | pipeline.addLast("FpmSession", fpmSessionHandler); |
| 391 | return pipeline; |
| 392 | }; |
| 393 | |
| 394 | InetSocketAddress listenAddress = new InetSocketAddress(FPM_PORT); |
| 395 | |
| 396 | serverBootstrap = new ServerBootstrap(channelFactory); |
| 397 | serverBootstrap.setOption("child.reuseAddr", true); |
| 398 | serverBootstrap.setOption("child.keepAlive", true); |
| 399 | serverBootstrap.setOption("child.tcpNoDelay", true); |
| 400 | serverBootstrap.setPipelineFactory(pipelineFactory); |
| 401 | try { |
| 402 | serverChannel = serverBootstrap.bind(listenAddress); |
| 403 | allChannels.add(serverChannel); |
| 404 | } catch (ChannelException e) { |
| 405 | log.debug("Exception binding to FPM port {}: ", |
| 406 | listenAddress.getPort(), e); |
| 407 | stopServer(); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | private void stopServer() { |
| 412 | allChannels.close().awaitUninterruptibly(); |
| 413 | allChannels.clear(); |
| 414 | if (serverBootstrap != null) { |
| 415 | serverBootstrap.releaseExternalResources(); |
| 416 | } |
| 417 | |
| 418 | if (clearRoutes) { |
Harshada Chaundkar | f8f7e85 | 2019-09-25 17:42:33 +0000 | [diff] [blame] | 419 | log.debug("Clearing routes for the peer"); |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 420 | peers.keySet().forEach(this::clearRoutes); |
| 421 | } |
| 422 | } |
| 423 | |
Kalhee Kim | 40beb72 | 2018-01-16 20:32:04 +0000 | [diff] [blame] | 424 | private boolean routeInDhcpStore(IpPrefix prefix) { |
| 425 | |
| 426 | if (dhcpStore != null) { |
| 427 | Collection<FpmRecord> dhcpRecords = dhcpStore.getFpmRecords(); |
| 428 | return dhcpRecords.stream().anyMatch(record -> record.ipPrefix().equals(prefix)); |
| 429 | } |
| 430 | return false; |
| 431 | } |
| 432 | |
| 433 | private boolean routeInRipStore(IpPrefix prefix) { |
| 434 | |
| 435 | if (ripStore != null) { |
| 436 | Collection<FpmRecord> ripRecords = ripStore.getFpmRecords(); |
| 437 | return ripRecords.stream().anyMatch(record -> record.ipPrefix().equals(prefix)); |
| 438 | } |
| 439 | return false; |
| 440 | } |
| 441 | |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 442 | private void fpmMessage(FpmPeer peer, FpmHeader fpmMessage) { |
| 443 | if (fpmMessage.type() == FpmHeader.FPM_TYPE_KEEPALIVE) { |
| 444 | return; |
| 445 | } |
| 446 | |
| 447 | Netlink netlink = fpmMessage.netlink(); |
| 448 | RtNetlink rtNetlink = netlink.rtNetlink(); |
| 449 | |
| 450 | if (log.isTraceEnabled()) { |
| 451 | log.trace("Received FPM message: {}", fpmMessage); |
| 452 | } |
| 453 | |
| 454 | if (!(rtNetlink.protocol() == RtProtocol.ZEBRA || |
| 455 | rtNetlink.protocol() == RtProtocol.UNSPEC)) { |
| 456 | log.trace("Ignoring non-zebra route"); |
| 457 | return; |
| 458 | } |
| 459 | |
| 460 | IpAddress dstAddress = null; |
| 461 | IpAddress gateway = null; |
| 462 | |
| 463 | for (RouteAttribute attribute : rtNetlink.attributes()) { |
| 464 | if (attribute.type() == RouteAttribute.RTA_DST) { |
| 465 | RouteAttributeDst raDst = (RouteAttributeDst) attribute; |
| 466 | dstAddress = raDst.dstAddress(); |
| 467 | } else if (attribute.type() == RouteAttribute.RTA_GATEWAY) { |
| 468 | RouteAttributeGateway raGateway = (RouteAttributeGateway) attribute; |
| 469 | gateway = raGateway.gateway(); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | if (dstAddress == null) { |
| 474 | log.error("Dst address missing!"); |
| 475 | return; |
| 476 | } |
| 477 | |
| 478 | IpPrefix prefix = IpPrefix.valueOf(dstAddress, rtNetlink.dstLength()); |
| 479 | |
Kalhee Kim | 40beb72 | 2018-01-16 20:32:04 +0000 | [diff] [blame] | 480 | // Ignore routes that we sent. |
Charles Chan | eb42a73 | 2018-06-25 13:01:35 -0700 | [diff] [blame] | 481 | if (gateway != null && ( |
| 482 | (prefix.isIp4() && pdPushNextHopIPv4 != null && |
| 483 | pdPushNextHopIPv4.contains(gateway.getIp4Address())) || |
| 484 | (prefix.isIp6() && pdPushNextHopIPv6 != null && |
| 485 | pdPushNextHopIPv6.contains(gateway.getIp6Address())))) { |
Kalhee Kim | 40beb72 | 2018-01-16 20:32:04 +0000 | [diff] [blame] | 486 | if (routeInDhcpStore(prefix) || routeInRipStore(prefix)) { |
| 487 | return; |
| 488 | } |
| 489 | } |
| 490 | |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 491 | List<Route> updates = new LinkedList<>(); |
| 492 | List<Route> withdraws = new LinkedList<>(); |
| 493 | |
| 494 | Route route; |
| 495 | switch (netlink.type()) { |
| 496 | case RTM_NEWROUTE: |
| 497 | if (gateway == null) { |
| 498 | // We ignore interface routes with no gateway for now. |
| 499 | return; |
| 500 | } |
| 501 | route = new Route(Route.Source.FPM, prefix, gateway, clusterService.getLocalNode().id()); |
| 502 | |
| 503 | |
| 504 | Route oldRoute = fpmRoutes.get(peer).put(prefix, route); |
| 505 | |
| 506 | if (oldRoute != null) { |
| 507 | log.trace("Swapping {} with {}", oldRoute, route); |
| 508 | withdraws.add(oldRoute); |
| 509 | } |
| 510 | updates.add(route); |
| 511 | break; |
| 512 | case RTM_DELROUTE: |
| 513 | Route existing = fpmRoutes.get(peer).remove(prefix); |
| 514 | if (existing == null) { |
| 515 | log.warn("Got delete for non-existent prefix"); |
| 516 | return; |
| 517 | } |
| 518 | |
| 519 | route = new Route(Route.Source.FPM, prefix, existing.nextHop(), clusterService.getLocalNode().id()); |
| 520 | |
| 521 | withdraws.add(route); |
| 522 | break; |
| 523 | case RTM_GETROUTE: |
| 524 | default: |
| 525 | break; |
| 526 | } |
| 527 | |
Charles Chan | 035ed1f | 2018-01-30 16:00:32 -0800 | [diff] [blame] | 528 | updateRouteStore(updates, withdraws); |
| 529 | } |
| 530 | |
| 531 | private synchronized void updateRouteStore(Collection<Route> routesToAdd, Collection<Route> routesToRemove) { |
| 532 | routeService.withdraw(routesToRemove); |
| 533 | routeService.update(routesToAdd); |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | private void clearRoutes(FpmPeer peer) { |
| 537 | log.info("Clearing all routes for peer {}", peer); |
| 538 | Map<IpPrefix, Route> routes = fpmRoutes.remove(peer); |
| 539 | if (routes != null) { |
Charles Chan | 035ed1f | 2018-01-30 16:00:32 -0800 | [diff] [blame] | 540 | updateRouteStore(Lists.newArrayList(), routes.values()); |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | |
| 544 | public void processStaticRoutes() { |
shalde064 | 280feec | 2018-06-15 19:01:29 -0400 | [diff] [blame] | 545 | log.debug("processStaticRoutes function is called"); |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 546 | for (Channel ch : allChannels) { |
| 547 | processStaticRoutes(ch); |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | public void processStaticRoutes(Channel ch) { |
| 552 | processRipStaticRoutes(ch); |
| 553 | processDhcpStaticRoutes(ch); |
| 554 | } |
| 555 | |
| 556 | private void processRipStaticRoutes(Channel ch) { |
| 557 | |
| 558 | /* Get RIP static routes. */ |
| 559 | if (ripStore != null) { |
| 560 | Collection<FpmRecord> ripRecords = ripStore.getFpmRecords(); |
| 561 | log.info("RIP store size is {}", ripRecords.size()); |
| 562 | |
| 563 | ripRecords.forEach(record -> sendRouteUpdateToChannel(true, |
| 564 | record.ipPrefix(), ch)); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | private void processDhcpStaticRoutes(Channel ch) { |
| 569 | |
| 570 | /* Get Dhcp static routes. */ |
| 571 | if (dhcpStore != null) { |
| 572 | Collection<FpmRecord> dhcpRecords = dhcpStore.getFpmRecords(); |
| 573 | log.info("Dhcp store size is {}", dhcpRecords.size()); |
| 574 | |
| 575 | dhcpRecords.forEach(record -> sendRouteUpdateToChannel(true, |
| 576 | record.ipPrefix(), ch)); |
| 577 | } |
| 578 | } |
| 579 | |
shalde064 | 280feec | 2018-06-15 19:01:29 -0400 | [diff] [blame] | 580 | private void updateRoute(IpAddress pdPushNextHop, boolean isAdd, IpPrefix prefix, |
| 581 | Channel ch, int raLength, short addrFamily) { |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 582 | try { |
shalde064 | 280feec | 2018-06-15 19:01:29 -0400 | [diff] [blame] | 583 | RouteAttributeDst raDst = RouteAttributeDst.builder() |
| 584 | .length(raLength) |
| 585 | .type(RouteAttribute.RTA_DST) |
| 586 | .dstAddress(prefix.address()) |
| 587 | .build(); |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 588 | |
Kalhee Kim | 715dd73 | 2018-01-23 14:39:56 +0000 | [diff] [blame] | 589 | RouteAttributeGateway raGateway = RouteAttributeGateway.builder() |
shalde064 | 280feec | 2018-06-15 19:01:29 -0400 | [diff] [blame] | 590 | .length(raLength) |
| 591 | .type(RouteAttribute.RTA_GATEWAY) |
| 592 | .gateway(pdPushNextHop) |
| 593 | .build(); |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 594 | |
Kalhee Kim | 715dd73 | 2018-01-23 14:39:56 +0000 | [diff] [blame] | 595 | // Build RtNetlink. |
| 596 | RtNetlink rtNetlink = RtNetlink.builder() |
shalde064 | 280feec | 2018-06-15 19:01:29 -0400 | [diff] [blame] | 597 | .addressFamily(addrFamily) |
| 598 | .dstLength(prefix.prefixLength()) |
| 599 | .routeAttribute(raDst) |
| 600 | .routeAttribute(raGateway) |
| 601 | .build(); |
Kalhee Kim | 715dd73 | 2018-01-23 14:39:56 +0000 | [diff] [blame] | 602 | |
| 603 | // Build Netlink. |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 604 | int messageLength = raDst.length() + raGateway.length() + |
shalde064 | 280feec | 2018-06-15 19:01:29 -0400 | [diff] [blame] | 605 | RtNetlink.RT_NETLINK_LENGTH + Netlink.NETLINK_HEADER_LENGTH; |
Kalhee Kim | 715dd73 | 2018-01-23 14:39:56 +0000 | [diff] [blame] | 606 | Netlink netLink = Netlink.builder() |
shalde064 | 280feec | 2018-06-15 19:01:29 -0400 | [diff] [blame] | 607 | .length(messageLength) |
| 608 | .type(isAdd ? NetlinkMessageType.RTM_NEWROUTE : NetlinkMessageType.RTM_DELROUTE) |
| 609 | .flags(Netlink.NETLINK_REQUEST | Netlink.NETLINK_CREATE) |
| 610 | .rtNetlink(rtNetlink) |
| 611 | .build(); |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 612 | |
Kalhee Kim | 715dd73 | 2018-01-23 14:39:56 +0000 | [diff] [blame] | 613 | // Build FpmHeader. |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 614 | messageLength += FpmHeader.FPM_HEADER_LENGTH; |
Kalhee Kim | 715dd73 | 2018-01-23 14:39:56 +0000 | [diff] [blame] | 615 | FpmHeader fpmMessage = FpmHeader.builder() |
shalde064 | 280feec | 2018-06-15 19:01:29 -0400 | [diff] [blame] | 616 | .version(FpmHeader.FPM_VERSION_1) |
| 617 | .type(FpmHeader.FPM_TYPE_NETLINK) |
| 618 | .length(messageLength) |
| 619 | .netlink(netLink) |
| 620 | .build(); |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 621 | |
| 622 | // Encode message in a channel buffer and transmit. |
| 623 | ch.write(fpmMessage.encode()); |
Harshada Chaundkar | f8f7e85 | 2019-09-25 17:42:33 +0000 | [diff] [blame] | 624 | log.debug("Fpm Message for updated route {}", fpmMessage.toString()); |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 625 | } catch (RuntimeException e) { |
| 626 | log.info("Route not sent over fpm connection."); |
| 627 | } |
| 628 | } |
| 629 | |
shalde064 | 280feec | 2018-06-15 19:01:29 -0400 | [diff] [blame] | 630 | private void sendRouteUpdateToChannel(boolean isAdd, IpPrefix prefix, Channel ch) { |
| 631 | |
| 632 | if (!pdPushEnabled) { |
| 633 | return; |
| 634 | } |
| 635 | int raLength; |
| 636 | short addrFamily; |
| 637 | |
| 638 | // Build route attributes. |
| 639 | if (prefix.isIp4()) { |
| 640 | List<Ip4Address> pdPushNextHopList; |
| 641 | if (pdPushNextHopIPv4 == null || pdPushNextHopIPv4.size() == 0) { |
| 642 | log.info("Prefix not pushed because ipv4 next-hop is null."); |
| 643 | return; |
| 644 | } |
| 645 | pdPushNextHopList = pdPushNextHopIPv4; |
| 646 | raLength = Ip4Address.BYTE_LENGTH + RouteAttribute.ROUTE_ATTRIBUTE_HEADER_LENGTH; |
| 647 | addrFamily = RtNetlink.RT_ADDRESS_FAMILY_INET; |
| 648 | for (Ip4Address pdPushNextHop: pdPushNextHopList) { |
Harshada Chaundkar | 1a098eb | 2019-01-15 00:05:57 +0000 | [diff] [blame] | 649 | log.trace("IPv4 next hop is:" + pdPushNextHop); |
shalde064 | 280feec | 2018-06-15 19:01:29 -0400 | [diff] [blame] | 650 | updateRoute(pdPushNextHop, isAdd, prefix, ch, raLength, addrFamily); |
| 651 | } |
| 652 | } else { |
| 653 | List<Ip6Address> pdPushNextHopList; |
| 654 | if (pdPushNextHopIPv6 == null || pdPushNextHopIPv6.size() == 0) { |
| 655 | log.info("Prefix not pushed because ipv6 next-hop is null."); |
| 656 | return; |
| 657 | } |
| 658 | pdPushNextHopList = pdPushNextHopIPv6; |
| 659 | raLength = Ip6Address.BYTE_LENGTH + RouteAttribute.ROUTE_ATTRIBUTE_HEADER_LENGTH; |
| 660 | addrFamily = RtNetlink.RT_ADDRESS_FAMILY_INET6; |
| 661 | for (Ip6Address pdPushNextHop: pdPushNextHopList) { |
Harshada Chaundkar | 1a098eb | 2019-01-15 00:05:57 +0000 | [diff] [blame] | 662 | log.trace("IPv6 next hop is:" + pdPushNextHop); |
shalde064 | 280feec | 2018-06-15 19:01:29 -0400 | [diff] [blame] | 663 | updateRoute(pdPushNextHop, isAdd, prefix, ch, raLength, addrFamily); |
| 664 | } |
| 665 | } |
| 666 | } |
| 667 | |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 668 | private void sendRouteUpdate(boolean isAdd, IpPrefix prefix) { |
| 669 | |
| 670 | for (Channel ch : allChannels) { |
| 671 | sendRouteUpdateToChannel(isAdd, prefix, ch); |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | public boolean isPdPushEnabled() { |
| 676 | return pdPushEnabled; |
| 677 | } |
| 678 | |
| 679 | private FpmPeerInfo toFpmInfo(FpmPeer peer, Collection<FpmConnectionInfo> connections) { |
| 680 | return new FpmPeerInfo(connections, |
| 681 | fpmRoutes.getOrDefault(peer, Collections.emptyMap()).size()); |
| 682 | } |
| 683 | |
| 684 | @Override |
| 685 | public Map<FpmPeer, FpmPeerInfo> peers() { |
| 686 | return peers.asJavaMap().entrySet().stream() |
| 687 | .collect(Collectors.toMap( |
| 688 | e -> e.getKey(), |
| 689 | e -> toFpmInfo(e.getKey(), e.getValue()))); |
| 690 | } |
| 691 | |
William Davies | 5bae505 | 2019-10-19 01:25:01 -0700 | [diff] [blame^] | 692 | @Override |
| 693 | public void updateAcceptRouteFlag(Collection<FpmPeerAcceptRoutes> modifiedPeers) { |
| 694 | modifiedPeers.forEach(modifiedPeer -> { |
| 695 | log.debug("FPM connection to {} is disabled", modifiedPeer); |
| 696 | NodeId localNode = clusterService.getLocalNode().id(); |
| 697 | log.debug("Peer Flag {}", modifiedPeer.isAcceptRoutes()); |
| 698 | peers.compute(modifiedPeer.peer(), (p, infos) -> { |
| 699 | if (infos == null) { |
| 700 | return null; |
| 701 | } |
| 702 | Iterator<FpmConnectionInfo> iterator = infos.iterator(); |
| 703 | if (iterator.hasNext()) { |
| 704 | FpmConnectionInfo connectionInfo = iterator.next(); |
| 705 | if (connectionInfo.isAcceptRoutes() == modifiedPeer.isAcceptRoutes()) { |
| 706 | return null; |
| 707 | } |
| 708 | localPeers.remove(modifiedPeer.peer()); |
| 709 | infos.remove(connectionInfo); |
| 710 | infos.add(new FpmConnectionInfo(localNode, modifiedPeer.peer(), |
| 711 | System.currentTimeMillis(), modifiedPeer.isAcceptRoutes())); |
| 712 | localPeers.put(modifiedPeer.peer(), infos); |
| 713 | } |
| 714 | Map<IpPrefix, Route> routes = fpmRoutes.get(modifiedPeer.peer()); |
| 715 | if (routes != null && !modifiedPeer.isAcceptRoutes()) { |
| 716 | updateRouteStore(Lists.newArrayList(), routes.values()); |
| 717 | } else { |
| 718 | updateRouteStore(routes.values(), Lists.newArrayList()); |
| 719 | } |
| 720 | |
| 721 | return infos; |
| 722 | }); |
| 723 | }); |
| 724 | |
| 725 | } |
| 726 | |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 727 | private class InternalFpmListener implements FpmListener { |
| 728 | @Override |
| 729 | public void fpmMessage(FpmPeer peer, FpmHeader fpmMessage) { |
| 730 | FpmManager.this.fpmMessage(peer, fpmMessage); |
| 731 | } |
| 732 | |
| 733 | @Override |
| 734 | public boolean peerConnected(FpmPeer peer) { |
Harshada Chaundkar | f8f7e85 | 2019-09-25 17:42:33 +0000 | [diff] [blame] | 735 | log.info("FPM connection to {} was connected", peer); |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 736 | if (peers.keySet().contains(peer)) { |
| 737 | return false; |
| 738 | } |
| 739 | |
| 740 | NodeId localNode = clusterService.getLocalNode().id(); |
| 741 | peers.compute(peer, (p, infos) -> { |
| 742 | if (infos == null) { |
| 743 | infos = new HashSet<>(); |
| 744 | } |
| 745 | |
William Davies | 5bae505 | 2019-10-19 01:25:01 -0700 | [diff] [blame^] | 746 | infos.add(new FpmConnectionInfo(localNode, peer, System.currentTimeMillis(), true)); |
Andrea Campanella | 4310f6e | 2018-03-27 16:35:39 -0700 | [diff] [blame] | 747 | localPeers.put(peer, infos); |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 748 | return infos; |
| 749 | }); |
| 750 | |
| 751 | fpmRoutes.computeIfAbsent(peer, p -> new ConcurrentHashMap<>()); |
| 752 | return true; |
| 753 | } |
| 754 | |
| 755 | @Override |
| 756 | public void peerDisconnected(FpmPeer peer) { |
| 757 | log.info("FPM connection to {} went down", peer); |
| 758 | |
| 759 | if (clearRoutes) { |
| 760 | clearRoutes(peer); |
| 761 | } |
| 762 | |
| 763 | peers.compute(peer, (p, infos) -> { |
| 764 | if (infos == null) { |
| 765 | return null; |
| 766 | } |
| 767 | |
| 768 | infos.stream() |
| 769 | .filter(i -> i.connectedTo().equals(clusterService.getLocalNode().id())) |
| 770 | .findAny() |
Andrea Campanella | 4310f6e | 2018-03-27 16:35:39 -0700 | [diff] [blame] | 771 | .ifPresent(i -> { |
| 772 | infos.remove(i); |
| 773 | localPeers.get(peer).remove(i); |
| 774 | }); |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 775 | |
| 776 | if (infos.isEmpty()) { |
| 777 | return null; |
| 778 | } |
| 779 | |
| 780 | return infos; |
| 781 | }); |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | /** |
| 786 | * Adds a channel to the channel group. |
| 787 | * |
| 788 | * @param channel the channel to add |
| 789 | */ |
| 790 | public void addSessionChannel(Channel channel) { |
| 791 | allChannels.add(channel); |
| 792 | } |
| 793 | |
| 794 | /** |
| 795 | * Removes a channel from the channel group. |
| 796 | * |
| 797 | * @param channel the channel to remove |
| 798 | */ |
| 799 | public void removeSessionChannel(Channel channel) { |
| 800 | allChannels.remove(channel); |
| 801 | } |
| 802 | |
| 803 | /** |
| 804 | * Store delegate for Fpm Prefix store. |
| 805 | * Handles Fpm prefix store event. |
| 806 | */ |
| 807 | class FpmPrefixStoreDelegate implements StoreDelegate<FpmPrefixStoreEvent> { |
| 808 | |
| 809 | @Override |
| 810 | public void notify(FpmPrefixStoreEvent e) { |
| 811 | |
| 812 | log.trace("FpmPrefixStoreEvent notify"); |
| 813 | |
| 814 | FpmRecord record = e.subject(); |
| 815 | switch (e.type()) { |
| 816 | case ADD: |
| 817 | sendRouteUpdate(true, record.ipPrefix()); |
| 818 | break; |
| 819 | case REMOVE: |
| 820 | sendRouteUpdate(false, record.ipPrefix()); |
| 821 | break; |
| 822 | default: |
| 823 | log.warn("unsupported store event type", e.type()); |
| 824 | return; |
| 825 | } |
| 826 | } |
| 827 | } |
Charles Chan | 035ed1f | 2018-01-30 16:00:32 -0800 | [diff] [blame] | 828 | |
| 829 | private class InternalClusterListener implements ClusterEventListener { |
| 830 | @Override |
| 831 | public void event(ClusterEvent event) { |
Jordan Halterman | aa2faca | 2018-08-13 02:41:50 -0700 | [diff] [blame] | 832 | clusterEventExecutor.execute(() -> { |
Andrea Campanella | 4310f6e | 2018-03-27 16:35:39 -0700 | [diff] [blame] | 833 | log.info("Receives ClusterEvent {} for {}", event.type(), event.subject().id()); |
Jordan Halterman | aa2faca | 2018-08-13 02:41:50 -0700 | [diff] [blame] | 834 | switch (event.type()) { |
| 835 | case INSTANCE_READY: |
| 836 | // When current node is healing from a network partition, |
| 837 | // seeing INSTANCE_READY means current node has the ability to read from the cluster, |
| 838 | // but it is possible that current node still can't write to the cluster at this moment. |
| 839 | // The AsyncDistributedLock is introduced to ensure we attempt to push FPM routes |
| 840 | // after current node can write. |
| 841 | // Adding 15 seconds retry for the current node to be able to write. |
| 842 | asyncLock.tryLock(Duration.ofSeconds(15)).whenComplete((result, error) -> { |
| 843 | if (result != null && result.isPresent()) { |
| 844 | log.debug("Lock obtained. Push local FPM routes to route store"); |
| 845 | // All FPM routes on current node will be pushed again even when current node is not |
| 846 | // the one that becomes READY. A better way is to do this only on the minority nodes. |
| 847 | pushFpmRoutes(); |
| 848 | localPeers.forEach((key, value) -> peers.put(key, value)); |
| 849 | asyncLock.unlock(); |
| 850 | } else { |
| 851 | log.debug("Fail to obtain lock. Abort."); |
| 852 | } |
| 853 | }); |
| 854 | break; |
| 855 | case INSTANCE_DEACTIVATED: |
| 856 | case INSTANCE_REMOVED: |
| 857 | ImmutableMap.copyOf(peers.asJavaMap()).forEach((key, value) -> { |
| 858 | if (value != null) { |
| 859 | value.stream() |
Andrea Campanella | 4310f6e | 2018-03-27 16:35:39 -0700 | [diff] [blame] | 860 | .filter(i -> i.connectedTo().equals(event.subject().id())) |
| 861 | .findAny() |
| 862 | .ifPresent(value::remove); |
Harshada Chaundkar | f8f7e85 | 2019-09-25 17:42:33 +0000 | [diff] [blame] | 863 | log.info("Connection {} removed for disabled peer {}", value, key); |
Jordan Halterman | aa2faca | 2018-08-13 02:41:50 -0700 | [diff] [blame] | 864 | if (value.isEmpty()) { |
| 865 | peers.remove(key); |
| 866 | } |
Andrea Campanella | 4310f6e | 2018-03-27 16:35:39 -0700 | [diff] [blame] | 867 | } |
Jordan Halterman | aa2faca | 2018-08-13 02:41:50 -0700 | [diff] [blame] | 868 | }); |
| 869 | break; |
| 870 | case INSTANCE_ADDED: |
| 871 | case INSTANCE_ACTIVATED: |
| 872 | default: |
| 873 | break; |
| 874 | } |
| 875 | }); |
Charles Chan | 035ed1f | 2018-01-30 16:00:32 -0800 | [diff] [blame] | 876 | } |
| 877 | } |
| 878 | |
Saurav Das | e7f5101 | 2018-02-09 17:26:45 -0800 | [diff] [blame] | 879 | @Override |
Charles Chan | 035ed1f | 2018-01-30 16:00:32 -0800 | [diff] [blame] | 880 | public void pushFpmRoutes() { |
| 881 | Set<Route> routes = fpmRoutes.values().stream() |
| 882 | .map(Map::entrySet).flatMap(Set::stream).map(Map.Entry::getValue) |
| 883 | .collect(Collectors.toSet()); |
| 884 | updateRouteStore(routes, Lists.newArrayList()); |
| 885 | log.info("{} FPM routes have been updated to route store", routes.size()); |
| 886 | } |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 887 | } |