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 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 18 | import java.util.Collection; |
| 19 | import java.util.Collections; |
| 20 | import java.util.HashMap; |
| 21 | import java.util.HashSet; |
| 22 | import java.util.Map; |
| 23 | import java.util.Set; |
| 24 | import java.util.stream.Collectors; |
| 25 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Activate; |
| 27 | import org.apache.felix.scr.annotations.Component; |
| 28 | import org.apache.felix.scr.annotations.Deactivate; |
| 29 | import org.apache.felix.scr.annotations.Reference; |
| 30 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 31 | import org.onlab.packet.Ethernet; |
Srikanth Vavilapalli | 89ad377 | 2015-03-25 18:00:38 -0700 | [diff] [blame] | 32 | import org.onlab.packet.Ip4Address; |
| 33 | import org.onlab.packet.Ip6Address; |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 34 | import org.onlab.packet.IpAddress; |
| 35 | import org.onlab.packet.IpPrefix; |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 36 | import org.onlab.packet.MacAddress; |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 37 | import org.onlab.packet.VlanId; |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 38 | import org.onlab.util.KryoNamespace; |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 39 | import org.onosproject.config.NetworkConfigService; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 40 | import org.onosproject.core.ApplicationId; |
| 41 | import org.onosproject.core.CoreService; |
| 42 | import org.onosproject.net.DeviceId; |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 43 | import org.onosproject.net.PortNumber; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 44 | import org.onosproject.net.flow.DefaultFlowRule; |
| 45 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 46 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 47 | import org.onosproject.net.flow.FlowRule; |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 48 | import org.onosproject.net.flow.FlowRule.Type; |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 49 | import org.onosproject.net.flow.FlowRuleOperations; |
| 50 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 51 | import org.onosproject.net.flow.FlowRuleService; |
| 52 | import org.onosproject.net.flow.TrafficSelector; |
| 53 | import org.onosproject.net.flow.TrafficTreatment; |
| 54 | import org.onosproject.net.group.DefaultGroupBucket; |
| 55 | import org.onosproject.net.group.DefaultGroupDescription; |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 56 | import org.onosproject.net.group.DefaultGroupKey; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 57 | import org.onosproject.net.group.Group; |
| 58 | import org.onosproject.net.group.GroupBucket; |
| 59 | import org.onosproject.net.group.GroupBuckets; |
| 60 | import org.onosproject.net.group.GroupDescription; |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 61 | import org.onosproject.net.group.GroupEvent; |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 62 | import org.onosproject.net.group.GroupListener; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 63 | import org.onosproject.net.group.GroupService; |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 64 | import org.onosproject.net.host.InterfaceIpAddress; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 65 | import org.onosproject.net.packet.PacketService; |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 66 | import org.onosproject.routing.FibEntry; |
Jonathan Hart | 2da1e60 | 2015-02-18 19:09:24 -0800 | [diff] [blame] | 67 | import org.onosproject.routing.FibListener; |
| 68 | import org.onosproject.routing.FibUpdate; |
| 69 | import org.onosproject.routing.RoutingService; |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 70 | import org.onosproject.routing.config.BgpSpeaker; |
Jonathan Hart | 2da1e60 | 2015-02-18 19:09:24 -0800 | [diff] [blame] | 71 | import org.onosproject.routing.config.Interface; |
| 72 | import org.onosproject.routing.config.RoutingConfigurationService; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 73 | import org.slf4j.Logger; |
| 74 | import org.slf4j.LoggerFactory; |
| 75 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 76 | import com.google.common.collect.ConcurrentHashMultiset; |
| 77 | import com.google.common.collect.HashMultimap; |
| 78 | import com.google.common.collect.Maps; |
| 79 | import com.google.common.collect.Multimap; |
| 80 | import com.google.common.collect.Multiset; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * BgpRouter component. |
| 84 | */ |
| 85 | @Component(immediate = true) |
| 86 | public class BgpRouter { |
| 87 | |
| 88 | private static final Logger log = LoggerFactory.getLogger(BgpRouter.class); |
| 89 | |
| 90 | private static final String BGP_ROUTER_APP = "org.onosproject.bgprouter"; |
| 91 | |
Jonathan Hart | 12ef205 | 2015-03-10 13:58:13 -0700 | [diff] [blame] | 92 | private static final int PRIORITY_OFFSET = 100; |
| 93 | private static final int PRIORITY_MULTIPLIER = 5; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 94 | |
| 95 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 96 | protected CoreService coreService; |
| 97 | |
| 98 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 99 | protected FlowRuleService flowService; |
| 100 | |
| 101 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 102 | protected GroupService groupService; |
| 103 | |
| 104 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 105 | protected RoutingService routingService; |
| 106 | |
| 107 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 108 | protected RoutingConfigurationService configService; |
| 109 | |
| 110 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 111 | protected PacketService packetService; |
| 112 | |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 113 | // |
| 114 | // NOTE: Unused reference - needed to guarantee that the |
| 115 | // NetworkConfigReader component is activated and the network configuration |
| 116 | // is read. |
| 117 | // |
| 118 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 119 | protected NetworkConfigService networkConfigService; |
| 120 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 121 | private ApplicationId appId; |
| 122 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 123 | // Reference count for how many times a next hop is used by a route |
| 124 | private final Multiset<IpAddress> nextHopsCount = ConcurrentHashMultiset.create(); |
| 125 | |
| 126 | // Mapping from prefix to its current next hop |
| 127 | private final Map<IpPrefix, IpAddress> prefixToNextHop = Maps.newHashMap(); |
| 128 | |
| 129 | // Mapping from next hop IP to next hop object containing group info |
| 130 | private final Map<IpAddress, NextHop> nextHops = Maps.newHashMap(); |
| 131 | |
| 132 | // Stores FIB updates that are waiting for groups to be set up |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 133 | private final Multimap<NextHopGroupKey, FibEntry> pendingUpdates = HashMultimap.create(); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 134 | |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 135 | // Device id of data-plane switch - should be learned from config |
| 136 | private DeviceId deviceId; |
| 137 | |
| 138 | // Device id of control-plane switch (OVS) connected to BGP Speaker - should be |
| 139 | // learned from config |
| 140 | private DeviceId ctrlDeviceId; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 141 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 142 | private final GroupListener groupListener = new InternalGroupListener(); |
| 143 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 144 | private TunnellingConnectivityManager connectivityManager; |
| 145 | |
sangho | 5eaf033 | 2015-03-09 15:08:12 -0700 | [diff] [blame] | 146 | private IcmpHandler icmpHandler; |
| 147 | |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 148 | private InternalTableHandler provisionStaticTables = new InternalTableHandler(); |
| 149 | |
Srikanth Vavilapalli | 89ad377 | 2015-03-25 18:00:38 -0700 | [diff] [blame] | 150 | private KryoNamespace appKryo = new KryoNamespace.Builder() |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 151 | .register(IpAddress.Version.class) |
| 152 | .register(IpAddress.class) |
Srikanth Vavilapalli | 89ad377 | 2015-03-25 18:00:38 -0700 | [diff] [blame] | 153 | .register(Ip4Address.class) |
| 154 | .register(Ip6Address.class) |
| 155 | .register(byte[].class) |
| 156 | .register(NextHopGroupKey.class) |
| 157 | .build(); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 158 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 159 | @Activate |
| 160 | protected void activate() { |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 161 | appId = coreService.registerApplication(BGP_ROUTER_APP); |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 162 | getDeviceConfiguration(configService.getBgpSpeakers()); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 163 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 164 | groupService.addListener(groupListener); |
| 165 | |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 166 | provisionStaticTables.provision(true, configService.getInterfaces()); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 167 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 168 | connectivityManager = new TunnellingConnectivityManager(appId, |
| 169 | configService, |
Jonathan Hart | 936a729 | 2015-03-06 18:02:57 -0800 | [diff] [blame] | 170 | packetService, |
| 171 | flowService); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 172 | |
sangho | 5eaf033 | 2015-03-09 15:08:12 -0700 | [diff] [blame] | 173 | icmpHandler = new IcmpHandler(configService, packetService); |
| 174 | |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 175 | routingService.addFibListener(new InternalFibListener()); |
| 176 | routingService.start(); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 177 | |
| 178 | connectivityManager.start(); |
| 179 | |
sangho | 5eaf033 | 2015-03-09 15:08:12 -0700 | [diff] [blame] | 180 | icmpHandler.start(); |
| 181 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 182 | log.info("BgpRouter started"); |
| 183 | } |
| 184 | |
| 185 | @Deactivate |
| 186 | protected void deactivate() { |
| 187 | routingService.stop(); |
| 188 | connectivityManager.stop(); |
sangho | 5eaf033 | 2015-03-09 15:08:12 -0700 | [diff] [blame] | 189 | icmpHandler.stop(); |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 190 | provisionStaticTables.provision(false, configService.getInterfaces()); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 191 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 192 | groupService.removeListener(groupListener); |
| 193 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 194 | log.info("BgpRouter stopped"); |
| 195 | } |
| 196 | |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 197 | private void getDeviceConfiguration(Map<String, BgpSpeaker> bgps) { |
| 198 | if (bgps == null || bgps.values().isEmpty()) { |
| 199 | log.error("BGP speakers configuration is missing"); |
| 200 | return; |
| 201 | } |
| 202 | for (BgpSpeaker s : bgps.values()) { |
| 203 | ctrlDeviceId = s.connectPoint().deviceId(); |
| 204 | if (s.interfaceAddresses() == null || s.interfaceAddresses().isEmpty()) { |
| 205 | log.error("BGP Router must have interfaces configured"); |
| 206 | return; |
| 207 | } |
| 208 | deviceId = s.interfaceAddresses().get(0).connectPoint().deviceId(); |
| 209 | break; |
| 210 | } |
sangho | 5eaf033 | 2015-03-09 15:08:12 -0700 | [diff] [blame] | 211 | |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 212 | log.info("Router dpid: {}", deviceId); |
| 213 | log.info("Control Plane OVS dpid: {}", ctrlDeviceId); |
| 214 | } |
| 215 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 216 | private void updateFibEntry(Collection<FibUpdate> updates) { |
Jonathan Hart | 5b14142 | 2015-03-06 12:59:09 -0800 | [diff] [blame] | 217 | Map<FibEntry, Group> toInstall = new HashMap<>(updates.size()); |
| 218 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 219 | for (FibUpdate update : updates) { |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 220 | FibEntry entry = update.entry(); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 221 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 222 | addNextHop(entry); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 223 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 224 | Group group; |
| 225 | synchronized (pendingUpdates) { |
| 226 | NextHop nextHop = nextHops.get(entry.nextHopIp()); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 227 | group = groupService.getGroup(deviceId, |
| 228 | new DefaultGroupKey( |
Srikanth Vavilapalli | 89ad377 | 2015-03-25 18:00:38 -0700 | [diff] [blame] | 229 | appKryo.serialize(nextHop.group()))); |
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 | if (group == null) { |
| 232 | log.debug("Adding pending flow {}", update.entry()); |
| 233 | pendingUpdates.put(nextHop.group(), update.entry()); |
| 234 | continue; |
| 235 | } |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 236 | } |
| 237 | |
Jonathan Hart | 5b14142 | 2015-03-06 12:59:09 -0800 | [diff] [blame] | 238 | toInstall.put(update.entry(), group); |
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 | |
Jonathan Hart | 5b14142 | 2015-03-06 12:59:09 -0800 | [diff] [blame] | 244 | private void installFlows(Map<FibEntry, Group> entriesToInstall) { |
| 245 | FlowRuleOperations.Builder builder = FlowRuleOperations.builder(); |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 246 | |
Jonathan Hart | 5b14142 | 2015-03-06 12:59:09 -0800 | [diff] [blame] | 247 | for (Map.Entry<FibEntry, Group> entry : entriesToInstall.entrySet()) { |
| 248 | FibEntry fibEntry = entry.getKey(); |
| 249 | Group group = entry.getValue(); |
| 250 | |
| 251 | FlowRule flowRule = generateRibFlowRule(fibEntry.prefix(), group); |
| 252 | |
| 253 | builder.add(flowRule); |
| 254 | } |
| 255 | |
| 256 | flowService.apply(builder.build()); |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | private synchronized void deleteFibEntry(Collection<FibUpdate> withdraws) { |
Jonathan Hart | 5b14142 | 2015-03-06 12:59:09 -0800 | [diff] [blame] | 260 | FlowRuleOperations.Builder builder = FlowRuleOperations.builder(); |
| 261 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 262 | for (FibUpdate update : withdraws) { |
| 263 | FibEntry entry = update.entry(); |
| 264 | |
Jonathan Hart | 37d659c | 2015-03-08 19:20:38 -0700 | [diff] [blame] | 265 | Group group = deleteNextHop(entry.prefix()); |
| 266 | if (group == null) { |
| 267 | log.warn("Group not found when deleting {}", entry); |
| 268 | return; |
| 269 | } |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 270 | |
Jonathan Hart | 37d659c | 2015-03-08 19:20:38 -0700 | [diff] [blame] | 271 | FlowRule flowRule = generateRibFlowRule(entry.prefix(), group); |
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 | builder.remove(flowRule); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 274 | } |
Jonathan Hart | 5b14142 | 2015-03-06 12:59:09 -0800 | [diff] [blame] | 275 | |
| 276 | flowService.apply(builder.build()); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 277 | } |
| 278 | |
Jonathan Hart | 37d659c | 2015-03-08 19:20:38 -0700 | [diff] [blame] | 279 | private FlowRule generateRibFlowRule(IpPrefix prefix, Group group) { |
| 280 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 281 | .matchEthType(Ethernet.TYPE_IPV4) |
| 282 | .matchIPDst(prefix) |
| 283 | .build(); |
| 284 | |
| 285 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 286 | .group(group.id()) |
| 287 | .build(); |
| 288 | |
Jonathan Hart | 12ef205 | 2015-03-10 13:58:13 -0700 | [diff] [blame] | 289 | |
| 290 | int priority = prefix.prefixLength() * PRIORITY_MULTIPLIER + PRIORITY_OFFSET; |
| 291 | |
Jonathan Hart | 37d659c | 2015-03-08 19:20:38 -0700 | [diff] [blame] | 292 | return new DefaultFlowRule(deviceId, selector, treatment, |
Jonathan Hart | 12ef205 | 2015-03-10 13:58:13 -0700 | [diff] [blame] | 293 | priority, appId, 0, true, |
Jonathan Hart | 37d659c | 2015-03-08 19:20:38 -0700 | [diff] [blame] | 294 | FlowRule.Type.IP); |
| 295 | } |
| 296 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 297 | private synchronized void addNextHop(FibEntry entry) { |
| 298 | prefixToNextHop.put(entry.prefix(), entry.nextHopIp()); |
| 299 | if (nextHopsCount.count(entry.nextHopIp()) == 0) { |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 300 | // There was no next hop in the multiset |
| 301 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 302 | Interface egressIntf = configService.getMatchingInterface(entry.nextHopIp()); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 303 | if (egressIntf == null) { |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 304 | log.warn("no egress interface found for {}", entry); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 305 | return; |
| 306 | } |
| 307 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 308 | NextHopGroupKey groupKey = new NextHopGroupKey(entry.nextHopIp()); |
| 309 | |
| 310 | NextHop nextHop = new NextHop(entry.nextHopIp(), entry.nextHopMac(), groupKey); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 311 | |
| 312 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 313 | .setEthSrc(egressIntf.mac()) |
| 314 | .setEthDst(nextHop.mac()) |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 315 | .pushVlan() |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 316 | .setVlanId(egressIntf.vlan()) |
alshabib | da1644e | 2015-03-13 14:01:35 -0700 | [diff] [blame] | 317 | .setVlanPcp((byte) 0) |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 318 | .setOutput(egressIntf.connectPoint().port()) |
| 319 | .build(); |
| 320 | |
| 321 | GroupBucket bucket = DefaultGroupBucket.createIndirectGroupBucket(treatment); |
| 322 | |
| 323 | GroupDescription groupDescription |
| 324 | = new DefaultGroupDescription(deviceId, |
| 325 | GroupDescription.Type.INDIRECT, |
| 326 | new GroupBuckets(Collections |
Srikanth Vavilapalli | 89ad377 | 2015-03-25 18:00:38 -0700 | [diff] [blame] | 327 | .singletonList(bucket)), |
| 328 | new DefaultGroupKey(appKryo.serialize(groupKey)), |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 329 | appId); |
| 330 | |
| 331 | groupService.addGroup(groupDescription); |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 332 | |
| 333 | nextHops.put(nextHop.ip(), nextHop); |
| 334 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 335 | } |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 336 | |
| 337 | nextHopsCount.add(entry.nextHopIp()); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 338 | } |
| 339 | |
Jonathan Hart | 37d659c | 2015-03-08 19:20:38 -0700 | [diff] [blame] | 340 | private synchronized Group deleteNextHop(IpPrefix prefix) { |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 341 | IpAddress nextHopIp = prefixToNextHop.remove(prefix); |
| 342 | NextHop nextHop = nextHops.get(nextHopIp); |
| 343 | if (nextHop == null) { |
| 344 | log.warn("No next hop found when removing prefix {}", prefix); |
Jonathan Hart | 37d659c | 2015-03-08 19:20:38 -0700 | [diff] [blame] | 345 | return null; |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 346 | } |
| 347 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 348 | Group group = groupService.getGroup(deviceId, |
| 349 | new DefaultGroupKey(appKryo. |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 350 | serialize(nextHop.group()))); |
Jonathan Hart | 37d659c | 2015-03-08 19:20:38 -0700 | [diff] [blame] | 351 | |
Jonathan Hart | f9f2cbb | 2015-03-12 17:44:03 -0700 | [diff] [blame] | 352 | // FIXME disabling group deletes for now until we verify the logic is OK |
| 353 | /*if (nextHopsCount.remove(nextHopIp, 1) <= 1) { |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 354 | // There was one or less next hops, so there are now none |
| 355 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 356 | log.debug("removing group for next hop {}", nextHop); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 357 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 358 | nextHops.remove(nextHopIp); |
| 359 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 360 | groupService.removeGroup(deviceId, |
| 361 | new DefaultGroupKey(appKryo.build().serialize(nextHop.group())), |
| 362 | appId); |
Jonathan Hart | f9f2cbb | 2015-03-12 17:44:03 -0700 | [diff] [blame] | 363 | }*/ |
Jonathan Hart | 37d659c | 2015-03-08 19:20:38 -0700 | [diff] [blame] | 364 | |
| 365 | return group; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | private class InternalFibListener implements FibListener { |
| 369 | |
| 370 | @Override |
| 371 | public void update(Collection<FibUpdate> updates, |
| 372 | Collection<FibUpdate> withdraws) { |
| 373 | BgpRouter.this.deleteFibEntry(withdraws); |
| 374 | BgpRouter.this.updateFibEntry(updates); |
| 375 | } |
| 376 | } |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 377 | |
| 378 | private class InternalTableHandler { |
| 379 | |
| 380 | private static final int CONTROLLER_PRIORITY = 255; |
| 381 | private static final int DROP_PRIORITY = 0; |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 382 | private static final int HIGHEST_PRIORITY = 0xffff; |
| 383 | private Set<InterfaceIpAddress> intfIps = new HashSet<InterfaceIpAddress>(); |
| 384 | private Set<MacAddress> intfMacs = new HashSet<MacAddress>(); |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 385 | private Map<PortNumber, VlanId> portVlanPair = Maps.newHashMap(); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 386 | |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 387 | public void provision(boolean install, Set<Interface> intfs) { |
Jonathan Hart | 5b14142 | 2015-03-06 12:59:09 -0800 | [diff] [blame] | 388 | getInterfaceConfig(intfs); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 389 | processTableZero(install); |
| 390 | processTableOne(install); |
| 391 | processTableTwo(install); |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 392 | processTableFour(install); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 393 | processTableFive(install); |
| 394 | processTableSix(install); |
| 395 | processTableNine(install); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 396 | } |
| 397 | |
Jonathan Hart | 5b14142 | 2015-03-06 12:59:09 -0800 | [diff] [blame] | 398 | private void getInterfaceConfig(Set<Interface> intfs) { |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 399 | log.info("Processing {} router interfaces", intfs.size()); |
| 400 | for (Interface intf : intfs) { |
| 401 | intfIps.addAll(intf.ipAddresses()); |
| 402 | intfMacs.add(intf.mac()); |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 403 | portVlanPair.put(intf.connectPoint().port(), intf.vlan()); |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 407 | private void processTableZero(boolean install) { |
| 408 | TrafficSelector.Builder selector; |
| 409 | TrafficTreatment.Builder treatment; |
| 410 | |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 411 | // Bcast rule |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 412 | selector = DefaultTrafficSelector.builder(); |
| 413 | treatment = DefaultTrafficTreatment.builder(); |
| 414 | |
| 415 | selector.matchEthDst(MacAddress.BROADCAST); |
| 416 | treatment.transition(FlowRule.Type.VLAN_MPLS); |
| 417 | |
| 418 | FlowRule rule = new DefaultFlowRule(deviceId, selector.build(), |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 419 | treatment.build(), |
| 420 | CONTROLLER_PRIORITY, appId, 0, |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 421 | true, FlowRule.Type.FIRST); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 422 | |
| 423 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 424 | |
| 425 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 426 | |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 427 | // Interface MACs |
| 428 | for (MacAddress mac : intfMacs) { |
| 429 | log.debug("adding rule for MAC: {}", mac); |
| 430 | selector = DefaultTrafficSelector.builder(); |
| 431 | treatment = DefaultTrafficTreatment.builder(); |
| 432 | |
| 433 | selector.matchEthDst(mac); |
| 434 | treatment.transition(FlowRule.Type.VLAN_MPLS); |
| 435 | |
| 436 | rule = new DefaultFlowRule(deviceId, selector.build(), |
| 437 | treatment.build(), |
| 438 | CONTROLLER_PRIORITY, appId, 0, |
| 439 | true, FlowRule.Type.FIRST); |
| 440 | |
| 441 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 442 | } |
| 443 | |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 444 | //Drop rule |
| 445 | selector = DefaultTrafficSelector.builder(); |
| 446 | treatment = DefaultTrafficTreatment.builder(); |
| 447 | |
| 448 | treatment.drop(); |
| 449 | |
| 450 | rule = new DefaultFlowRule(deviceId, selector.build(), |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 451 | treatment.build(), DROP_PRIORITY, appId, |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 452 | 0, true, FlowRule.Type.FIRST); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 453 | |
| 454 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 455 | |
| 456 | flowService.apply(ops.build(new FlowRuleOperationsContext() { |
| 457 | @Override |
| 458 | public void onSuccess(FlowRuleOperations ops) { |
| 459 | log.info("Provisioned default table for bgp router"); |
| 460 | } |
| 461 | |
| 462 | @Override |
| 463 | public void onError(FlowRuleOperations ops) { |
| 464 | log.info("Failed to provision default table for bgp router"); |
| 465 | } |
| 466 | })); |
| 467 | |
| 468 | } |
| 469 | |
| 470 | private void processTableOne(boolean install) { |
| 471 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 472 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment |
| 473 | .builder(); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 474 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 475 | FlowRule rule; |
| 476 | |
sangho | c0b3c34 | 2015-03-12 17:12:10 -0700 | [diff] [blame] | 477 | selector.matchVlanId(VlanId.ANY); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 478 | treatment.transition(FlowRule.Type.VLAN); |
| 479 | |
| 480 | rule = new DefaultFlowRule(deviceId, selector.build(), |
| 481 | treatment.build(), CONTROLLER_PRIORITY, |
| 482 | appId, 0, true, FlowRule.Type.VLAN_MPLS); |
| 483 | |
| 484 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 485 | |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 486 | flowService.apply(ops.build(new FlowRuleOperationsContext() { |
| 487 | @Override |
| 488 | public void onSuccess(FlowRuleOperations ops) { |
| 489 | log.info("Provisioned vlan/mpls table for bgp router"); |
| 490 | } |
| 491 | |
| 492 | @Override |
| 493 | public void onError(FlowRuleOperations ops) { |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 494 | log.info( |
| 495 | "Failed to provision vlan/mpls table for bgp router"); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 496 | } |
| 497 | })); |
| 498 | |
| 499 | } |
| 500 | |
| 501 | private void processTableTwo(boolean install) { |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 502 | TrafficSelector.Builder selector; |
| 503 | TrafficTreatment.Builder treatment; |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 504 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 505 | FlowRule rule; |
| 506 | |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 507 | //Interface Vlans |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 508 | for (Map.Entry<PortNumber, VlanId> portVlan : portVlanPair.entrySet()) { |
| 509 | log.debug("adding rule for VLAN: {}", portVlan); |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 510 | selector = DefaultTrafficSelector.builder(); |
| 511 | treatment = DefaultTrafficTreatment.builder(); |
| 512 | |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 513 | selector.matchVlanId(portVlan.getValue()); |
| 514 | selector.matchInPort(portVlan.getKey()); |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 515 | treatment.transition(Type.ETHER); |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 516 | treatment.deferred().popVlan(); |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 517 | |
| 518 | rule = new DefaultFlowRule(deviceId, selector.build(), |
| 519 | treatment.build(), CONTROLLER_PRIORITY, appId, |
| 520 | 0, true, FlowRule.Type.VLAN); |
| 521 | |
| 522 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 523 | } |
| 524 | |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 525 | //Drop rule |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 526 | selector = DefaultTrafficSelector.builder(); |
| 527 | treatment = DefaultTrafficTreatment.builder(); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 528 | |
| 529 | treatment.drop(); |
| 530 | |
| 531 | rule = new DefaultFlowRule(deviceId, selector.build(), |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 532 | treatment.build(), DROP_PRIORITY, appId, |
| 533 | 0, true, FlowRule.Type.VLAN); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 534 | |
| 535 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 536 | |
| 537 | flowService.apply(ops.build(new FlowRuleOperationsContext() { |
| 538 | @Override |
| 539 | public void onSuccess(FlowRuleOperations ops) { |
| 540 | log.info("Provisioned vlan table for bgp router"); |
| 541 | } |
| 542 | |
| 543 | @Override |
| 544 | public void onError(FlowRuleOperations ops) { |
| 545 | log.info("Failed to provision vlan table for bgp router"); |
| 546 | } |
| 547 | })); |
| 548 | } |
| 549 | |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 550 | private void processTableFour(boolean install) { |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 551 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 552 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment |
| 553 | .builder(); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 554 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 555 | FlowRule rule; |
| 556 | |
| 557 | selector.matchEthType(Ethernet.TYPE_ARP); |
| 558 | treatment.punt(); |
| 559 | |
| 560 | rule = new DefaultFlowRule(deviceId, selector.build(), |
| 561 | treatment.build(), CONTROLLER_PRIORITY, |
| 562 | appId, 0, true, FlowRule.Type.ETHER); |
| 563 | |
| 564 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 565 | |
| 566 | selector = DefaultTrafficSelector.builder(); |
| 567 | treatment = DefaultTrafficTreatment.builder(); |
| 568 | |
| 569 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 570 | treatment.transition(FlowRule.Type.COS); |
| 571 | |
| 572 | rule = new DefaultFlowRule(deviceId, selector.build(), |
| 573 | treatment.build(), CONTROLLER_PRIORITY, |
| 574 | appId, 0, true, FlowRule.Type.ETHER); |
| 575 | |
| 576 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 577 | |
| 578 | //Drop rule |
| 579 | selector = DefaultTrafficSelector.builder(); |
| 580 | treatment = DefaultTrafficTreatment.builder(); |
| 581 | |
| 582 | treatment.drop(); |
| 583 | |
| 584 | rule = new DefaultFlowRule(deviceId, selector.build(), |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 585 | treatment.build(), DROP_PRIORITY, appId, |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 586 | 0, true, FlowRule.Type.ETHER); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 587 | |
| 588 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 589 | |
| 590 | flowService.apply(ops.build(new FlowRuleOperationsContext() { |
| 591 | @Override |
| 592 | public void onSuccess(FlowRuleOperations ops) { |
| 593 | log.info("Provisioned ether table for bgp router"); |
| 594 | } |
| 595 | |
| 596 | @Override |
| 597 | public void onError(FlowRuleOperations ops) { |
| 598 | log.info("Failed to provision ether table for bgp router"); |
| 599 | } |
| 600 | })); |
| 601 | |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | private void processTableFive(boolean install) { |
| 605 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 606 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment |
| 607 | .builder(); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 608 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 609 | FlowRule rule; |
| 610 | |
| 611 | treatment.transition(FlowRule.Type.IP); |
| 612 | |
| 613 | rule = new DefaultFlowRule(deviceId, selector.build(), |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 614 | treatment.build(), DROP_PRIORITY, appId, |
| 615 | 0, true, FlowRule.Type.COS); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 616 | |
| 617 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 618 | |
| 619 | flowService.apply(ops.build(new FlowRuleOperationsContext() { |
| 620 | @Override |
| 621 | public void onSuccess(FlowRuleOperations ops) { |
| 622 | log.info("Provisioned cos table for bgp router"); |
| 623 | } |
| 624 | |
| 625 | @Override |
| 626 | public void onError(FlowRuleOperations ops) { |
| 627 | log.info("Failed to provision cos table for bgp router"); |
| 628 | } |
| 629 | })); |
| 630 | |
| 631 | } |
| 632 | |
| 633 | private void processTableSix(boolean install) { |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 634 | TrafficSelector.Builder selector; |
| 635 | TrafficTreatment.Builder treatment; |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 636 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 637 | FlowRule rule; |
| 638 | |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 639 | |
| 640 | //Interface IPs |
| 641 | for (InterfaceIpAddress ipAddr : intfIps) { |
| 642 | log.debug("adding rule for IPs: {}", ipAddr.ipAddress()); |
| 643 | selector = DefaultTrafficSelector.builder(); |
| 644 | treatment = DefaultTrafficTreatment.builder(); |
| 645 | |
| 646 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 647 | selector.matchIPDst(IpPrefix.valueOf(ipAddr.ipAddress(), 32)); |
| 648 | treatment.transition(Type.ACL); |
| 649 | |
| 650 | rule = new DefaultFlowRule(deviceId, selector.build(), |
| 651 | treatment.build(), HIGHEST_PRIORITY, appId, |
| 652 | 0, true, FlowRule.Type.IP); |
| 653 | |
| 654 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 655 | } |
| 656 | |
| 657 | |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 658 | //Drop rule |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 659 | selector = DefaultTrafficSelector.builder(); |
| 660 | treatment = DefaultTrafficTreatment.builder(); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 661 | |
| 662 | treatment.drop(); |
| 663 | |
| 664 | rule = new DefaultFlowRule(deviceId, selector.build(), |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 665 | treatment.build(), DROP_PRIORITY, appId, |
| 666 | 0, true, FlowRule.Type.IP); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 667 | |
| 668 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 669 | |
| 670 | flowService.apply(ops.build(new FlowRuleOperationsContext() { |
| 671 | @Override |
| 672 | public void onSuccess(FlowRuleOperations ops) { |
| 673 | log.info("Provisioned FIB table for bgp router"); |
| 674 | } |
| 675 | |
| 676 | @Override |
| 677 | public void onError(FlowRuleOperations ops) { |
| 678 | log.info("Failed to provision FIB table for bgp router"); |
| 679 | } |
| 680 | })); |
| 681 | } |
| 682 | |
| 683 | private void processTableNine(boolean install) { |
| 684 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 685 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment |
| 686 | .builder(); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 687 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 688 | FlowRule rule; |
| 689 | |
| 690 | treatment.punt(); |
| 691 | |
| 692 | rule = new DefaultFlowRule(deviceId, selector.build(), |
| 693 | treatment.build(), CONTROLLER_PRIORITY, |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 694 | appId, 0, true, FlowRule.Type.DEFAULT); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 695 | |
| 696 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 697 | |
| 698 | flowService.apply(ops.build(new FlowRuleOperationsContext() { |
| 699 | @Override |
| 700 | public void onSuccess(FlowRuleOperations ops) { |
| 701 | log.info("Provisioned Local table for bgp router"); |
| 702 | } |
| 703 | |
| 704 | @Override |
| 705 | public void onError(FlowRuleOperations ops) { |
| 706 | log.info("Failed to provision Local table for bgp router"); |
| 707 | } |
| 708 | })); |
| 709 | } |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 710 | } |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 711 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 712 | private class InternalGroupListener implements GroupListener { |
| 713 | |
| 714 | @Override |
| 715 | public void event(GroupEvent event) { |
| 716 | Group group = event.subject(); |
| 717 | |
| 718 | if (event.type() == GroupEvent.Type.GROUP_ADDED || |
| 719 | event.type() == GroupEvent.Type.GROUP_UPDATED) { |
| 720 | synchronized (pendingUpdates) { |
Jonathan Hart | 5b14142 | 2015-03-06 12:59:09 -0800 | [diff] [blame] | 721 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 722 | NextHopGroupKey nhGroupKey = |
Srikanth Vavilapalli | 89ad377 | 2015-03-25 18:00:38 -0700 | [diff] [blame] | 723 | appKryo.deserialize(group.appCookie().key()); |
Jonathan Hart | 5b14142 | 2015-03-06 12:59:09 -0800 | [diff] [blame] | 724 | Map<FibEntry, Group> entriesToInstall = |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 725 | pendingUpdates.removeAll(nhGroupKey) |
Jonathan Hart | 5b14142 | 2015-03-06 12:59:09 -0800 | [diff] [blame] | 726 | .stream() |
| 727 | .collect(Collectors |
| 728 | .toMap(e -> e, e -> group)); |
| 729 | |
| 730 | installFlows(entriesToInstall); |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 731 | } |
| 732 | } |
| 733 | } |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 734 | } |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 735 | } |