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