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