Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 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.pim.impl; |
| 17 | |
Jonathan Hart | 7f4bc52 | 2016-02-20 11:32:43 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 19 | import org.onlab.packet.Ethernet; |
| 20 | import org.onlab.packet.IPv4; |
Rusty Eddy | 4d5a92f | 2016-01-25 17:12:14 -0800 | [diff] [blame] | 21 | import org.onlab.packet.Ip4Address; |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 22 | import org.onlab.packet.IpAddress; |
Rusty Eddy | bcad55b | 2016-02-11 18:56:09 -0800 | [diff] [blame] | 23 | import org.onlab.packet.IpPrefix; |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 24 | import org.onlab.packet.MacAddress; |
| 25 | import org.onlab.packet.PIM; |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 26 | import org.onlab.packet.pim.PIMAddrUnicast; |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 27 | import org.onlab.packet.pim.PIMHello; |
| 28 | import org.onlab.packet.pim.PIMHelloOption; |
Rusty Eddy | bcad55b | 2016-02-11 18:56:09 -0800 | [diff] [blame] | 29 | import org.onlab.packet.pim.PIMJoinPrune; |
| 30 | import org.onlab.packet.pim.PIMJoinPruneGroup; |
Jonathan Hart | 36fd31e | 2016-01-28 15:55:31 -0800 | [diff] [blame] | 31 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 32 | import org.onosproject.net.flow.TrafficTreatment; |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 33 | import org.onosproject.net.host.InterfaceIpAddress; |
Ray Milkey | b68bbbc | 2017-12-18 10:05:49 -0800 | [diff] [blame] | 34 | import org.onosproject.net.intf.Interface; |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 35 | import org.onosproject.net.mcast.McastRoute; |
Jonathan Hart | 36fd31e | 2016-01-28 15:55:31 -0800 | [diff] [blame] | 36 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 37 | import org.onosproject.net.packet.PacketService; |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 38 | import org.slf4j.Logger; |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 39 | |
Jonathan Hart | 36fd31e | 2016-01-28 15:55:31 -0800 | [diff] [blame] | 40 | import java.nio.ByteBuffer; |
Ray Milkey | b68bbbc | 2017-12-18 10:05:49 -0800 | [diff] [blame] | 41 | import java.security.SecureRandom; |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 42 | import java.util.Collection; |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 43 | import java.util.List; |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 44 | import java.util.Map; |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 45 | import java.util.Set; |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 46 | import java.util.concurrent.ConcurrentHashMap; |
Jonathan Hart | 6be7095 | 2016-02-12 21:11:26 -0800 | [diff] [blame] | 47 | import java.util.concurrent.TimeUnit; |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 48 | import java.util.stream.Collectors; |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 49 | |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 50 | import static com.google.common.base.Preconditions.checkArgument; |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 51 | import static com.google.common.base.Preconditions.checkNotNull; |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 52 | import static org.slf4j.LoggerFactory.getLogger; |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 53 | |
| 54 | /** |
Ray Milkey | a059a70 | 2016-01-12 11:10:33 -0800 | [diff] [blame] | 55 | * PIM Interface represents an ONOS Interface with IP and MAC addresses for |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 56 | * a given ConnectPoint. |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 57 | */ |
Jonathan Hart | fbfe2a8 | 2016-03-29 11:36:33 -0700 | [diff] [blame] | 58 | public final class PimInterface { |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 59 | |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 60 | private final Logger log = getLogger(getClass()); |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 61 | |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 62 | private static final int JOIN_PERIOD = 60; |
| 63 | private static final double HOLD_TIME_MULTIPLIER = 3.5; |
| 64 | |
Jonathan Hart | 36fd31e | 2016-01-28 15:55:31 -0800 | [diff] [blame] | 65 | private final PacketService packetService; |
| 66 | |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 67 | private Interface onosInterface; |
Jonathan Hart | 36fd31e | 2016-01-28 15:55:31 -0800 | [diff] [blame] | 68 | private final TrafficTreatment outputTreatment; |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 69 | |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 70 | // Our hello opt holdtime |
| 71 | private short holdtime = PIMHelloOption.DEFAULT_HOLDTIME; |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 72 | |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 73 | // Our hello opt prune delay |
| 74 | private int pruneDelay = PIMHelloOption.DEFAULT_PRUNEDELAY; |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 75 | |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 76 | // Neighbor priority |
| 77 | private int priority = PIMHelloOption.DEFAULT_PRIORITY; |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 78 | |
Jonathan Hart | 6be7095 | 2016-02-12 21:11:26 -0800 | [diff] [blame] | 79 | private final int helloInterval; |
| 80 | |
| 81 | private long lastHello; |
| 82 | |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 83 | // Our current genid |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 84 | private final int generationId; |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 85 | |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 86 | // The IP address of the DR |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 87 | private IpAddress drIpaddress; |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 88 | |
| 89 | // A map of all our PIM neighbors keyed on our neighbors IP address |
Jonathan Hart | fbfe2a8 | 2016-03-29 11:36:33 -0700 | [diff] [blame] | 90 | private Map<IpAddress, PimNeighbor> pimNeighbors = new ConcurrentHashMap<>(); |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 91 | |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 92 | private Map<McastRoute, RouteData> routes = new ConcurrentHashMap<>(); |
| 93 | |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 94 | /** |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 95 | * Create a PIMInterface from an ONOS Interface. |
Charles Chan | 30ba400 | 2015-11-05 14:45:16 -0800 | [diff] [blame] | 96 | * |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 97 | * @param intf the ONOS Interface. |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 98 | * @param holdTime hold time |
| 99 | * @param priority priority |
| 100 | * @param propagationDelay propagation delay |
| 101 | * @param overrideInterval override interval |
| 102 | * @param packetService reference to the packet service |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 103 | */ |
Jonathan Hart | fbfe2a8 | 2016-03-29 11:36:33 -0700 | [diff] [blame] | 104 | private PimInterface(Interface intf, |
Jonathan Hart | 6be7095 | 2016-02-12 21:11:26 -0800 | [diff] [blame] | 105 | int helloInterval, |
| 106 | short holdTime, |
| 107 | int priority, |
| 108 | short propagationDelay, |
| 109 | short overrideInterval, |
| 110 | PacketService packetService) { |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 111 | |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 112 | onosInterface = intf; |
Jonathan Hart | 36fd31e | 2016-01-28 15:55:31 -0800 | [diff] [blame] | 113 | outputTreatment = createOutputTreatment(); |
Jonathan Hart | 6be7095 | 2016-02-12 21:11:26 -0800 | [diff] [blame] | 114 | this.helloInterval = helloInterval; |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 115 | this.holdtime = holdTime; |
Jonathan Hart | 36fd31e | 2016-01-28 15:55:31 -0800 | [diff] [blame] | 116 | this.packetService = packetService; |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 117 | IpAddress ourIp = getIpAddress(); |
| 118 | MacAddress mac = intf.mac(); |
| 119 | |
Jonathan Hart | 6be7095 | 2016-02-12 21:11:26 -0800 | [diff] [blame] | 120 | lastHello = 0; |
| 121 | |
Ray Milkey | b68bbbc | 2017-12-18 10:05:49 -0800 | [diff] [blame] | 122 | generationId = new SecureRandom().nextInt(); |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 123 | |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 124 | // Create a PIM Neighbor to represent ourselves for DR election. |
Jonathan Hart | fbfe2a8 | 2016-03-29 11:36:33 -0700 | [diff] [blame] | 125 | PimNeighbor us = new PimNeighbor(ourIp, mac, holdTime, 0, priority, generationId); |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 126 | |
| 127 | pimNeighbors.put(ourIp, us); |
| 128 | drIpaddress = ourIp; |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Jonathan Hart | 36fd31e | 2016-01-28 15:55:31 -0800 | [diff] [blame] | 131 | private TrafficTreatment createOutputTreatment() { |
| 132 | return DefaultTrafficTreatment.builder() |
| 133 | .setOutput(onosInterface.connectPoint().port()) |
| 134 | .build(); |
| 135 | } |
| 136 | |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 137 | /** |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 138 | * Return the ONOS Interface. |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 139 | * |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 140 | * @return ONOS Interface. |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 141 | */ |
| 142 | public Interface getInterface() { |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 143 | return onosInterface; |
| 144 | |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /** |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 148 | * Set the ONOS Interface, it will override a previous value. |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 149 | * |
Jian Li | dfba739 | 2016-01-22 16:46:58 -0800 | [diff] [blame] | 150 | * @param intf ONOS Interface |
| 151 | * @return PIM interface instance |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 152 | */ |
Jonathan Hart | fbfe2a8 | 2016-03-29 11:36:33 -0700 | [diff] [blame] | 153 | public PimInterface setInterface(Interface intf) { |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 154 | onosInterface = intf; |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 155 | return this; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Get the set of IP Addresses associated with this interface. |
| 160 | * |
| 161 | * @return a set of Ip Addresses on this interface |
| 162 | */ |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 163 | public List<InterfaceIpAddress> getIpAddresses() { |
| 164 | return onosInterface.ipAddressesList(); |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Return a single "best" IP address. |
| 169 | * |
Ray Milkey | c108a6b | 2017-08-23 15:23:50 -0700 | [diff] [blame] | 170 | * @return the chosen IP address or null if none |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 171 | */ |
| 172 | public IpAddress getIpAddress() { |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 173 | if (onosInterface.ipAddressesList().isEmpty()) { |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 174 | return null; |
| 175 | } |
| 176 | |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 177 | IpAddress ipaddr = null; |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 178 | for (InterfaceIpAddress ifipaddr : onosInterface.ipAddressesList()) { |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 179 | ipaddr = ifipaddr.ipAddress(); |
| 180 | break; |
| 181 | } |
| 182 | return ipaddr; |
| 183 | } |
| 184 | |
| 185 | /** |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 186 | * Get the holdtime. |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 187 | * |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 188 | * @return the holdtime |
| 189 | */ |
| 190 | public short getHoldtime() { |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 191 | return holdtime; |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Get the prune delay. |
| 196 | * |
| 197 | * @return The prune delay |
| 198 | */ |
| 199 | public int getPruneDelay() { |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 200 | return pruneDelay; |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Get our hello priority. |
| 205 | * |
| 206 | * @return our priority |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 207 | */ |
| 208 | public int getPriority() { |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 209 | return priority; |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /** |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 213 | * Get our generation ID. |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 214 | * |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 215 | * @return our generation ID |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 216 | */ |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 217 | public int getGenerationId() { |
| 218 | return generationId; |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | /** |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 222 | * Gets the neighbors seen on this interface. |
| 223 | * |
| 224 | * @return PIM neighbors |
| 225 | */ |
Jonathan Hart | fbfe2a8 | 2016-03-29 11:36:33 -0700 | [diff] [blame] | 226 | public Collection<PimNeighbor> getNeighbors() { |
Jonathan Hart | 7f4bc52 | 2016-02-20 11:32:43 -0800 | [diff] [blame] | 227 | return ImmutableList.copyOf(pimNeighbors.values()); |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 228 | } |
| 229 | |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 230 | public Collection<McastRoute> getRoutes() { |
| 231 | return routes.keySet(); |
| 232 | } |
| 233 | |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 234 | /** |
| 235 | * Checks whether any of our neighbors have expired, and cleans up their |
| 236 | * state if they have. |
| 237 | */ |
| 238 | public void checkNeighborTimeouts() { |
Jonathan Hart | fbfe2a8 | 2016-03-29 11:36:33 -0700 | [diff] [blame] | 239 | Set<PimNeighbor> expired = pimNeighbors.values().stream() |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 240 | // Don't time ourselves out! |
| 241 | .filter(neighbor -> !neighbor.ipAddress().equals(getIpAddress())) |
| 242 | .filter(neighbor -> neighbor.isExpired()) |
| 243 | .collect(Collectors.toSet()); |
| 244 | |
Jonathan Hart | fbfe2a8 | 2016-03-29 11:36:33 -0700 | [diff] [blame] | 245 | for (PimNeighbor neighbor : expired) { |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 246 | log.info("Timing out neighbor {}", neighbor); |
| 247 | pimNeighbors.remove(neighbor.ipAddress(), neighbor); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | /** |
Rusty Eddy | 4d5a92f | 2016-01-25 17:12:14 -0800 | [diff] [blame] | 252 | * Multicast a hello message out our interface. This hello message is sent |
| 253 | * periodically during the normal PIM Neighbor refresh time, as well as a |
| 254 | * result of a newly created interface. |
| 255 | */ |
| 256 | public void sendHello() { |
Jonathan Hart | 6be7095 | 2016-02-12 21:11:26 -0800 | [diff] [blame] | 257 | if (lastHello + TimeUnit.SECONDS.toMillis(helloInterval) > |
| 258 | System.currentTimeMillis()) { |
| 259 | return; |
| 260 | } |
| 261 | |
| 262 | lastHello = System.currentTimeMillis(); |
| 263 | |
Rusty Eddy | 4d5a92f | 2016-01-25 17:12:14 -0800 | [diff] [blame] | 264 | // Create the base PIM Packet and mark it a hello packet |
Jonathan Hart | fbfe2a8 | 2016-03-29 11:36:33 -0700 | [diff] [blame] | 265 | PimPacket pimPacket = new PimPacket(PIM.TYPE_HELLO); |
Rusty Eddy | 4d5a92f | 2016-01-25 17:12:14 -0800 | [diff] [blame] | 266 | |
| 267 | // We need to set the source MAC and IPv4 addresses |
| 268 | pimPacket.setSrcMacAddr(onosInterface.mac()); |
| 269 | pimPacket.setSrcIpAddress(Ip4Address.valueOf(getIpAddress().toOctets())); |
| 270 | |
| 271 | // Create the hello message with options |
| 272 | PIMHello hello = new PIMHello(); |
| 273 | hello.createDefaultOptions(); |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 274 | hello.addOption(PIMHelloOption.createHoldTime(holdtime)); |
| 275 | hello.addOption(PIMHelloOption.createPriority(priority)); |
| 276 | hello.addOption(PIMHelloOption.createGenID(generationId)); |
Rusty Eddy | 4d5a92f | 2016-01-25 17:12:14 -0800 | [diff] [blame] | 277 | |
| 278 | // Now set the hello option payload |
Jonathan Hart | fbfe2a8 | 2016-03-29 11:36:33 -0700 | [diff] [blame] | 279 | pimPacket.setPimPayload(hello); |
Rusty Eddy | 4d5a92f | 2016-01-25 17:12:14 -0800 | [diff] [blame] | 280 | |
Jonathan Hart | 36fd31e | 2016-01-28 15:55:31 -0800 | [diff] [blame] | 281 | packetService.emit(new DefaultOutboundPacket( |
| 282 | onosInterface.connectPoint().deviceId(), |
| 283 | outputTreatment, |
| 284 | ByteBuffer.wrap(pimPacket.getEthernet().serialize()))); |
Rusty Eddy | 4d5a92f | 2016-01-25 17:12:14 -0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | /** |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 288 | * Process an incoming PIM Hello message. There are a few things going on in |
| 289 | * this method: |
| 290 | * <ul> |
| 291 | * <li>We <em>may</em> have to create a new neighbor if one does not already exist</li> |
| 292 | * <li>We <em>may</em> need to re-elect a new DR if new information is received</li> |
| 293 | * <li>We <em>may</em> need to send an existing neighbor all joins if the genid changed</li> |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 294 | * <li>We will refresh the neighbor's timestamp</li> |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 295 | * </ul> |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 296 | * |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 297 | * @param ethPkt the Ethernet packet header |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 298 | */ |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 299 | public void processHello(Ethernet ethPkt) { |
Jonathan Hart | 7f4bc52 | 2016-02-20 11:32:43 -0800 | [diff] [blame] | 300 | if (log.isTraceEnabled()) { |
| 301 | log.trace("Received a PIM hello packet"); |
| 302 | } |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 303 | |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 304 | // We'll need to save our neighbors MAC address |
| 305 | MacAddress nbrmac = ethPkt.getSourceMAC(); |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 306 | |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 307 | // And we'll need to save neighbors IP Address. |
| 308 | IPv4 iphdr = (IPv4) ethPkt.getPayload(); |
| 309 | IpAddress srcip = IpAddress.valueOf(iphdr.getSourceAddress()); |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 310 | |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 311 | PIM pimhdr = (PIM) iphdr.getPayload(); |
| 312 | if (pimhdr.getPimMsgType() != PIM.TYPE_HELLO) { |
| 313 | log.error("process Hello has received a non hello packet type: " + pimhdr.getPimMsgType()); |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 314 | return; |
| 315 | } |
| 316 | |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 317 | // get the DR values for later calculation |
Jonathan Hart | fbfe2a8 | 2016-03-29 11:36:33 -0700 | [diff] [blame] | 318 | PimNeighbor dr = pimNeighbors.get(drIpaddress); |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 319 | checkNotNull(dr); |
| 320 | |
| 321 | IpAddress drip = drIpaddress; |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 322 | int drpri = dr.priority(); |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 323 | |
| 324 | // Assume we do not need to run a DR election |
| 325 | boolean reElectDr = false; |
| 326 | boolean genidChanged = false; |
| 327 | |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 328 | PIMHello hello = (PIMHello) pimhdr.getPayload(); |
| 329 | |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 330 | // Determine if we already have a PIMNeighbor |
Jonathan Hart | fbfe2a8 | 2016-03-29 11:36:33 -0700 | [diff] [blame] | 331 | PimNeighbor nbr = pimNeighbors.getOrDefault(srcip, null); |
| 332 | PimNeighbor newNbr = PimNeighbor.createPimNeighbor(srcip, nbrmac, hello.getOptions().values()); |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 333 | |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 334 | if (nbr == null) { |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 335 | pimNeighbors.putIfAbsent(srcip, newNbr); |
| 336 | nbr = newNbr; |
| 337 | } else if (!nbr.equals(newNbr)) { |
| 338 | if (newNbr.holdtime() == 0) { |
| 339 | // Neighbor has shut down. Remove them and clean up |
| 340 | pimNeighbors.remove(srcip, nbr); |
| 341 | return; |
| 342 | } else { |
| 343 | // Neighbor has changed one of their options. |
| 344 | pimNeighbors.put(srcip, newNbr); |
| 345 | nbr = newNbr; |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 346 | } |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 347 | } |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 348 | |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 349 | // Refresh this neighbor's timestamp |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 350 | nbr.refreshTimestamp(); |
| 351 | |
| 352 | /* |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 353 | * the election method will first determine if an election |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 354 | * needs to be run, if so it will run the election. The |
| 355 | * IP address of the DR will be returned. If the IP address |
| 356 | * of the DR is different from what we already have we know a |
| 357 | * new DR has been elected. |
| 358 | */ |
| 359 | IpAddress electedIp = election(nbr, drip, drpri); |
| 360 | if (!drip.equals(electedIp)) { |
| 361 | // we have a new DR. |
| 362 | drIpaddress = electedIp; |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 363 | } |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 364 | } |
| 365 | |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 366 | // Run an election if we need to. Return the elected IP address. |
Jonathan Hart | fbfe2a8 | 2016-03-29 11:36:33 -0700 | [diff] [blame] | 367 | private IpAddress election(PimNeighbor nbr, IpAddress drIp, int drPriority) { |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 368 | |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 369 | IpAddress nbrIp = nbr.ipAddress(); |
| 370 | if (nbr.priority() > drPriority) { |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 371 | return nbrIp; |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 372 | } |
| 373 | |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 374 | if (nbrIp.compareTo(drIp) > 0) { |
| 375 | return nbrIp; |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 376 | } |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 377 | return drIp; |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 378 | } |
| 379 | |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 380 | /** |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 381 | * Process an incoming PIM JoinPrune message. |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 382 | * |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 383 | * @param ethPkt the Ethernet packet header. |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 384 | */ |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 385 | public void processJoinPrune(Ethernet ethPkt) { |
Rusty Eddy | bcad55b | 2016-02-11 18:56:09 -0800 | [diff] [blame] | 386 | |
| 387 | IPv4 ip = (IPv4) ethPkt.getPayload(); |
| 388 | checkNotNull(ip); |
| 389 | |
| 390 | PIM pim = (PIM) ip.getPayload(); |
| 391 | checkNotNull(pim); |
| 392 | |
| 393 | PIMJoinPrune jpHdr = (PIMJoinPrune) pim.getPayload(); |
| 394 | checkNotNull(jpHdr); |
| 395 | |
| 396 | /* |
| 397 | * The Join/Prune messages are grouped by Group address. We'll walk each group address |
| 398 | * where we will possibly have to walk a list of source address for the joins and prunes. |
| 399 | */ |
| 400 | Collection<PIMJoinPruneGroup> jpgs = jpHdr.getJoinPrunes(); |
| 401 | for (PIMJoinPruneGroup jpg : jpgs) { |
| 402 | IpPrefix gpfx = jpg.getGroup(); |
| 403 | |
| 404 | // Walk the joins first. |
| 405 | for (IpPrefix spfx : jpg.getJoins().values()) { |
| 406 | |
| 407 | // We may need |
| 408 | |
| 409 | |
| 410 | } |
| 411 | |
| 412 | for (IpPrefix spfx : jpg.getPrunes().values()) { |
| 413 | |
| 414 | // TODO: this is where we many need to remove multi-cast state and possibly intents. |
| 415 | |
| 416 | } |
| 417 | } |
| 418 | |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 419 | } |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 420 | |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 421 | public void addRoute(McastRoute route, IpAddress nextHop, MacAddress nextHopMac) { |
| 422 | RouteData data = new RouteData(nextHop, nextHopMac); |
| 423 | routes.put(route, data); |
| 424 | |
| 425 | sendJoinPrune(route, data, true); |
| 426 | } |
| 427 | |
| 428 | public void removeRoute(McastRoute route) { |
| 429 | RouteData data = routes.remove(route); |
| 430 | |
| 431 | if (data != null) { |
| 432 | sendJoinPrune(route, data, false); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | public void sendJoins() { |
| 437 | routes.entrySet().forEach(entry -> { |
| 438 | if (entry.getValue().timestamp + TimeUnit.SECONDS.toMillis(JOIN_PERIOD) > |
| 439 | System.currentTimeMillis()) { |
| 440 | return; |
| 441 | } |
| 442 | |
| 443 | sendJoinPrune(entry.getKey(), entry.getValue(), true); |
| 444 | }); |
| 445 | } |
| 446 | |
| 447 | private void sendJoinPrune(McastRoute route, RouteData data, boolean join) { |
| 448 | PIMJoinPrune jp = new PIMJoinPrune(); |
| 449 | |
| 450 | jp.addJoinPrune(route.source().toIpPrefix(), route.group().toIpPrefix(), join); |
| 451 | jp.setHoldTime(join ? (short) Math.floor(JOIN_PERIOD * HOLD_TIME_MULTIPLIER) : 0); |
| 452 | jp.setUpstreamAddr(new PIMAddrUnicast(data.ipAddress.toString())); |
| 453 | |
| 454 | PIM pim = new PIM(); |
| 455 | pim.setPIMType(PIM.TYPE_JOIN_PRUNE_REQUEST); |
| 456 | pim.setPayload(jp); |
| 457 | |
| 458 | IPv4 ipv4 = new IPv4(); |
| 459 | ipv4.setDestinationAddress(PIM.PIM_ADDRESS.getIp4Address().toInt()); |
| 460 | ipv4.setSourceAddress(getIpAddress().getIp4Address().toInt()); |
| 461 | ipv4.setProtocol(IPv4.PROTOCOL_PIM); |
| 462 | ipv4.setTtl((byte) 1); |
| 463 | ipv4.setDiffServ((byte) 0xc0); |
| 464 | ipv4.setPayload(pim); |
| 465 | |
| 466 | Ethernet eth = new Ethernet(); |
| 467 | eth.setSourceMACAddress(onosInterface.mac()); |
| 468 | eth.setDestinationMACAddress(MacAddress.valueOf("01:00:5E:00:00:0d")); |
| 469 | eth.setEtherType(Ethernet.TYPE_IPV4); |
| 470 | eth.setPayload(ipv4); |
| 471 | |
| 472 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 473 | .setOutput(onosInterface.connectPoint().port()) |
| 474 | .build(); |
| 475 | |
| 476 | packetService.emit(new DefaultOutboundPacket(onosInterface.connectPoint().deviceId(), |
| 477 | treatment, ByteBuffer.wrap(eth.serialize()))); |
| 478 | |
| 479 | data.timestamp = System.currentTimeMillis(); |
| 480 | } |
| 481 | |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 482 | /** |
| 483 | * Returns a builder for a PIM interface. |
| 484 | * |
| 485 | * @return PIM interface builder |
| 486 | */ |
| 487 | public static Builder builder() { |
| 488 | return new Builder(); |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * Builder for a PIM interface. |
| 493 | */ |
| 494 | public static class Builder { |
| 495 | private Interface intf; |
| 496 | private PacketService packetService; |
Jonathan Hart | fbfe2a8 | 2016-03-29 11:36:33 -0700 | [diff] [blame] | 497 | private int helloInterval = PimInterfaceManager.DEFAULT_HELLO_INTERVAL; |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 498 | private short holdtime = PIMHelloOption.DEFAULT_HOLDTIME; |
| 499 | private int priority = PIMHelloOption.DEFAULT_PRIORITY; |
| 500 | private short propagationDelay = PIMHelloOption.DEFAULT_PRUNEDELAY; |
| 501 | private short overrideInterval = PIMHelloOption.DEFAULT_OVERRIDEINTERVAL; |
| 502 | |
| 503 | /** |
| 504 | * Uses the specified ONOS interface. |
| 505 | * |
| 506 | * @param intf ONOS interface |
| 507 | * @return this PIM interface builder |
| 508 | */ |
| 509 | public Builder withInterface(Interface intf) { |
| 510 | this.intf = checkNotNull(intf); |
| 511 | return this; |
| 512 | } |
| 513 | |
| 514 | /** |
| 515 | * Sets the reference to the packet service. |
| 516 | * |
| 517 | * @param packetService packet service |
| 518 | * @return this PIM interface builder |
| 519 | */ |
| 520 | public Builder withPacketService(PacketService packetService) { |
| 521 | this.packetService = checkNotNull(packetService); |
| 522 | return this; |
| 523 | } |
| 524 | |
| 525 | /** |
Jonathan Hart | 6be7095 | 2016-02-12 21:11:26 -0800 | [diff] [blame] | 526 | * Users the specified hello interval. |
| 527 | * |
| 528 | * @param helloInterval hello interval in seconds |
| 529 | * @return this PIM interface builder |
| 530 | */ |
| 531 | public Builder withHelloInterval(int helloInterval) { |
| 532 | this.helloInterval = helloInterval; |
| 533 | return this; |
| 534 | } |
| 535 | |
| 536 | /** |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 537 | * Uses the specified hold time. |
| 538 | * |
| 539 | * @param holdTime hold time in seconds |
| 540 | * @return this PIM interface builder |
| 541 | */ |
| 542 | public Builder withHoldTime(short holdTime) { |
| 543 | this.holdtime = holdTime; |
| 544 | return this; |
| 545 | } |
| 546 | |
| 547 | /** |
| 548 | * Uses the specified DR priority. |
| 549 | * |
| 550 | * @param priority DR priority |
| 551 | * @return this PIM interface builder |
| 552 | */ |
| 553 | public Builder withPriority(int priority) { |
| 554 | this.priority = priority; |
| 555 | return this; |
| 556 | } |
| 557 | |
| 558 | /** |
| 559 | * Uses the specified propagation delay. |
| 560 | * |
| 561 | * @param propagationDelay propagation delay in ms |
| 562 | * @return this PIM interface builder |
| 563 | */ |
| 564 | public Builder withPropagationDelay(short propagationDelay) { |
| 565 | this.propagationDelay = propagationDelay; |
| 566 | return this; |
| 567 | } |
| 568 | |
| 569 | /** |
| 570 | * Uses the specified override interval. |
| 571 | * |
| 572 | * @param overrideInterval override interval in ms |
| 573 | * @return this PIM interface builder |
| 574 | */ |
| 575 | public Builder withOverrideInterval(short overrideInterval) { |
| 576 | this.overrideInterval = overrideInterval; |
| 577 | return this; |
| 578 | } |
| 579 | |
| 580 | /** |
| 581 | * Builds the PIM interface. |
| 582 | * |
| 583 | * @return PIM interface |
| 584 | */ |
Jonathan Hart | fbfe2a8 | 2016-03-29 11:36:33 -0700 | [diff] [blame] | 585 | public PimInterface build() { |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 586 | checkArgument(intf != null, "Must provide an interface"); |
| 587 | checkArgument(packetService != null, "Must provide a packet service"); |
| 588 | |
Jonathan Hart | fbfe2a8 | 2016-03-29 11:36:33 -0700 | [diff] [blame] | 589 | return new PimInterface(intf, helloInterval, holdtime, priority, |
Jonathan Hart | 6be7095 | 2016-02-12 21:11:26 -0800 | [diff] [blame] | 590 | propagationDelay, overrideInterval, packetService); |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | } |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 594 | |
| 595 | private static class RouteData { |
Ray Milkey | e4afdb5 | 2017-04-05 09:42:04 -0700 | [diff] [blame] | 596 | final IpAddress ipAddress; |
| 597 | final MacAddress macAddress; |
| 598 | long timestamp; |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 599 | |
| 600 | public RouteData(IpAddress ip, MacAddress mac) { |
| 601 | this.ipAddress = ip; |
| 602 | this.macAddress = mac; |
| 603 | timestamp = System.currentTimeMillis(); |
| 604 | } |
| 605 | } |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 606 | } |