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; |
Srikanth Vavilapalli | 89ad377 | 2015-03-25 18:00:38 -0700 | [diff] [blame] | 29 | import org.onlab.packet.Ip4Address; |
| 30 | import org.onlab.packet.Ip6Address; |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 31 | import org.onlab.packet.IpAddress; |
| 32 | import org.onlab.packet.IpPrefix; |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 33 | import org.onlab.packet.MacAddress; |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 34 | import org.onlab.packet.VlanId; |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 35 | import org.onlab.util.KryoNamespace; |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 36 | import org.onosproject.config.NetworkConfigService; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 37 | import org.onosproject.core.ApplicationId; |
| 38 | import org.onosproject.core.CoreService; |
| 39 | import org.onosproject.net.DeviceId; |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 40 | import org.onosproject.net.PortNumber; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 41 | import org.onosproject.net.flow.DefaultFlowRule; |
| 42 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 43 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 44 | import org.onosproject.net.flow.FlowRule; |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 45 | import org.onosproject.net.flow.FlowRuleOperations; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 46 | import org.onosproject.net.flow.FlowRuleService; |
| 47 | import org.onosproject.net.flow.TrafficSelector; |
| 48 | import org.onosproject.net.flow.TrafficTreatment; |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame^] | 49 | import org.onosproject.net.flow.criteria.Criteria; |
| 50 | import org.onosproject.net.flowobjective.DefaultFilteringObjective; |
| 51 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 52 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 53 | import org.onosproject.net.group.DefaultGroupBucket; |
| 54 | import org.onosproject.net.group.DefaultGroupDescription; |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 55 | import org.onosproject.net.group.DefaultGroupKey; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 56 | import org.onosproject.net.group.Group; |
| 57 | import org.onosproject.net.group.GroupBucket; |
| 58 | import org.onosproject.net.group.GroupBuckets; |
| 59 | import org.onosproject.net.group.GroupDescription; |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 60 | import org.onosproject.net.group.GroupEvent; |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 61 | import org.onosproject.net.group.GroupListener; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 62 | import org.onosproject.net.group.GroupService; |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 63 | import org.onosproject.net.host.InterfaceIpAddress; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 64 | import org.onosproject.net.packet.PacketService; |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 65 | import org.onosproject.routing.FibEntry; |
Jonathan Hart | 2da1e60 | 2015-02-18 19:09:24 -0800 | [diff] [blame] | 66 | import org.onosproject.routing.FibListener; |
| 67 | import org.onosproject.routing.FibUpdate; |
| 68 | import org.onosproject.routing.RoutingService; |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 69 | import org.onosproject.routing.config.BgpSpeaker; |
Jonathan Hart | 2da1e60 | 2015-02-18 19:09:24 -0800 | [diff] [blame] | 70 | import org.onosproject.routing.config.Interface; |
| 71 | import org.onosproject.routing.config.RoutingConfigurationService; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 72 | import org.slf4j.Logger; |
| 73 | import org.slf4j.LoggerFactory; |
| 74 | |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame^] | 75 | import java.util.Collection; |
| 76 | import java.util.Collections; |
| 77 | import java.util.HashMap; |
| 78 | import java.util.HashSet; |
| 79 | import java.util.Map; |
| 80 | import java.util.Set; |
| 81 | import java.util.stream.Collectors; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 82 | |
| 83 | /** |
| 84 | * BgpRouter component. |
| 85 | */ |
| 86 | @Component(immediate = true) |
| 87 | public class BgpRouter { |
| 88 | |
| 89 | private static final Logger log = LoggerFactory.getLogger(BgpRouter.class); |
| 90 | |
| 91 | private static final String BGP_ROUTER_APP = "org.onosproject.bgprouter"; |
| 92 | |
Jonathan Hart | 12ef205 | 2015-03-10 13:58:13 -0700 | [diff] [blame] | 93 | private static final int PRIORITY_OFFSET = 100; |
| 94 | private static final int PRIORITY_MULTIPLIER = 5; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 95 | |
| 96 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 97 | protected CoreService coreService; |
| 98 | |
| 99 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 100 | protected FlowRuleService flowService; |
| 101 | |
| 102 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 103 | protected GroupService groupService; |
| 104 | |
| 105 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 106 | protected RoutingService routingService; |
| 107 | |
| 108 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 109 | protected RoutingConfigurationService configService; |
| 110 | |
| 111 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 112 | protected PacketService packetService; |
| 113 | |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 114 | // |
| 115 | // NOTE: Unused reference - needed to guarantee that the |
| 116 | // NetworkConfigReader component is activated and the network configuration |
| 117 | // is read. |
| 118 | // |
| 119 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 120 | protected NetworkConfigService networkConfigService; |
| 121 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 122 | private ApplicationId appId; |
| 123 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 124 | // Reference count for how many times a next hop is used by a route |
| 125 | private final Multiset<IpAddress> nextHopsCount = ConcurrentHashMultiset.create(); |
| 126 | |
| 127 | // Mapping from prefix to its current next hop |
| 128 | private final Map<IpPrefix, IpAddress> prefixToNextHop = Maps.newHashMap(); |
| 129 | |
| 130 | // Mapping from next hop IP to next hop object containing group info |
| 131 | private final Map<IpAddress, NextHop> nextHops = Maps.newHashMap(); |
| 132 | |
| 133 | // Stores FIB updates that are waiting for groups to be set up |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 134 | private final Multimap<NextHopGroupKey, FibEntry> pendingUpdates = HashMultimap.create(); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 135 | |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 136 | // Device id of data-plane switch - should be learned from config |
| 137 | private DeviceId deviceId; |
| 138 | |
| 139 | // Device id of control-plane switch (OVS) connected to BGP Speaker - should be |
| 140 | // learned from config |
| 141 | private DeviceId ctrlDeviceId; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 142 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 143 | private final GroupListener groupListener = new InternalGroupListener(); |
| 144 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 145 | private TunnellingConnectivityManager connectivityManager; |
| 146 | |
sangho | 5eaf033 | 2015-03-09 15:08:12 -0700 | [diff] [blame] | 147 | private IcmpHandler icmpHandler; |
| 148 | |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 149 | private InternalTableHandler provisionStaticTables = new InternalTableHandler(); |
| 150 | |
Srikanth Vavilapalli | 89ad377 | 2015-03-25 18:00:38 -0700 | [diff] [blame] | 151 | private KryoNamespace appKryo = new KryoNamespace.Builder() |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 152 | .register(IpAddress.Version.class) |
| 153 | .register(IpAddress.class) |
Srikanth Vavilapalli | 89ad377 | 2015-03-25 18:00:38 -0700 | [diff] [blame] | 154 | .register(Ip4Address.class) |
| 155 | .register(Ip6Address.class) |
| 156 | .register(byte[].class) |
| 157 | .register(NextHopGroupKey.class) |
| 158 | .build(); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 159 | |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame^] | 160 | |
| 161 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 162 | protected FlowObjectiveService flowObjectiveService; |
| 163 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 164 | @Activate |
| 165 | protected void activate() { |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 166 | appId = coreService.registerApplication(BGP_ROUTER_APP); |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 167 | getDeviceConfiguration(configService.getBgpSpeakers()); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 168 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 169 | groupService.addListener(groupListener); |
| 170 | |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 171 | provisionStaticTables.provision(true, configService.getInterfaces()); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 172 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 173 | connectivityManager = new TunnellingConnectivityManager(appId, |
| 174 | configService, |
Jonathan Hart | 936a729 | 2015-03-06 18:02:57 -0800 | [diff] [blame] | 175 | packetService, |
| 176 | flowService); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 177 | |
sangho | 5eaf033 | 2015-03-09 15:08:12 -0700 | [diff] [blame] | 178 | icmpHandler = new IcmpHandler(configService, packetService); |
| 179 | |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 180 | routingService.addFibListener(new InternalFibListener()); |
| 181 | routingService.start(); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 182 | |
| 183 | connectivityManager.start(); |
| 184 | |
sangho | 5eaf033 | 2015-03-09 15:08:12 -0700 | [diff] [blame] | 185 | icmpHandler.start(); |
| 186 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 187 | log.info("BgpRouter started"); |
| 188 | } |
| 189 | |
| 190 | @Deactivate |
| 191 | protected void deactivate() { |
| 192 | routingService.stop(); |
| 193 | connectivityManager.stop(); |
sangho | 5eaf033 | 2015-03-09 15:08:12 -0700 | [diff] [blame] | 194 | icmpHandler.stop(); |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 195 | provisionStaticTables.provision(false, configService.getInterfaces()); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 196 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 197 | groupService.removeListener(groupListener); |
| 198 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 199 | log.info("BgpRouter stopped"); |
| 200 | } |
| 201 | |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 202 | private void getDeviceConfiguration(Map<String, BgpSpeaker> bgps) { |
| 203 | if (bgps == null || bgps.values().isEmpty()) { |
| 204 | log.error("BGP speakers configuration is missing"); |
| 205 | return; |
| 206 | } |
| 207 | for (BgpSpeaker s : bgps.values()) { |
| 208 | ctrlDeviceId = s.connectPoint().deviceId(); |
| 209 | if (s.interfaceAddresses() == null || s.interfaceAddresses().isEmpty()) { |
| 210 | log.error("BGP Router must have interfaces configured"); |
| 211 | return; |
| 212 | } |
| 213 | deviceId = s.interfaceAddresses().get(0).connectPoint().deviceId(); |
| 214 | break; |
| 215 | } |
sangho | 5eaf033 | 2015-03-09 15:08:12 -0700 | [diff] [blame] | 216 | |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 217 | log.info("Router dpid: {}", deviceId); |
| 218 | log.info("Control Plane OVS dpid: {}", ctrlDeviceId); |
| 219 | } |
| 220 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 221 | private void updateFibEntry(Collection<FibUpdate> updates) { |
Jonathan Hart | 5b14142 | 2015-03-06 12:59:09 -0800 | [diff] [blame] | 222 | Map<FibEntry, Group> toInstall = new HashMap<>(updates.size()); |
| 223 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 224 | for (FibUpdate update : updates) { |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 225 | FibEntry entry = update.entry(); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 226 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 227 | addNextHop(entry); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 228 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 229 | Group group; |
| 230 | synchronized (pendingUpdates) { |
| 231 | NextHop nextHop = nextHops.get(entry.nextHopIp()); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 232 | group = groupService.getGroup(deviceId, |
| 233 | new DefaultGroupKey( |
Srikanth Vavilapalli | 89ad377 | 2015-03-25 18:00:38 -0700 | [diff] [blame] | 234 | appKryo.serialize(nextHop.group()))); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 235 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 236 | if (group == null) { |
| 237 | log.debug("Adding pending flow {}", update.entry()); |
| 238 | pendingUpdates.put(nextHop.group(), update.entry()); |
| 239 | continue; |
| 240 | } |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 241 | } |
| 242 | |
Jonathan Hart | 5b14142 | 2015-03-06 12:59:09 -0800 | [diff] [blame] | 243 | toInstall.put(update.entry(), group); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 244 | } |
Jonathan Hart | 5b14142 | 2015-03-06 12:59:09 -0800 | [diff] [blame] | 245 | |
| 246 | installFlows(toInstall); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 247 | } |
| 248 | |
Jonathan Hart | 5b14142 | 2015-03-06 12:59:09 -0800 | [diff] [blame] | 249 | private void installFlows(Map<FibEntry, Group> entriesToInstall) { |
| 250 | FlowRuleOperations.Builder builder = FlowRuleOperations.builder(); |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 251 | |
Jonathan Hart | 5b14142 | 2015-03-06 12:59:09 -0800 | [diff] [blame] | 252 | for (Map.Entry<FibEntry, Group> entry : entriesToInstall.entrySet()) { |
| 253 | FibEntry fibEntry = entry.getKey(); |
| 254 | Group group = entry.getValue(); |
| 255 | |
| 256 | FlowRule flowRule = generateRibFlowRule(fibEntry.prefix(), group); |
| 257 | |
| 258 | builder.add(flowRule); |
| 259 | } |
| 260 | |
| 261 | flowService.apply(builder.build()); |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | private synchronized void deleteFibEntry(Collection<FibUpdate> withdraws) { |
Jonathan Hart | 5b14142 | 2015-03-06 12:59:09 -0800 | [diff] [blame] | 265 | FlowRuleOperations.Builder builder = FlowRuleOperations.builder(); |
| 266 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 267 | for (FibUpdate update : withdraws) { |
| 268 | FibEntry entry = update.entry(); |
| 269 | |
Jonathan Hart | 37d659c | 2015-03-08 19:20:38 -0700 | [diff] [blame] | 270 | Group group = deleteNextHop(entry.prefix()); |
| 271 | if (group == null) { |
| 272 | log.warn("Group not found when deleting {}", entry); |
| 273 | return; |
| 274 | } |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 275 | |
Jonathan Hart | 37d659c | 2015-03-08 19:20:38 -0700 | [diff] [blame] | 276 | FlowRule flowRule = generateRibFlowRule(entry.prefix(), group); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 277 | |
Jonathan Hart | 5b14142 | 2015-03-06 12:59:09 -0800 | [diff] [blame] | 278 | builder.remove(flowRule); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 279 | } |
Jonathan Hart | 5b14142 | 2015-03-06 12:59:09 -0800 | [diff] [blame] | 280 | |
| 281 | flowService.apply(builder.build()); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 282 | } |
| 283 | |
Jonathan Hart | 37d659c | 2015-03-08 19:20:38 -0700 | [diff] [blame] | 284 | private FlowRule generateRibFlowRule(IpPrefix prefix, Group group) { |
| 285 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 286 | .matchEthType(Ethernet.TYPE_IPV4) |
| 287 | .matchIPDst(prefix) |
| 288 | .build(); |
| 289 | |
| 290 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 291 | .group(group.id()) |
| 292 | .build(); |
| 293 | |
Jonathan Hart | 12ef205 | 2015-03-10 13:58:13 -0700 | [diff] [blame] | 294 | |
| 295 | int priority = prefix.prefixLength() * PRIORITY_MULTIPLIER + PRIORITY_OFFSET; |
| 296 | |
Jonathan Hart | 37d659c | 2015-03-08 19:20:38 -0700 | [diff] [blame] | 297 | return new DefaultFlowRule(deviceId, selector, treatment, |
Jonathan Hart | 12ef205 | 2015-03-10 13:58:13 -0700 | [diff] [blame] | 298 | priority, appId, 0, true, |
Jonathan Hart | 37d659c | 2015-03-08 19:20:38 -0700 | [diff] [blame] | 299 | FlowRule.Type.IP); |
| 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 | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 307 | Interface egressIntf = configService.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 | |
| 326 | GroupBucket bucket = DefaultGroupBucket.createIndirectGroupBucket(treatment); |
| 327 | |
| 328 | GroupDescription groupDescription |
| 329 | = new DefaultGroupDescription(deviceId, |
| 330 | GroupDescription.Type.INDIRECT, |
| 331 | new GroupBuckets(Collections |
Srikanth Vavilapalli | 89ad377 | 2015-03-25 18:00:38 -0700 | [diff] [blame] | 332 | .singletonList(bucket)), |
| 333 | new DefaultGroupKey(appKryo.serialize(groupKey)), |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 334 | appId); |
| 335 | |
| 336 | groupService.addGroup(groupDescription); |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 337 | |
| 338 | nextHops.put(nextHop.ip(), nextHop); |
| 339 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 340 | } |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 341 | |
| 342 | nextHopsCount.add(entry.nextHopIp()); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 343 | } |
| 344 | |
Jonathan Hart | 37d659c | 2015-03-08 19:20:38 -0700 | [diff] [blame] | 345 | private synchronized Group deleteNextHop(IpPrefix prefix) { |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 346 | IpAddress nextHopIp = prefixToNextHop.remove(prefix); |
| 347 | NextHop nextHop = nextHops.get(nextHopIp); |
| 348 | if (nextHop == null) { |
| 349 | log.warn("No next hop found when removing prefix {}", prefix); |
Jonathan Hart | 37d659c | 2015-03-08 19:20:38 -0700 | [diff] [blame] | 350 | return null; |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 351 | } |
| 352 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 353 | Group group = groupService.getGroup(deviceId, |
| 354 | new DefaultGroupKey(appKryo. |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 355 | serialize(nextHop.group()))); |
Jonathan Hart | 37d659c | 2015-03-08 19:20:38 -0700 | [diff] [blame] | 356 | |
Jonathan Hart | f9f2cbb | 2015-03-12 17:44:03 -0700 | [diff] [blame] | 357 | // FIXME disabling group deletes for now until we verify the logic is OK |
| 358 | /*if (nextHopsCount.remove(nextHopIp, 1) <= 1) { |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 359 | // There was one or less next hops, so there are now none |
| 360 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 361 | log.debug("removing group for next hop {}", nextHop); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 362 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 363 | nextHops.remove(nextHopIp); |
| 364 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 365 | groupService.removeGroup(deviceId, |
| 366 | new DefaultGroupKey(appKryo.build().serialize(nextHop.group())), |
| 367 | appId); |
Jonathan Hart | f9f2cbb | 2015-03-12 17:44:03 -0700 | [diff] [blame] | 368 | }*/ |
Jonathan Hart | 37d659c | 2015-03-08 19:20:38 -0700 | [diff] [blame] | 369 | |
| 370 | return group; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | private class InternalFibListener implements FibListener { |
| 374 | |
| 375 | @Override |
| 376 | public void update(Collection<FibUpdate> updates, |
| 377 | Collection<FibUpdate> withdraws) { |
| 378 | BgpRouter.this.deleteFibEntry(withdraws); |
| 379 | BgpRouter.this.updateFibEntry(updates); |
| 380 | } |
| 381 | } |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 382 | |
| 383 | private class InternalTableHandler { |
| 384 | |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 385 | private Set<InterfaceIpAddress> intfIps = new HashSet<InterfaceIpAddress>(); |
| 386 | private Set<MacAddress> intfMacs = new HashSet<MacAddress>(); |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 387 | private Map<PortNumber, VlanId> portVlanPair = Maps.newHashMap(); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 388 | |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 389 | public void provision(boolean install, Set<Interface> intfs) { |
Jonathan Hart | 5b14142 | 2015-03-06 12:59:09 -0800 | [diff] [blame] | 390 | getInterfaceConfig(intfs); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 391 | } |
| 392 | |
Jonathan Hart | 5b14142 | 2015-03-06 12:59:09 -0800 | [diff] [blame] | 393 | private void getInterfaceConfig(Set<Interface> intfs) { |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 394 | log.info("Processing {} router interfaces", intfs.size()); |
| 395 | for (Interface intf : intfs) { |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame^] | 396 | FilteringObjective.Builder fob = DefaultFilteringObjective.builder(); |
| 397 | flowObjectiveService.filter(deviceId, Collections.singletonList( |
| 398 | fob.addCondition(Criteria.matchEthDst(intf.mac())) |
| 399 | .fromApp(appId).permit().add())); |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 400 | intfIps.addAll(intf.ipAddresses()); |
| 401 | intfMacs.add(intf.mac()); |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 402 | portVlanPair.put(intf.connectPoint().port(), intf.vlan()); |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 406 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 407 | } |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 408 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 409 | private class InternalGroupListener implements GroupListener { |
| 410 | |
| 411 | @Override |
| 412 | public void event(GroupEvent event) { |
| 413 | Group group = event.subject(); |
| 414 | |
| 415 | if (event.type() == GroupEvent.Type.GROUP_ADDED || |
| 416 | event.type() == GroupEvent.Type.GROUP_UPDATED) { |
| 417 | synchronized (pendingUpdates) { |
Jonathan Hart | 5b14142 | 2015-03-06 12:59:09 -0800 | [diff] [blame] | 418 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 419 | NextHopGroupKey nhGroupKey = |
Srikanth Vavilapalli | 89ad377 | 2015-03-25 18:00:38 -0700 | [diff] [blame] | 420 | appKryo.deserialize(group.appCookie().key()); |
Jonathan Hart | 5b14142 | 2015-03-06 12:59:09 -0800 | [diff] [blame] | 421 | Map<FibEntry, Group> entriesToInstall = |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 422 | pendingUpdates.removeAll(nhGroupKey) |
Jonathan Hart | 5b14142 | 2015-03-06 12:59:09 -0800 | [diff] [blame] | 423 | .stream() |
| 424 | .collect(Collectors |
| 425 | .toMap(e -> e, e -> group)); |
| 426 | |
| 427 | installFlows(entriesToInstall); |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | } |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 431 | } |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 432 | } |