blob: 1d09e35ca8e57ad83a7becf295b30e89b23c2d4d [file] [log] [blame]
Rusty Eddy95421642015-10-21 17:22:13 -07001/*
Rusty Eddybcad55b2016-02-11 18:56:09 -08002 * Copyright 2015, 2016 Open Networking Laboratory
Rusty Eddy95421642015-10-21 17:22:13 -07003 *
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 */
16package org.onosproject.pim.impl;
17
18import org.onlab.packet.Ethernet;
19import org.onlab.packet.IPv4;
Rusty Eddy4d5a92f2016-01-25 17:12:14 -080020import org.onlab.packet.Ip4Address;
Rusty Eddy95421642015-10-21 17:22:13 -070021import org.onlab.packet.IpAddress;
Rusty Eddybcad55b2016-02-11 18:56:09 -080022import org.onlab.packet.IpPrefix;
Rusty Eddy95421642015-10-21 17:22:13 -070023import org.onlab.packet.MacAddress;
24import org.onlab.packet.PIM;
Jonathan Hart00cddda2016-02-16 10:30:37 -080025import org.onlab.packet.pim.PIMAddrUnicast;
Rusty Eddy95421642015-10-21 17:22:13 -070026import org.onlab.packet.pim.PIMHello;
27import org.onlab.packet.pim.PIMHelloOption;
Rusty Eddybcad55b2016-02-11 18:56:09 -080028import org.onlab.packet.pim.PIMJoinPrune;
29import org.onlab.packet.pim.PIMJoinPruneGroup;
Rusty Eddy95421642015-10-21 17:22:13 -070030import org.onosproject.incubator.net.intf.Interface;
Jonathan Hart36fd31e2016-01-28 15:55:31 -080031import org.onosproject.net.flow.DefaultTrafficTreatment;
32import org.onosproject.net.flow.TrafficTreatment;
Rusty Eddy95421642015-10-21 17:22:13 -070033import org.onosproject.net.host.InterfaceIpAddress;
Jonathan Hart00cddda2016-02-16 10:30:37 -080034import org.onosproject.net.mcast.McastRoute;
Jonathan Hart36fd31e2016-01-28 15:55:31 -080035import org.onosproject.net.packet.DefaultOutboundPacket;
36import org.onosproject.net.packet.PacketService;
Rusty Eddy95421642015-10-21 17:22:13 -070037import org.slf4j.Logger;
Rusty Eddy95421642015-10-21 17:22:13 -070038
Jonathan Hart36fd31e2016-01-28 15:55:31 -080039import java.nio.ByteBuffer;
Jonathan Hart54119bb2016-02-06 18:48:27 -080040import java.util.Collection;
Rusty Eddy390498d2016-01-15 19:21:32 -080041import java.util.HashMap;
Jonathan Hart00cddda2016-02-16 10:30:37 -080042import java.util.List;
Rusty Eddy390498d2016-01-15 19:21:32 -080043import java.util.Map;
Jonathan Hart5af5f142016-01-28 18:45:27 -080044import java.util.Random;
Rusty Eddy4ae5aa82015-12-15 12:58:27 -080045import java.util.Set;
Jonathan Hart00cddda2016-02-16 10:30:37 -080046import java.util.concurrent.ConcurrentHashMap;
Jonathan Hart6be70952016-02-12 21:11:26 -080047import java.util.concurrent.TimeUnit;
Jonathan Hart54119bb2016-02-06 18:48:27 -080048import java.util.stream.Collectors;
Rusty Eddy95421642015-10-21 17:22:13 -070049
Jonathan Hart5af5f142016-01-28 18:45:27 -080050import static com.google.common.base.Preconditions.checkArgument;
Rusty Eddy390498d2016-01-15 19:21:32 -080051import static com.google.common.base.Preconditions.checkNotNull;
Rusty Eddy4ae5aa82015-12-15 12:58:27 -080052import static org.slf4j.LoggerFactory.getLogger;
Rusty Eddy95421642015-10-21 17:22:13 -070053
54/**
Ray Milkeya059a702016-01-12 11:10:33 -080055 * PIM Interface represents an ONOS Interface with IP and MAC addresses for
Rusty Eddy4ae5aa82015-12-15 12:58:27 -080056 * a given ConnectPoint.
Rusty Eddy95421642015-10-21 17:22:13 -070057 */
Jonathan Hart5af5f142016-01-28 18:45:27 -080058public final class PIMInterface {
Rusty Eddy95421642015-10-21 17:22:13 -070059
Rusty Eddy4ae5aa82015-12-15 12:58:27 -080060 private final Logger log = getLogger(getClass());
Rusty Eddy95421642015-10-21 17:22:13 -070061
Jonathan Hart00cddda2016-02-16 10:30:37 -080062 private static final int JOIN_PERIOD = 60;
63 private static final double HOLD_TIME_MULTIPLIER = 3.5;
64
Jonathan Hart36fd31e2016-01-28 15:55:31 -080065 private final PacketService packetService;
66
Rusty Eddy4ae5aa82015-12-15 12:58:27 -080067 private Interface onosInterface;
Jonathan Hart36fd31e2016-01-28 15:55:31 -080068 private final TrafficTreatment outputTreatment;
Rusty Eddy95421642015-10-21 17:22:13 -070069
Rusty Eddy4ae5aa82015-12-15 12:58:27 -080070 // Our hello opt holdtime
71 private short holdtime = PIMHelloOption.DEFAULT_HOLDTIME;
Rusty Eddy95421642015-10-21 17:22:13 -070072
Rusty Eddy4ae5aa82015-12-15 12:58:27 -080073 // Our hello opt prune delay
74 private int pruneDelay = PIMHelloOption.DEFAULT_PRUNEDELAY;
Rusty Eddy95421642015-10-21 17:22:13 -070075
Rusty Eddy4ae5aa82015-12-15 12:58:27 -080076 // Neighbor priority
77 private int priority = PIMHelloOption.DEFAULT_PRIORITY;
Rusty Eddy95421642015-10-21 17:22:13 -070078
Jonathan Hart6be70952016-02-12 21:11:26 -080079 private final int helloInterval;
80
81 private long lastHello;
82
Rusty Eddy4ae5aa82015-12-15 12:58:27 -080083 // Our current genid
Jonathan Hart5af5f142016-01-28 18:45:27 -080084 private final int generationId;
Rusty Eddy95421642015-10-21 17:22:13 -070085
Rusty Eddy390498d2016-01-15 19:21:32 -080086 // The IP address of the DR
Jonathan Hart5af5f142016-01-28 18:45:27 -080087 private IpAddress drIpaddress;
Rusty Eddy390498d2016-01-15 19:21:32 -080088
89 // A map of all our PIM neighbors keyed on our neighbors IP address
90 private Map<IpAddress, PIMNeighbor> pimNeighbors = new HashMap<>();
91
Jonathan Hart00cddda2016-02-16 10:30:37 -080092 private Map<McastRoute, RouteData> routes = new ConcurrentHashMap<>();
93
Rusty Eddy95421642015-10-21 17:22:13 -070094 /**
Rusty Eddy4ae5aa82015-12-15 12:58:27 -080095 * Create a PIMInterface from an ONOS Interface.
Charles Chan30ba4002015-11-05 14:45:16 -080096 *
Rusty Eddy4ae5aa82015-12-15 12:58:27 -080097 * @param intf the ONOS Interface.
Jonathan Hart5af5f142016-01-28 18:45:27 -080098 * @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 Eddy95421642015-10-21 17:22:13 -0700103 */
Jonathan Hart5af5f142016-01-28 18:45:27 -0800104 private PIMInterface(Interface intf,
Jonathan Hart6be70952016-02-12 21:11:26 -0800105 int helloInterval,
106 short holdTime,
107 int priority,
108 short propagationDelay,
109 short overrideInterval,
110 PacketService packetService) {
Jonathan Hart5af5f142016-01-28 18:45:27 -0800111
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800112 onosInterface = intf;
Jonathan Hart36fd31e2016-01-28 15:55:31 -0800113 outputTreatment = createOutputTreatment();
Jonathan Hart6be70952016-02-12 21:11:26 -0800114 this.helloInterval = helloInterval;
Jonathan Hart5af5f142016-01-28 18:45:27 -0800115 this.holdtime = holdTime;
Jonathan Hart36fd31e2016-01-28 15:55:31 -0800116 this.packetService = packetService;
Rusty Eddy390498d2016-01-15 19:21:32 -0800117 IpAddress ourIp = getIpAddress();
118 MacAddress mac = intf.mac();
119
Jonathan Hart6be70952016-02-12 21:11:26 -0800120 lastHello = 0;
121
Jonathan Hart5af5f142016-01-28 18:45:27 -0800122 generationId = new Random().nextInt();
123
Rusty Eddy390498d2016-01-15 19:21:32 -0800124 // Create a PIM Neighbor to represent ourselves for DR election.
Jonathan Hart54119bb2016-02-06 18:48:27 -0800125 PIMNeighbor us = new PIMNeighbor(ourIp, mac, holdTime, 0, priority, generationId);
Rusty Eddy390498d2016-01-15 19:21:32 -0800126
127 pimNeighbors.put(ourIp, us);
128 drIpaddress = ourIp;
Rusty Eddy95421642015-10-21 17:22:13 -0700129 }
130
Jonathan Hart36fd31e2016-01-28 15:55:31 -0800131 private TrafficTreatment createOutputTreatment() {
132 return DefaultTrafficTreatment.builder()
133 .setOutput(onosInterface.connectPoint().port())
134 .build();
135 }
136
Rusty Eddy95421642015-10-21 17:22:13 -0700137 /**
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800138 * Return the ONOS Interface.
Rusty Eddy95421642015-10-21 17:22:13 -0700139 *
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800140 * @return ONOS Interface.
Rusty Eddy95421642015-10-21 17:22:13 -0700141 */
142 public Interface getInterface() {
Rusty Eddy390498d2016-01-15 19:21:32 -0800143 return onosInterface;
144
Rusty Eddy95421642015-10-21 17:22:13 -0700145 }
146
147 /**
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800148 * Set the ONOS Interface, it will override a previous value.
Rusty Eddy95421642015-10-21 17:22:13 -0700149 *
Jian Lidfba7392016-01-22 16:46:58 -0800150 * @param intf ONOS Interface
151 * @return PIM interface instance
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800152 */
153 public PIMInterface setInterface(Interface intf) {
Rusty Eddy390498d2016-01-15 19:21:32 -0800154 onosInterface = intf;
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800155 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 Hart00cddda2016-02-16 10:30:37 -0800163 public List<InterfaceIpAddress> getIpAddresses() {
164 return onosInterface.ipAddressesList();
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800165 }
166
167 /**
168 * Return a single "best" IP address.
169 *
170 * @return the choosen IP address or null if none
Rusty Eddy95421642015-10-21 17:22:13 -0700171 */
172 public IpAddress getIpAddress() {
Jonathan Hart00cddda2016-02-16 10:30:37 -0800173 if (onosInterface.ipAddressesList().isEmpty()) {
Rusty Eddy95421642015-10-21 17:22:13 -0700174 return null;
175 }
176
Rusty Eddy95421642015-10-21 17:22:13 -0700177 IpAddress ipaddr = null;
Jonathan Hart00cddda2016-02-16 10:30:37 -0800178 for (InterfaceIpAddress ifipaddr : onosInterface.ipAddressesList()) {
Rusty Eddy95421642015-10-21 17:22:13 -0700179 ipaddr = ifipaddr.ipAddress();
180 break;
181 }
182 return ipaddr;
183 }
184
185 /**
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800186 * Get the holdtime.
Rusty Eddy95421642015-10-21 17:22:13 -0700187 *
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800188 * @return the holdtime
189 */
190 public short getHoldtime() {
Rusty Eddy390498d2016-01-15 19:21:32 -0800191 return holdtime;
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800192 }
193
194 /**
195 * Get the prune delay.
196 *
197 * @return The prune delay
198 */
199 public int getPruneDelay() {
Rusty Eddy390498d2016-01-15 19:21:32 -0800200 return pruneDelay;
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800201 }
202
203 /**
204 * Get our hello priority.
205 *
206 * @return our priority
Rusty Eddy95421642015-10-21 17:22:13 -0700207 */
208 public int getPriority() {
Rusty Eddy390498d2016-01-15 19:21:32 -0800209 return priority;
Rusty Eddy95421642015-10-21 17:22:13 -0700210 }
211
212 /**
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800213 * Get our generation ID.
Rusty Eddy95421642015-10-21 17:22:13 -0700214 *
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800215 * @return our generation ID
Rusty Eddy95421642015-10-21 17:22:13 -0700216 */
Jonathan Hart5af5f142016-01-28 18:45:27 -0800217 public int getGenerationId() {
218 return generationId;
Rusty Eddy95421642015-10-21 17:22:13 -0700219 }
220
221 /**
Jonathan Hart54119bb2016-02-06 18:48:27 -0800222 * Gets the neighbors seen on this interface.
223 *
224 * @return PIM neighbors
225 */
226 public Collection<PIMNeighbor> getNeighbors() {
227 return pimNeighbors.values();
228 }
229
Jonathan Hart00cddda2016-02-16 10:30:37 -0800230 public Collection<McastRoute> getRoutes() {
231 return routes.keySet();
232 }
233
Jonathan Hart54119bb2016-02-06 18:48:27 -0800234 /**
235 * Checks whether any of our neighbors have expired, and cleans up their
236 * state if they have.
237 */
238 public void checkNeighborTimeouts() {
239 Set<PIMNeighbor> expired = pimNeighbors.values().stream()
240 // Don't time ourselves out!
241 .filter(neighbor -> !neighbor.ipAddress().equals(getIpAddress()))
242 .filter(neighbor -> neighbor.isExpired())
243 .collect(Collectors.toSet());
244
245 for (PIMNeighbor neighbor : expired) {
246 log.info("Timing out neighbor {}", neighbor);
247 pimNeighbors.remove(neighbor.ipAddress(), neighbor);
248 }
249 }
250
251 /**
Rusty Eddy4d5a92f2016-01-25 17:12:14 -0800252 * 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 Hart6be70952016-02-12 21:11:26 -0800257 if (lastHello + TimeUnit.SECONDS.toMillis(helloInterval) >
258 System.currentTimeMillis()) {
259 return;
260 }
261
262 lastHello = System.currentTimeMillis();
263
Rusty Eddy4d5a92f2016-01-25 17:12:14 -0800264 // Create the base PIM Packet and mark it a hello packet
265 PIMPacket pimPacket = new PIMPacket(PIM.TYPE_HELLO);
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 Hart5af5f142016-01-28 18:45:27 -0800274 hello.addOption(PIMHelloOption.createHoldTime(holdtime));
275 hello.addOption(PIMHelloOption.createPriority(priority));
276 hello.addOption(PIMHelloOption.createGenID(generationId));
Rusty Eddy4d5a92f2016-01-25 17:12:14 -0800277
278 // Now set the hello option payload
279 pimPacket.setPIMPayload(hello);
280
Jonathan Hart36fd31e2016-01-28 15:55:31 -0800281 packetService.emit(new DefaultOutboundPacket(
282 onosInterface.connectPoint().deviceId(),
283 outputTreatment,
284 ByteBuffer.wrap(pimPacket.getEthernet().serialize())));
Rusty Eddy4d5a92f2016-01-25 17:12:14 -0800285 }
286
287 /**
Rusty Eddy390498d2016-01-15 19:21:32 -0800288 * 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 Hart54119bb2016-02-06 18:48:27 -0800294 * <li>We will refresh the neighbor's timestamp</li>
Rusty Eddy390498d2016-01-15 19:21:32 -0800295 * </ul>
Rusty Eddy95421642015-10-21 17:22:13 -0700296 *
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800297 * @param ethPkt the Ethernet packet header
Rusty Eddy95421642015-10-21 17:22:13 -0700298 */
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800299 public void processHello(Ethernet ethPkt) {
Rusty Eddy95421642015-10-21 17:22:13 -0700300
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800301 // We'll need to save our neighbors MAC address
302 MacAddress nbrmac = ethPkt.getSourceMAC();
Rusty Eddy95421642015-10-21 17:22:13 -0700303
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800304 // And we'll need to save neighbors IP Address.
305 IPv4 iphdr = (IPv4) ethPkt.getPayload();
306 IpAddress srcip = IpAddress.valueOf(iphdr.getSourceAddress());
Rusty Eddy95421642015-10-21 17:22:13 -0700307
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800308 PIM pimhdr = (PIM) iphdr.getPayload();
309 if (pimhdr.getPimMsgType() != PIM.TYPE_HELLO) {
310 log.error("process Hello has received a non hello packet type: " + pimhdr.getPimMsgType());
Rusty Eddy95421642015-10-21 17:22:13 -0700311 return;
312 }
313
Rusty Eddy390498d2016-01-15 19:21:32 -0800314 // get the DR values for later calculation
315 PIMNeighbor dr = pimNeighbors.get(drIpaddress);
316 checkNotNull(dr);
317
318 IpAddress drip = drIpaddress;
Jonathan Hart54119bb2016-02-06 18:48:27 -0800319 int drpri = dr.priority();
Rusty Eddy390498d2016-01-15 19:21:32 -0800320
321 // Assume we do not need to run a DR election
322 boolean reElectDr = false;
323 boolean genidChanged = false;
324
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800325 PIMHello hello = (PIMHello) pimhdr.getPayload();
326
Rusty Eddy390498d2016-01-15 19:21:32 -0800327 // Determine if we already have a PIMNeighbor
328 PIMNeighbor nbr = pimNeighbors.getOrDefault(srcip, null);
Jonathan Hart54119bb2016-02-06 18:48:27 -0800329 PIMNeighbor newNbr = PIMNeighbor.createPimNeighbor(srcip, nbrmac, hello.getOptions().values());
330
Rusty Eddy390498d2016-01-15 19:21:32 -0800331 if (nbr == null) {
Jonathan Hart54119bb2016-02-06 18:48:27 -0800332 pimNeighbors.putIfAbsent(srcip, newNbr);
333 nbr = newNbr;
334 } else if (!nbr.equals(newNbr)) {
335 if (newNbr.holdtime() == 0) {
336 // Neighbor has shut down. Remove them and clean up
337 pimNeighbors.remove(srcip, nbr);
338 return;
339 } else {
340 // Neighbor has changed one of their options.
341 pimNeighbors.put(srcip, newNbr);
342 nbr = newNbr;
Rusty Eddy390498d2016-01-15 19:21:32 -0800343 }
Rusty Eddy95421642015-10-21 17:22:13 -0700344 }
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800345
Jonathan Hart54119bb2016-02-06 18:48:27 -0800346 // Refresh this neighbor's timestamp
Rusty Eddy390498d2016-01-15 19:21:32 -0800347 nbr.refreshTimestamp();
348
349 /*
Jonathan Hart5af5f142016-01-28 18:45:27 -0800350 * the election method will first determine if an election
Rusty Eddy390498d2016-01-15 19:21:32 -0800351 * needs to be run, if so it will run the election. The
352 * IP address of the DR will be returned. If the IP address
353 * of the DR is different from what we already have we know a
354 * new DR has been elected.
355 */
356 IpAddress electedIp = election(nbr, drip, drpri);
357 if (!drip.equals(electedIp)) {
358 // we have a new DR.
359 drIpaddress = electedIp;
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800360 }
Rusty Eddy95421642015-10-21 17:22:13 -0700361 }
362
Rusty Eddy390498d2016-01-15 19:21:32 -0800363 // Run an election if we need to. Return the elected IP address.
Jonathan Hart5af5f142016-01-28 18:45:27 -0800364 private IpAddress election(PIMNeighbor nbr, IpAddress drIp, int drPriority) {
Rusty Eddy390498d2016-01-15 19:21:32 -0800365
Jonathan Hart54119bb2016-02-06 18:48:27 -0800366 IpAddress nbrIp = nbr.ipAddress();
367 if (nbr.priority() > drPriority) {
Jonathan Hart5af5f142016-01-28 18:45:27 -0800368 return nbrIp;
Rusty Eddy390498d2016-01-15 19:21:32 -0800369 }
370
Jonathan Hart5af5f142016-01-28 18:45:27 -0800371 if (nbrIp.compareTo(drIp) > 0) {
372 return nbrIp;
Rusty Eddy390498d2016-01-15 19:21:32 -0800373 }
Jonathan Hart5af5f142016-01-28 18:45:27 -0800374 return drIp;
Rusty Eddy390498d2016-01-15 19:21:32 -0800375 }
376
Rusty Eddy95421642015-10-21 17:22:13 -0700377 /**
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800378 * Process an incoming PIM JoinPrune message.
Rusty Eddy95421642015-10-21 17:22:13 -0700379 *
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800380 * @param ethPkt the Ethernet packet header.
Rusty Eddy95421642015-10-21 17:22:13 -0700381 */
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800382 public void processJoinPrune(Ethernet ethPkt) {
Rusty Eddybcad55b2016-02-11 18:56:09 -0800383
384 IPv4 ip = (IPv4) ethPkt.getPayload();
385 checkNotNull(ip);
386
387 PIM pim = (PIM) ip.getPayload();
388 checkNotNull(pim);
389
390 PIMJoinPrune jpHdr = (PIMJoinPrune) pim.getPayload();
391 checkNotNull(jpHdr);
392
393 /*
394 * The Join/Prune messages are grouped by Group address. We'll walk each group address
395 * where we will possibly have to walk a list of source address for the joins and prunes.
396 */
397 Collection<PIMJoinPruneGroup> jpgs = jpHdr.getJoinPrunes();
398 for (PIMJoinPruneGroup jpg : jpgs) {
399 IpPrefix gpfx = jpg.getGroup();
400
401 // Walk the joins first.
402 for (IpPrefix spfx : jpg.getJoins().values()) {
403
404 // We may need
405
406
407 }
408
409 for (IpPrefix spfx : jpg.getPrunes().values()) {
410
411 // TODO: this is where we many need to remove multi-cast state and possibly intents.
412
413 }
414 }
415
Rusty Eddy95421642015-10-21 17:22:13 -0700416 }
Jonathan Hart5af5f142016-01-28 18:45:27 -0800417
Jonathan Hart00cddda2016-02-16 10:30:37 -0800418 public void addRoute(McastRoute route, IpAddress nextHop, MacAddress nextHopMac) {
419 RouteData data = new RouteData(nextHop, nextHopMac);
420 routes.put(route, data);
421
422 sendJoinPrune(route, data, true);
423 }
424
425 public void removeRoute(McastRoute route) {
426 RouteData data = routes.remove(route);
427
428 if (data != null) {
429 sendJoinPrune(route, data, false);
430 }
431 }
432
433 public void sendJoins() {
434 routes.entrySet().forEach(entry -> {
435 if (entry.getValue().timestamp + TimeUnit.SECONDS.toMillis(JOIN_PERIOD) >
436 System.currentTimeMillis()) {
437 return;
438 }
439
440 sendJoinPrune(entry.getKey(), entry.getValue(), true);
441 });
442 }
443
444 private void sendJoinPrune(McastRoute route, RouteData data, boolean join) {
445 PIMJoinPrune jp = new PIMJoinPrune();
446
447 jp.addJoinPrune(route.source().toIpPrefix(), route.group().toIpPrefix(), join);
448 jp.setHoldTime(join ? (short) Math.floor(JOIN_PERIOD * HOLD_TIME_MULTIPLIER) : 0);
449 jp.setUpstreamAddr(new PIMAddrUnicast(data.ipAddress.toString()));
450
451 PIM pim = new PIM();
452 pim.setPIMType(PIM.TYPE_JOIN_PRUNE_REQUEST);
453 pim.setPayload(jp);
454
455 IPv4 ipv4 = new IPv4();
456 ipv4.setDestinationAddress(PIM.PIM_ADDRESS.getIp4Address().toInt());
457 ipv4.setSourceAddress(getIpAddress().getIp4Address().toInt());
458 ipv4.setProtocol(IPv4.PROTOCOL_PIM);
459 ipv4.setTtl((byte) 1);
460 ipv4.setDiffServ((byte) 0xc0);
461 ipv4.setPayload(pim);
462
463 Ethernet eth = new Ethernet();
464 eth.setSourceMACAddress(onosInterface.mac());
465 eth.setDestinationMACAddress(MacAddress.valueOf("01:00:5E:00:00:0d"));
466 eth.setEtherType(Ethernet.TYPE_IPV4);
467 eth.setPayload(ipv4);
468
469 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
470 .setOutput(onosInterface.connectPoint().port())
471 .build();
472
473 packetService.emit(new DefaultOutboundPacket(onosInterface.connectPoint().deviceId(),
474 treatment, ByteBuffer.wrap(eth.serialize())));
475
476 data.timestamp = System.currentTimeMillis();
477 }
478
479 /*private void sendPrune(McastRoute route, RouteData data) {
480 PIMJoinPrune jp = new PIMJoinPrune();
481
482 jp.addJoinPrune(route.source().toIpPrefix(), route.group().toIpPrefix(), false);
483 jp.setHoldTime((short) 0);
484 jp.setUpstreamAddr(new PIMAddrUnicast(data.ipAddress.toString()));
485
486 PIM pim = new PIM();
487 pim.setPIMType(PIM.TYPE_JOIN_PRUNE_REQUEST);
488 pim.setPayload(jp);
489
490 IPv4 ipv4 = new IPv4();
491 ipv4.setDestinationAddress(PIM.PIM_ADDRESS.getIp4Address().toInt());
492 ipv4.setSourceAddress(getIpAddress().getIp4Address().toInt());
493 ipv4.setProtocol(IPv4.PROTOCOL_PIM);
494 ipv4.setTtl((byte) 1);
495 ipv4.setDiffServ((byte) 0xc0);
496 ipv4.setPayload(pim);
497
498 Ethernet eth = new Ethernet();
499 eth.setSourceMACAddress(onosInterface.mac());
500 eth.setDestinationMACAddress(MacAddress.valueOf("01:00:5E:00:00:0d"));
501 eth.setEtherType(Ethernet.TYPE_IPV4);
502 eth.setPayload(ipv4);
503
504 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
505 .setOutput(onosInterface.connectPoint().port())
506 .build();
507
508 packetService.emit(new DefaultOutboundPacket(onosInterface.connectPoint().deviceId(),
509 treatment, ByteBuffer.wrap(eth.serialize())));
510 }*/
511
Jonathan Hart5af5f142016-01-28 18:45:27 -0800512 /**
513 * Returns a builder for a PIM interface.
514 *
515 * @return PIM interface builder
516 */
517 public static Builder builder() {
518 return new Builder();
519 }
520
521 /**
522 * Builder for a PIM interface.
523 */
524 public static class Builder {
525 private Interface intf;
526 private PacketService packetService;
Jonathan Hart6be70952016-02-12 21:11:26 -0800527 private int helloInterval = PIMInterfaceManager.DEFAULT_HELLO_INTERVAL;
Jonathan Hart5af5f142016-01-28 18:45:27 -0800528 private short holdtime = PIMHelloOption.DEFAULT_HOLDTIME;
529 private int priority = PIMHelloOption.DEFAULT_PRIORITY;
530 private short propagationDelay = PIMHelloOption.DEFAULT_PRUNEDELAY;
531 private short overrideInterval = PIMHelloOption.DEFAULT_OVERRIDEINTERVAL;
532
533 /**
534 * Uses the specified ONOS interface.
535 *
536 * @param intf ONOS interface
537 * @return this PIM interface builder
538 */
539 public Builder withInterface(Interface intf) {
540 this.intf = checkNotNull(intf);
541 return this;
542 }
543
544 /**
545 * Sets the reference to the packet service.
546 *
547 * @param packetService packet service
548 * @return this PIM interface builder
549 */
550 public Builder withPacketService(PacketService packetService) {
551 this.packetService = checkNotNull(packetService);
552 return this;
553 }
554
555 /**
Jonathan Hart6be70952016-02-12 21:11:26 -0800556 * Users the specified hello interval.
557 *
558 * @param helloInterval hello interval in seconds
559 * @return this PIM interface builder
560 */
561 public Builder withHelloInterval(int helloInterval) {
562 this.helloInterval = helloInterval;
563 return this;
564 }
565
566 /**
Jonathan Hart5af5f142016-01-28 18:45:27 -0800567 * Uses the specified hold time.
568 *
569 * @param holdTime hold time in seconds
570 * @return this PIM interface builder
571 */
572 public Builder withHoldTime(short holdTime) {
573 this.holdtime = holdTime;
574 return this;
575 }
576
577 /**
578 * Uses the specified DR priority.
579 *
580 * @param priority DR priority
581 * @return this PIM interface builder
582 */
583 public Builder withPriority(int priority) {
584 this.priority = priority;
585 return this;
586 }
587
588 /**
589 * Uses the specified propagation delay.
590 *
591 * @param propagationDelay propagation delay in ms
592 * @return this PIM interface builder
593 */
594 public Builder withPropagationDelay(short propagationDelay) {
595 this.propagationDelay = propagationDelay;
596 return this;
597 }
598
599 /**
600 * Uses the specified override interval.
601 *
602 * @param overrideInterval override interval in ms
603 * @return this PIM interface builder
604 */
605 public Builder withOverrideInterval(short overrideInterval) {
606 this.overrideInterval = overrideInterval;
607 return this;
608 }
609
610 /**
611 * Builds the PIM interface.
612 *
613 * @return PIM interface
614 */
615 public PIMInterface build() {
616 checkArgument(intf != null, "Must provide an interface");
617 checkArgument(packetService != null, "Must provide a packet service");
618
Jonathan Hart6be70952016-02-12 21:11:26 -0800619 return new PIMInterface(intf, helloInterval, holdtime, priority,
620 propagationDelay, overrideInterval, packetService);
Jonathan Hart5af5f142016-01-28 18:45:27 -0800621 }
622
623 }
Jonathan Hart00cddda2016-02-16 10:30:37 -0800624
625 private static class RouteData {
626 public final IpAddress ipAddress;
627 public final MacAddress macAddress;
628 public long timestamp;
629
630 public RouteData(IpAddress ip, MacAddress mac) {
631 this.ipAddress = ip;
632 this.macAddress = mac;
633 timestamp = System.currentTimeMillis();
634 }
635 }
Rusty Eddy95421642015-10-21 17:22:13 -0700636}