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