Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [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.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; |
| 22 | import org.onlab.packet.MacAddress; |
| 23 | import org.onlab.packet.PIM; |
| 24 | import org.onlab.packet.pim.PIMHello; |
| 25 | import org.onlab.packet.pim.PIMHelloOption; |
| 26 | import org.onosproject.incubator.net.intf.Interface; |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 27 | import org.onosproject.net.host.InterfaceIpAddress; |
| 28 | import org.slf4j.Logger; |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 29 | |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 30 | import java.util.HashMap; |
| 31 | import java.util.Map; |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 32 | import java.util.Set; |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 33 | |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 34 | import static com.google.common.base.Preconditions.checkNotNull; |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 35 | import static org.slf4j.LoggerFactory.getLogger; |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 36 | |
| 37 | /** |
Ray Milkey | a059a70 | 2016-01-12 11:10:33 -0800 | [diff] [blame] | 38 | * PIM Interface represents an ONOS Interface with IP and MAC addresses for |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 39 | * a given ConnectPoint. |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 40 | */ |
| 41 | public class PIMInterface { |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 42 | |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 43 | private final Logger log = getLogger(getClass()); |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 44 | |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 45 | private Interface onosInterface; |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 46 | |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 47 | // Our hello opt holdtime |
| 48 | private short holdtime = PIMHelloOption.DEFAULT_HOLDTIME; |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 49 | |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 50 | // Our hello opt prune delay |
| 51 | private int pruneDelay = PIMHelloOption.DEFAULT_PRUNEDELAY; |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 52 | |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 53 | // Neighbor priority |
| 54 | private int priority = PIMHelloOption.DEFAULT_PRIORITY; |
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 | // Our current genid |
| 57 | private int genid = PIMHelloOption.DEFAULT_GENID; // Needs to be assigned. |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 58 | |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 59 | // The IP address of the DR |
| 60 | IpAddress drIpaddress; |
| 61 | |
| 62 | // A map of all our PIM neighbors keyed on our neighbors IP address |
| 63 | private Map<IpAddress, PIMNeighbor> pimNeighbors = new HashMap<>(); |
| 64 | |
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 | * Create a PIMInterface from an ONOS Interface. |
Charles Chan | 30ba400 | 2015-11-05 14:45:16 -0800 | [diff] [blame] | 67 | * |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 68 | * @param intf the ONOS Interface. |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 69 | */ |
| 70 | public PIMInterface(Interface intf) { |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 71 | onosInterface = intf; |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 72 | IpAddress ourIp = getIpAddress(); |
| 73 | MacAddress mac = intf.mac(); |
| 74 | |
| 75 | // Create a PIM Neighbor to represent ourselves for DR election. |
| 76 | PIMNeighbor us = new PIMNeighbor(ourIp, mac); |
| 77 | |
| 78 | // Priority and IP address are all we need to DR election. |
| 79 | us.setPriority(priority); |
| 80 | |
| 81 | pimNeighbors.put(ourIp, us); |
| 82 | drIpaddress = ourIp; |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /** |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 86 | * Return the ONOS Interface. |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 87 | * |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 88 | * @return ONOS Interface. |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 89 | */ |
| 90 | public Interface getInterface() { |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 91 | return onosInterface; |
| 92 | |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /** |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 96 | * Set the ONOS Interface, it will override a previous value. |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 97 | * |
Jian Li | dfba739 | 2016-01-22 16:46:58 -0800 | [diff] [blame] | 98 | * @param intf ONOS Interface |
| 99 | * @return PIM interface instance |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 100 | */ |
| 101 | public PIMInterface setInterface(Interface intf) { |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 102 | onosInterface = intf; |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 103 | return this; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Get the set of IP Addresses associated with this interface. |
| 108 | * |
| 109 | * @return a set of Ip Addresses on this interface |
| 110 | */ |
| 111 | public Set<InterfaceIpAddress> getIpAddresses() { |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 112 | return onosInterface.ipAddresses(); |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Return a single "best" IP address. |
| 117 | * |
| 118 | * @return the choosen IP address or null if none |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 119 | */ |
| 120 | public IpAddress getIpAddress() { |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 121 | if (onosInterface.ipAddresses().isEmpty()) { |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 122 | return null; |
| 123 | } |
| 124 | |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 125 | IpAddress ipaddr = null; |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 126 | for (InterfaceIpAddress ifipaddr : onosInterface.ipAddresses()) { |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 127 | ipaddr = ifipaddr.ipAddress(); |
| 128 | break; |
| 129 | } |
| 130 | return ipaddr; |
| 131 | } |
| 132 | |
| 133 | /** |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 134 | * Get the holdtime. |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 135 | * |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 136 | * @return the holdtime |
| 137 | */ |
| 138 | public short getHoldtime() { |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 139 | return holdtime; |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Get the prune delay. |
| 144 | * |
| 145 | * @return The prune delay |
| 146 | */ |
| 147 | public int getPruneDelay() { |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 148 | return pruneDelay; |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Get our hello priority. |
| 153 | * |
| 154 | * @return our priority |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 155 | */ |
| 156 | public int getPriority() { |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 157 | return priority; |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /** |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 161 | * Get our generation ID. |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 162 | * |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 163 | * @return our generation ID |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 164 | */ |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 165 | public int getGenid() { |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 166 | return genid; |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /** |
Rusty Eddy | 4d5a92f | 2016-01-25 17:12:14 -0800 | [diff] [blame^] | 170 | * Multicast a hello message out our interface. This hello message is sent |
| 171 | * periodically during the normal PIM Neighbor refresh time, as well as a |
| 172 | * result of a newly created interface. |
| 173 | */ |
| 174 | public void sendHello() { |
| 175 | |
| 176 | // Create the base PIM Packet and mark it a hello packet |
| 177 | PIMPacket pimPacket = new PIMPacket(PIM.TYPE_HELLO); |
| 178 | |
| 179 | // We need to set the source MAC and IPv4 addresses |
| 180 | pimPacket.setSrcMacAddr(onosInterface.mac()); |
| 181 | pimPacket.setSrcIpAddress(Ip4Address.valueOf(getIpAddress().toOctets())); |
| 182 | |
| 183 | // Create the hello message with options |
| 184 | PIMHello hello = new PIMHello(); |
| 185 | hello.createDefaultOptions(); |
| 186 | |
| 187 | // Now set the hello option payload |
| 188 | pimPacket.setPIMPayload(hello); |
| 189 | |
| 190 | // TODO: How to send the packet.?. |
| 191 | } |
| 192 | |
| 193 | /** |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 194 | * Process an incoming PIM Hello message. There are a few things going on in |
| 195 | * this method: |
| 196 | * <ul> |
| 197 | * <li>We <em>may</em> have to create a new neighbor if one does not already exist</li> |
| 198 | * <li>We <em>may</em> need to re-elect a new DR if new information is received</li> |
| 199 | * <li>We <em>may</em> need to send an existing neighbor all joins if the genid changed</li> |
| 200 | * <li>We will refresh the neighbors timestamp</li> |
| 201 | * </ul> |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 202 | * |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 203 | * @param ethPkt the Ethernet packet header |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 204 | */ |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 205 | public void processHello(Ethernet ethPkt) { |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 206 | |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 207 | // We'll need to save our neighbors MAC address |
| 208 | MacAddress nbrmac = ethPkt.getSourceMAC(); |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 209 | |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 210 | // And we'll need to save neighbors IP Address. |
| 211 | IPv4 iphdr = (IPv4) ethPkt.getPayload(); |
| 212 | IpAddress srcip = IpAddress.valueOf(iphdr.getSourceAddress()); |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 213 | |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 214 | PIM pimhdr = (PIM) iphdr.getPayload(); |
| 215 | if (pimhdr.getPimMsgType() != PIM.TYPE_HELLO) { |
| 216 | 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] | 217 | return; |
| 218 | } |
| 219 | |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 220 | // get the DR values for later calculation |
| 221 | PIMNeighbor dr = pimNeighbors.get(drIpaddress); |
| 222 | checkNotNull(dr); |
| 223 | |
| 224 | IpAddress drip = drIpaddress; |
| 225 | int drpri = dr.getPriority(); |
| 226 | |
| 227 | // Assume we do not need to run a DR election |
| 228 | boolean reElectDr = false; |
| 229 | boolean genidChanged = false; |
| 230 | |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 231 | PIMHello hello = (PIMHello) pimhdr.getPayload(); |
| 232 | |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 233 | // Determine if we already have a PIMNeighbor |
| 234 | PIMNeighbor nbr = pimNeighbors.getOrDefault(srcip, null); |
| 235 | if (nbr == null) { |
| 236 | nbr = new PIMNeighbor(srcip, hello.getOptions()); |
| 237 | checkNotNull(nbr); |
| 238 | } else { |
| 239 | Integer previousGenid = nbr.getGenid(); |
| 240 | nbr.addOptions(hello.getOptions()); |
| 241 | if (previousGenid != nbr.getGenid()) { |
| 242 | genidChanged = true; |
| 243 | } |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 244 | } |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 245 | |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 246 | // Refresh this neighbors timestamp |
| 247 | nbr.refreshTimestamp(); |
| 248 | |
| 249 | /* |
| 250 | * the election method will frist determine if an election |
| 251 | * needs to be run, if so it will run the election. The |
| 252 | * IP address of the DR will be returned. If the IP address |
| 253 | * of the DR is different from what we already have we know a |
| 254 | * new DR has been elected. |
| 255 | */ |
| 256 | IpAddress electedIp = election(nbr, drip, drpri); |
| 257 | if (!drip.equals(electedIp)) { |
| 258 | // we have a new DR. |
| 259 | drIpaddress = electedIp; |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 260 | } |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 263 | // Run an election if we need to. Return the elected IP address. |
| 264 | private IpAddress election(PIMNeighbor nbr, IpAddress drip, int drpri) { |
| 265 | |
| 266 | IpAddress nbrip = nbr.getIpaddr(); |
| 267 | if (nbr.getPriority() > drpri) { |
| 268 | return nbrip; |
| 269 | } |
| 270 | |
| 271 | if (nbrip.compareTo(drip) > 0) { |
| 272 | return nbrip; |
| 273 | } |
| 274 | return drip; |
| 275 | } |
| 276 | |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 277 | /** |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 278 | * Process an incoming PIM JoinPrune message. |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 279 | * |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 280 | * @param ethPkt the Ethernet packet header. |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 281 | */ |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 282 | public void processJoinPrune(Ethernet ethPkt) { |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 283 | // TODO: add Join/Prune processing code. |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 284 | } |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 285 | } |