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 | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 29 | import java.util.Set; |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 30 | |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 31 | import static org.slf4j.LoggerFactory.getLogger; |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
Ray Milkey | a059a70 | 2016-01-12 11:10:33 -0800 | [diff] [blame] | 34 | * PIM Interface represents an ONOS Interface with IP and MAC addresses for |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 35 | * a given ConnectPoint. |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 36 | */ |
| 37 | public class PIMInterface { |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 38 | |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 39 | private final Logger log = getLogger(getClass()); |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 40 | |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 41 | private Interface onosInterface; |
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 | // Our hello opt holdtime |
| 44 | private short holdtime = PIMHelloOption.DEFAULT_HOLDTIME; |
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 prune delay |
| 47 | private int pruneDelay = PIMHelloOption.DEFAULT_PRUNEDELAY; |
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 | // Neighbor priority |
| 50 | private int priority = PIMHelloOption.DEFAULT_PRIORITY; |
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 | // Our current genid |
| 53 | private int genid = PIMHelloOption.DEFAULT_GENID; // Needs to be assigned. |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 54 | |
| 55 | /** |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 56 | * Create a PIMInterface from an ONOS Interface. |
Charles Chan | 30ba400 | 2015-11-05 14:45:16 -0800 | [diff] [blame] | 57 | * |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 58 | * @param intf the ONOS Interface. |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 59 | */ |
| 60 | public PIMInterface(Interface intf) { |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 61 | onosInterface = intf; |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /** |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 65 | * Return the ONOS Interface. |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 66 | * |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 67 | * @return ONOS Interface. |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 68 | */ |
| 69 | public Interface getInterface() { |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 70 | return this.onosInterface; |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /** |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 74 | * Set the ONOS Interface, it will override a previous value. |
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 | * @param intf ONOS Interface. |
| 77 | */ |
| 78 | public PIMInterface setInterface(Interface intf) { |
| 79 | this.onosInterface = intf; |
| 80 | return this; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Get the set of IP Addresses associated with this interface. |
| 85 | * |
| 86 | * @return a set of Ip Addresses on this interface |
| 87 | */ |
| 88 | public Set<InterfaceIpAddress> getIpAddresses() { |
| 89 | return this.onosInterface.ipAddresses(); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Return a single "best" IP address. |
| 94 | * |
| 95 | * @return the choosen IP address or null if none |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 96 | */ |
| 97 | public IpAddress getIpAddress() { |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 98 | if (onosInterface.ipAddresses().isEmpty()) { |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 99 | return null; |
| 100 | } |
| 101 | |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 102 | IpAddress ipaddr = null; |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 103 | for (InterfaceIpAddress ifipaddr : onosInterface.ipAddresses()) { |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 104 | ipaddr = ifipaddr.ipAddress(); |
| 105 | break; |
| 106 | } |
| 107 | return ipaddr; |
| 108 | } |
| 109 | |
| 110 | /** |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 111 | * Get the holdtime. |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 112 | * |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 113 | * @return the holdtime |
| 114 | */ |
| 115 | public short getHoldtime() { |
| 116 | return this.holdtime; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Get the prune delay. |
| 121 | * |
| 122 | * @return The prune delay |
| 123 | */ |
| 124 | public int getPruneDelay() { |
| 125 | return this.pruneDelay; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Get our hello priority. |
| 130 | * |
| 131 | * @return our priority |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 132 | */ |
| 133 | public int getPriority() { |
| 134 | return this.priority; |
| 135 | } |
| 136 | |
| 137 | /** |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 138 | * Get our generation ID. |
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 our generation ID |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 141 | */ |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 142 | public int getGenid() { |
| 143 | return this.genid; |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /** |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 147 | * Process an incoming PIM Hello message. |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 148 | * |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 149 | * @param ethPkt the Ethernet packet header |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 150 | */ |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 151 | public void processHello(Ethernet ethPkt) { |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 152 | |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 153 | // We'll need to save our neighbors MAC address |
| 154 | MacAddress nbrmac = ethPkt.getSourceMAC(); |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 155 | |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 156 | // And we'll need to save neighbors IP Address. |
| 157 | IPv4 iphdr = (IPv4) ethPkt.getPayload(); |
| 158 | IpAddress srcip = IpAddress.valueOf(iphdr.getSourceAddress()); |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 159 | |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 160 | PIM pimhdr = (PIM) iphdr.getPayload(); |
| 161 | if (pimhdr.getPimMsgType() != PIM.TYPE_HELLO) { |
| 162 | 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] | 163 | return; |
| 164 | } |
| 165 | |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 166 | // TODO: Maybe a good idea to check the checksum. Let's jump into the hello options. |
| 167 | PIMHello hello = (PIMHello) pimhdr.getPayload(); |
| 168 | |
| 169 | // TODO: this is about where we find or create our PIMNeighbor |
| 170 | |
| 171 | boolean reElectDr = false; |
| 172 | |
| 173 | // Start walking through all the hello options to handle accordingly. |
| 174 | for (PIMHelloOption opt : hello.getOptions().values()) { |
| 175 | |
| 176 | // TODO: This is where we handle the options and modify the neighbor accordingly. |
| 177 | // We'll need to create the PIMNeighbor class next. Depending on what happens |
| 178 | // we may need to re-elect a DR |
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 | |
| 181 | if (reElectDr) { |
| 182 | // TODO: create an election() method and call it here with a PIMNeighbor |
| 183 | } |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /** |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 187 | * Process an incoming PIM JoinPrune message. |
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 | * @param ethPkt the Ethernet packet header. |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 190 | */ |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 191 | public void processJoinPrune(Ethernet ethPkt) { |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 192 | |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 193 | } |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 194 | } |