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 | |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ConcurrentHashMultiset; |
| 19 | import com.google.common.collect.HashMultimap; |
| 20 | import com.google.common.collect.Maps; |
| 21 | import com.google.common.collect.Multimap; |
| 22 | import com.google.common.collect.Multiset; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Activate; |
| 24 | import org.apache.felix.scr.annotations.Component; |
| 25 | import org.apache.felix.scr.annotations.Deactivate; |
| 26 | import org.apache.felix.scr.annotations.Reference; |
| 27 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 28 | import org.onlab.packet.Ethernet; |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 29 | import org.onlab.packet.IpAddress; |
| 30 | import org.onlab.packet.IpPrefix; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 31 | import org.onosproject.core.ApplicationId; |
| 32 | import org.onosproject.core.CoreService; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 33 | import org.onosproject.net.config.NetworkConfigService; |
| 34 | import org.onosproject.incubator.net.intf.Interface; |
| 35 | import org.onosproject.incubator.net.intf.InterfaceService; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 36 | import org.onosproject.net.DeviceId; |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 37 | import org.onosproject.net.device.DeviceEvent; |
| 38 | import org.onosproject.net.device.DeviceListener; |
| 39 | import org.onosproject.net.device.DeviceService; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 40 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 41 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 42 | import org.onosproject.net.flow.TrafficSelector; |
| 43 | import org.onosproject.net.flow.TrafficTreatment; |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 44 | import org.onosproject.net.flow.criteria.Criteria; |
| 45 | import org.onosproject.net.flowobjective.DefaultFilteringObjective; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 46 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 47 | import org.onosproject.net.flowobjective.DefaultNextObjective; |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 48 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 49 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 50 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 51 | import org.onosproject.net.flowobjective.NextObjective; |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 52 | import org.onosproject.net.flowobjective.Objective; |
| 53 | import org.onosproject.net.flowobjective.ObjectiveContext; |
| 54 | import org.onosproject.net.flowobjective.ObjectiveError; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 55 | import org.onosproject.net.packet.PacketService; |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 56 | import org.onosproject.routing.FibEntry; |
Jonathan Hart | 2da1e60 | 2015-02-18 19:09:24 -0800 | [diff] [blame] | 57 | import org.onosproject.routing.FibListener; |
| 58 | import org.onosproject.routing.FibUpdate; |
| 59 | import org.onosproject.routing.RoutingService; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 60 | import org.onosproject.routing.config.BgpConfig; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 61 | import org.slf4j.Logger; |
| 62 | import org.slf4j.LoggerFactory; |
| 63 | |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 64 | import java.util.Collection; |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 65 | import java.util.HashMap; |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 66 | import java.util.Map; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 67 | import java.util.Optional; |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 68 | import java.util.Set; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 69 | |
Saurav Das | dfc639e | 2015-04-30 11:48:16 -0700 | [diff] [blame] | 70 | /* For test only - will be removed before Cardinal release |
| 71 | import org.onlab.packet.Ip4Address; |
| 72 | import org.onlab.packet.Ip4Prefix; |
| 73 | import org.onlab.packet.MacAddress; |
| 74 | import java.util.Collections; |
| 75 | import static org.onlab.util.Tools.delay; |
| 76 | */ |
| 77 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 78 | /** |
| 79 | * BgpRouter component. |
| 80 | */ |
| 81 | @Component(immediate = true) |
| 82 | public class BgpRouter { |
| 83 | |
| 84 | private static final Logger log = LoggerFactory.getLogger(BgpRouter.class); |
| 85 | |
| 86 | private static final String BGP_ROUTER_APP = "org.onosproject.bgprouter"; |
| 87 | |
Jonathan Hart | 12ef205 | 2015-03-10 13:58:13 -0700 | [diff] [blame] | 88 | private static final int PRIORITY_OFFSET = 100; |
| 89 | private static final int PRIORITY_MULTIPLIER = 5; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 90 | |
| 91 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 92 | protected CoreService coreService; |
| 93 | |
| 94 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 95 | protected RoutingService routingService; |
| 96 | |
| 97 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 98 | protected InterfaceService interfaceService; |
| 99 | |
| 100 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 101 | protected NetworkConfigService networkConfigService; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 102 | |
| 103 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 104 | protected PacketService packetService; |
| 105 | |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 106 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 107 | protected FlowObjectiveService flowObjectiveService; |
| 108 | |
| 109 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 110 | protected DeviceService deviceService; |
| 111 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 112 | private ApplicationId appId; |
| 113 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 114 | // Reference count for how many times a next hop is used by a route |
| 115 | private final Multiset<IpAddress> nextHopsCount = ConcurrentHashMultiset.create(); |
| 116 | |
| 117 | // Mapping from prefix to its current next hop |
| 118 | private final Map<IpPrefix, IpAddress> prefixToNextHop = Maps.newHashMap(); |
| 119 | |
| 120 | // Mapping from next hop IP to next hop object containing group info |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 121 | private final Map<IpAddress, Integer> nextHops = Maps.newHashMap(); |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 122 | |
| 123 | // Stores FIB updates that are waiting for groups to be set up |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 124 | private final Multimap<NextHopGroupKey, FibEntry> pendingUpdates = HashMultimap.create(); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 125 | |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 126 | // Device id of data-plane switch - should be learned from config |
| 127 | private DeviceId deviceId; |
| 128 | |
| 129 | // Device id of control-plane switch (OVS) connected to BGP Speaker - should be |
| 130 | // learned from config |
| 131 | private DeviceId ctrlDeviceId; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 132 | |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 133 | // Responsible for handling BGP traffic (encapsulated within OF messages) |
| 134 | // between the data-plane switch and the Quagga VM using a control plane OVS. |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 135 | private TunnellingConnectivityManager connectivityManager; |
| 136 | |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 137 | private DeviceListener deviceListener; |
sangho | 5eaf033 | 2015-03-09 15:08:12 -0700 | [diff] [blame] | 138 | private IcmpHandler icmpHandler; |
| 139 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 140 | @Activate |
| 141 | protected void activate() { |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 142 | appId = coreService.registerApplication(BGP_ROUTER_APP); |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 143 | |
| 144 | ApplicationId routerAppId = coreService.getAppId(RoutingService.ROUTER_APP_ID); |
| 145 | BgpConfig bgpConfig = |
| 146 | networkConfigService.getConfig(routerAppId, RoutingService.CONFIG_CLASS); |
| 147 | |
| 148 | if (bgpConfig == null) { |
| 149 | log.error("No BgpConfig found"); |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | getDeviceConfiguration(bgpConfig); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 154 | |
| 155 | connectivityManager = new TunnellingConnectivityManager(appId, |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 156 | bgpConfig, |
| 157 | interfaceService, |
Jonathan Hart | 936a729 | 2015-03-06 18:02:57 -0800 | [diff] [blame] | 158 | packetService, |
Saurav Das | 3d03826 | 2015-04-23 12:36:58 -0700 | [diff] [blame] | 159 | flowObjectiveService); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 160 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 161 | icmpHandler = new IcmpHandler(interfaceService, packetService); |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 162 | deviceListener = new InnerDeviceListener(); |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 163 | routingService.addFibListener(new InternalFibListener()); |
| 164 | routingService.start(); |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 165 | deviceService.addListener(deviceListener); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 166 | connectivityManager.start(); |
sangho | 5eaf033 | 2015-03-09 15:08:12 -0700 | [diff] [blame] | 167 | icmpHandler.start(); |
| 168 | |
Jonathan Hart | 49bcae9 | 2015-06-04 15:33:15 -0700 | [diff] [blame] | 169 | // Initialize devices now if they are already connected |
| 170 | if (deviceService.isAvailable(deviceId)) { |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 171 | processIntfFilters(true, interfaceService.getInterfaces()); |
Jonathan Hart | 49bcae9 | 2015-06-04 15:33:15 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | if (deviceService.isAvailable(ctrlDeviceId)) { |
| 175 | connectivityManager.notifySwitchAvailable(); |
| 176 | } |
| 177 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 178 | log.info("BgpRouter started"); |
| 179 | } |
| 180 | |
| 181 | @Deactivate |
| 182 | protected void deactivate() { |
| 183 | routingService.stop(); |
| 184 | connectivityManager.stop(); |
sangho | 5eaf033 | 2015-03-09 15:08:12 -0700 | [diff] [blame] | 185 | icmpHandler.stop(); |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 186 | deviceService.removeListener(deviceListener); |
| 187 | //processIntfFilters(false, configService.getInterfaces()); //TODO necessary? |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 188 | log.info("BgpRouter stopped"); |
| 189 | } |
| 190 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 191 | private void getDeviceConfiguration(BgpConfig bgpConfig) { |
| 192 | Optional<BgpConfig.BgpSpeakerConfig> bgpSpeaker = |
| 193 | bgpConfig.bgpSpeakers().stream().findAny(); |
| 194 | |
| 195 | if (!bgpSpeaker.isPresent()) { |
| 196 | log.error("BGP speaker configuration not found"); |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 197 | return; |
| 198 | } |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 199 | |
| 200 | ctrlDeviceId = bgpSpeaker.get().connectPoint().deviceId(); |
| 201 | |
| 202 | Optional<IpAddress> peerAddress = |
| 203 | bgpSpeaker.get().peers().stream().findAny(); |
| 204 | |
| 205 | if (!peerAddress.isPresent()) { |
| 206 | log.error("BGP speaker must have peers configured"); |
| 207 | return; |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 208 | } |
sangho | 5eaf033 | 2015-03-09 15:08:12 -0700 | [diff] [blame] | 209 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 210 | Interface intf = interfaceService.getMatchingInterface(peerAddress.get()); |
| 211 | |
| 212 | if (intf == null) { |
| 213 | log.error("No interface found for peer"); |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | // Assume all peers are configured on the same device - this is required |
| 218 | // by the BGP router |
| 219 | deviceId = intf.connectPoint().deviceId(); |
| 220 | |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 221 | log.info("Router dpid: {}", deviceId); |
| 222 | log.info("Control Plane OVS dpid: {}", ctrlDeviceId); |
| 223 | } |
| 224 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 225 | private void updateFibEntry(Collection<FibUpdate> updates) { |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 226 | Map<FibEntry, Integer> toInstall = new HashMap<>(updates.size()); |
Jonathan Hart | 5b14142 | 2015-03-06 12:59:09 -0800 | [diff] [blame] | 227 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 228 | for (FibUpdate update : updates) { |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 229 | FibEntry entry = update.entry(); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 230 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 231 | addNextHop(entry); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 232 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 233 | Integer nextId; |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 234 | synchronized (pendingUpdates) { |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 235 | nextId = nextHops.get(entry.nextHopIp()); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 236 | } |
| 237 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 238 | toInstall.put(update.entry(), nextId); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 239 | } |
Jonathan Hart | 5b14142 | 2015-03-06 12:59:09 -0800 | [diff] [blame] | 240 | |
| 241 | installFlows(toInstall); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 242 | } |
| 243 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 244 | private void installFlows(Map<FibEntry, Integer> entriesToInstall) { |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 245 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 246 | for (Map.Entry<FibEntry, Integer> entry : entriesToInstall.entrySet()) { |
Jonathan Hart | 5b14142 | 2015-03-06 12:59:09 -0800 | [diff] [blame] | 247 | FibEntry fibEntry = entry.getKey(); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 248 | Integer nextId = entry.getValue(); |
Jonathan Hart | 5b14142 | 2015-03-06 12:59:09 -0800 | [diff] [blame] | 249 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 250 | flowObjectiveService.forward(deviceId, |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 251 | generateRibForwardingObj(fibEntry.prefix(), nextId).add()); |
Saurav Das | 3d03826 | 2015-04-23 12:36:58 -0700 | [diff] [blame] | 252 | log.trace("Sending forwarding objective {} -> nextId:{}", fibEntry, nextId); |
Jonathan Hart | 5b14142 | 2015-03-06 12:59:09 -0800 | [diff] [blame] | 253 | } |
| 254 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | private synchronized void deleteFibEntry(Collection<FibUpdate> withdraws) { |
Jonathan Hart | 5b14142 | 2015-03-06 12:59:09 -0800 | [diff] [blame] | 258 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 259 | for (FibUpdate update : withdraws) { |
| 260 | FibEntry entry = update.entry(); |
sangho | dde53d1 | 2015-04-30 10:34:41 -0700 | [diff] [blame] | 261 | //Integer nextId = nextHops.get(entry.nextHopIp()); |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 262 | |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 263 | /* Group group = deleteNextHop(entry.prefix()); |
Jonathan Hart | 37d659c | 2015-03-08 19:20:38 -0700 | [diff] [blame] | 264 | if (group == null) { |
| 265 | log.warn("Group not found when deleting {}", entry); |
| 266 | return; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 267 | }*/ |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 268 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 269 | flowObjectiveService.forward(deviceId, |
sangho | dde53d1 | 2015-04-30 10:34:41 -0700 | [diff] [blame] | 270 | generateRibForwardingObj(entry.prefix(), null).remove()); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 271 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 272 | } |
Jonathan Hart | 5b14142 | 2015-03-06 12:59:09 -0800 | [diff] [blame] | 273 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 276 | private ForwardingObjective.Builder generateRibForwardingObj(IpPrefix prefix, |
| 277 | Integer nextId) { |
Jonathan Hart | 37d659c | 2015-03-08 19:20:38 -0700 | [diff] [blame] | 278 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 279 | .matchEthType(Ethernet.TYPE_IPV4) |
| 280 | .matchIPDst(prefix) |
| 281 | .build(); |
| 282 | |
Jonathan Hart | 12ef205 | 2015-03-10 13:58:13 -0700 | [diff] [blame] | 283 | int priority = prefix.prefixLength() * PRIORITY_MULTIPLIER + PRIORITY_OFFSET; |
| 284 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 285 | ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective.builder() |
| 286 | .fromApp(appId) |
| 287 | .makePermanent() |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 288 | .withSelector(selector) |
| 289 | .withPriority(priority) |
| 290 | .withFlag(ForwardingObjective.Flag.SPECIFIC); |
| 291 | |
sangho | dde53d1 | 2015-04-30 10:34:41 -0700 | [diff] [blame] | 292 | if (nextId == null) { |
Saurav Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame] | 293 | // Route withdraws are not specified with next hops. Generating |
| 294 | // dummy treatment as there is no equivalent nextId info. |
sangho | dde53d1 | 2015-04-30 10:34:41 -0700 | [diff] [blame] | 295 | fwdBuilder.withTreatment(DefaultTrafficTreatment.builder().build()); |
| 296 | } else { |
| 297 | fwdBuilder.nextStep(nextId); |
| 298 | } |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 299 | return fwdBuilder; |
Jonathan Hart | 37d659c | 2015-03-08 19:20:38 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 302 | private synchronized void addNextHop(FibEntry entry) { |
| 303 | prefixToNextHop.put(entry.prefix(), entry.nextHopIp()); |
| 304 | if (nextHopsCount.count(entry.nextHopIp()) == 0) { |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 305 | // There was no next hop in the multiset |
| 306 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 307 | Interface egressIntf = interfaceService.getMatchingInterface(entry.nextHopIp()); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 308 | if (egressIntf == null) { |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 309 | log.warn("no egress interface found for {}", entry); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 310 | return; |
| 311 | } |
| 312 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 313 | NextHopGroupKey groupKey = new NextHopGroupKey(entry.nextHopIp()); |
| 314 | |
| 315 | NextHop nextHop = new NextHop(entry.nextHopIp(), entry.nextHopMac(), groupKey); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 316 | |
| 317 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 318 | .setEthSrc(egressIntf.mac()) |
| 319 | .setEthDst(nextHop.mac()) |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 320 | .pushVlan() |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 321 | .setVlanId(egressIntf.vlan()) |
alshabib | da1644e | 2015-03-13 14:01:35 -0700 | [diff] [blame] | 322 | .setVlanPcp((byte) 0) |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 323 | .setOutput(egressIntf.connectPoint().port()) |
| 324 | .build(); |
| 325 | |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 326 | int nextId = flowObjectiveService.allocateNextId(); |
| 327 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 328 | NextObjective nextObjective = DefaultNextObjective.builder() |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 329 | .withId(nextId) |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 330 | .addTreatment(treatment) |
| 331 | .withType(NextObjective.Type.SIMPLE) |
| 332 | .fromApp(appId) |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 333 | .add(); // TODO add callbacks |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 334 | |
| 335 | flowObjectiveService.next(deviceId, nextObjective); |
| 336 | |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 337 | nextHops.put(nextHop.ip(), nextId); |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 338 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 339 | } |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 340 | |
| 341 | nextHopsCount.add(entry.nextHopIp()); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 342 | } |
| 343 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 344 | /*private synchronized Group deleteNextHop(IpPrefix prefix) { |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 345 | IpAddress nextHopIp = prefixToNextHop.remove(prefix); |
| 346 | NextHop nextHop = nextHops.get(nextHopIp); |
| 347 | if (nextHop == null) { |
| 348 | log.warn("No next hop found when removing prefix {}", prefix); |
Jonathan Hart | 37d659c | 2015-03-08 19:20:38 -0700 | [diff] [blame] | 349 | return null; |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 350 | } |
| 351 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 352 | Group group = groupService.getGroup(deviceId, |
| 353 | new DefaultGroupKey(appKryo. |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 354 | serialize(nextHop.group()))); |
Jonathan Hart | 37d659c | 2015-03-08 19:20:38 -0700 | [diff] [blame] | 355 | |
Jonathan Hart | f9f2cbb | 2015-03-12 17:44:03 -0700 | [diff] [blame] | 356 | // FIXME disabling group deletes for now until we verify the logic is OK |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 357 | if (nextHopsCount.remove(nextHopIp, 1) <= 1) { |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 358 | // There was one or less next hops, so there are now none |
| 359 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 360 | log.debug("removing group for next hop {}", nextHop); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 361 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 362 | nextHops.remove(nextHopIp); |
| 363 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 364 | groupService.removeGroup(deviceId, |
| 365 | new DefaultGroupKey(appKryo.build().serialize(nextHop.group())), |
| 366 | appId); |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 367 | } |
Jonathan Hart | 37d659c | 2015-03-08 19:20:38 -0700 | [diff] [blame] | 368 | |
| 369 | return group; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 370 | }*/ |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 371 | |
| 372 | private class InternalFibListener implements FibListener { |
| 373 | |
| 374 | @Override |
| 375 | public void update(Collection<FibUpdate> updates, |
| 376 | Collection<FibUpdate> withdraws) { |
| 377 | BgpRouter.this.deleteFibEntry(withdraws); |
| 378 | BgpRouter.this.updateFibEntry(updates); |
| 379 | } |
| 380 | } |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 381 | |
Saurav Das | cfd63d2 | 2015-04-13 16:08:24 -0700 | [diff] [blame] | 382 | private void processIntfFilters(boolean install, Set<Interface> intfs) { |
| 383 | log.info("Processing {} router interfaces", intfs.size()); |
| 384 | for (Interface intf : intfs) { |
Jonathan Hart | 49bcae9 | 2015-06-04 15:33:15 -0700 | [diff] [blame] | 385 | if (!intf.connectPoint().deviceId().equals(deviceId)) { |
| 386 | // Ignore interfaces if they are not on the router switch |
| 387 | continue; |
| 388 | } |
| 389 | |
Saurav Das | cfd63d2 | 2015-04-13 16:08:24 -0700 | [diff] [blame] | 390 | FilteringObjective.Builder fob = DefaultFilteringObjective.builder(); |
| 391 | fob.withKey(Criteria.matchInPort(intf.connectPoint().port())) |
| 392 | .addCondition(Criteria.matchEthDst(intf.mac())) |
| 393 | .addCondition(Criteria.matchVlanId(intf.vlan())); |
| 394 | intf.ipAddresses().stream() |
| 395 | .forEach(ipaddr -> fob.addCondition( |
Saurav Das | c39f603 | 2015-05-14 17:12:47 -0700 | [diff] [blame] | 396 | Criteria.matchIPDst( |
| 397 | IpPrefix.valueOf(ipaddr.ipAddress(), 32)))); |
Saurav Das | cfd63d2 | 2015-04-13 16:08:24 -0700 | [diff] [blame] | 398 | fob.permit().fromApp(appId); |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 399 | flowObjectiveService.filter( |
| 400 | deviceId, |
| 401 | fob.add(new ObjectiveContext() { |
| 402 | @Override |
| 403 | public void onSuccess(Objective objective) { |
| 404 | log.info("Successfully installed interface based " |
Saurav Das | c39f603 | 2015-05-14 17:12:47 -0700 | [diff] [blame] | 405 | + "filtering objectives for intf {}", intf); |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | @Override |
| 409 | public void onError(Objective objective, |
| 410 | ObjectiveError error) { |
Saurav Das | f9ba422 | 2015-05-07 17:13:59 -0700 | [diff] [blame] | 411 | log.error("Failed to install interface filters for intf {}: {}", |
| 412 | intf, error); |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 413 | // TODO something more than just logging |
| 414 | } |
| 415 | })); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 416 | } |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 417 | } |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 418 | |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 419 | // Triggers driver setup when a device is (re)detected. |
| 420 | private class InnerDeviceListener implements DeviceListener { |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 421 | @Override |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 422 | public void event(DeviceEvent event) { |
| 423 | switch (event.type()) { |
| 424 | case DEVICE_ADDED: |
| 425 | case DEVICE_AVAILABILITY_CHANGED: |
| 426 | if (deviceService.isAvailable(event.subject().id())) { |
| 427 | log.info("Device connected {}", event.subject().id()); |
sangho | f22fb40 | 2015-04-27 23:55:10 -0700 | [diff] [blame] | 428 | if (event.subject().id().equals(deviceId)) { |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 429 | processIntfFilters(true, interfaceService.getInterfaces()); |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 430 | |
sangho | dde53d1 | 2015-04-30 10:34:41 -0700 | [diff] [blame] | 431 | /* For test only - will be removed before Cardinal release |
sangho | f22fb40 | 2015-04-27 23:55:10 -0700 | [diff] [blame] | 432 | delay(1000); |
| 433 | FibEntry fibEntry = new FibEntry(Ip4Prefix.valueOf("10.1.0.0/16"), |
| 434 | Ip4Address.valueOf("192.168.10.1"), |
| 435 | MacAddress.valueOf("DE:AD:BE:EF:FE:ED")); |
| 436 | FibUpdate fibUpdate = new FibUpdate(FibUpdate.Type.UPDATE, fibEntry); |
| 437 | updateFibEntry(Collections.singletonList(fibUpdate)); |
sangho | dde53d1 | 2015-04-30 10:34:41 -0700 | [diff] [blame] | 438 | */ |
sangho | f22fb40 | 2015-04-27 23:55:10 -0700 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | if (event.subject().id().equals(ctrlDeviceId)) { |
| 442 | connectivityManager.notifySwitchAvailable(); |
| 443 | } |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 444 | } |
| 445 | break; |
Jonathan Hart | 5b14142 | 2015-03-06 12:59:09 -0800 | [diff] [blame] | 446 | |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 447 | // TODO other cases |
| 448 | case DEVICE_UPDATED: |
| 449 | break; |
| 450 | case DEVICE_REMOVED: |
| 451 | break; |
| 452 | case DEVICE_SUSPENDED: |
| 453 | break; |
| 454 | case PORT_ADDED: |
| 455 | break; |
| 456 | case PORT_UPDATED: |
| 457 | break; |
| 458 | case PORT_REMOVED: |
| 459 | break; |
| 460 | default: |
| 461 | break; |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 462 | } |
| 463 | } |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 464 | } |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 465 | } |