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