Rusty Eddy
Committed by Gerrit Code Review

Added a Scheduled Executor, PIM packet maker to generate hello packets

Change-Id: I27b91a9eb2906e9345223382238fd4fcfd1397f4
...@@ -17,6 +17,7 @@ package org.onosproject.pim.impl; ...@@ -17,6 +17,7 @@ package org.onosproject.pim.impl;
17 17
18 import org.onlab.packet.Ethernet; 18 import org.onlab.packet.Ethernet;
19 import org.onlab.packet.IPv4; 19 import org.onlab.packet.IPv4;
20 +import org.onlab.packet.Ip4Address;
20 import org.onlab.packet.IpAddress; 21 import org.onlab.packet.IpAddress;
21 import org.onlab.packet.MacAddress; 22 import org.onlab.packet.MacAddress;
22 import org.onlab.packet.PIM; 23 import org.onlab.packet.PIM;
...@@ -166,6 +167,30 @@ public class PIMInterface { ...@@ -166,6 +167,30 @@ public class PIMInterface {
166 } 167 }
167 168
168 /** 169 /**
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 + /**
169 * Process an incoming PIM Hello message. There are a few things going on in 194 * Process an incoming PIM Hello message. There are a few things going on in
170 * this method: 195 * this method:
171 * <ul> 196 * <ul>
......
...@@ -28,6 +28,9 @@ import org.onosproject.net.ConnectPoint; ...@@ -28,6 +28,9 @@ import org.onosproject.net.ConnectPoint;
28 import org.onosproject.net.provider.ProviderId; 28 import org.onosproject.net.provider.ProviderId;
29 import org.slf4j.Logger; 29 import org.slf4j.Logger;
30 import java.util.Map; 30 import java.util.Map;
31 +import java.util.concurrent.Executors;
32 +import java.util.concurrent.ScheduledExecutorService;
33 +import java.util.concurrent.TimeUnit;
31 34
32 import static org.slf4j.LoggerFactory.getLogger; 35 import static org.slf4j.LoggerFactory.getLogger;
33 36
...@@ -45,6 +48,17 @@ public class PIMInterfaceManager implements PIMInterfaceService { ...@@ -45,6 +48,17 @@ public class PIMInterfaceManager implements PIMInterfaceService {
45 // Create ourselves a provider ID 48 // Create ourselves a provider ID
46 private static final ProviderId PID = new ProviderId("pim", "org.onosproject.pim"); 49 private static final ProviderId PID = new ProviderId("pim", "org.onosproject.pim");
47 50
51 + // Create a Scheduled Executor service to send PIM hellos
52 + private final ScheduledExecutorService helloScheduler =
53 + Executors.newScheduledThreadPool(1);
54 +
55 + // Wait for a bout 3 seconds before sending the initial hello messages.
56 + // TODO: make this tunnable.
57 + private final long initialHelloDelay = (long) 3;
58 +
59 + // Send PIM hello packets: 30 seconds.
60 + private final long pimHelloPeriod = (long) 30;
61 +
48 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 62 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
49 protected InterfaceService interfaceService; 63 protected InterfaceService interfaceService;
50 64
...@@ -60,10 +74,24 @@ public class PIMInterfaceManager implements PIMInterfaceService { ...@@ -60,10 +74,24 @@ public class PIMInterfaceManager implements PIMInterfaceService {
60 for (Interface intf : interfaceService.getInterfaces()) { 74 for (Interface intf : interfaceService.getInterfaces()) {
61 pimInterfaces.put(intf.connectPoint(), new PIMInterface(intf)); 75 pimInterfaces.put(intf.connectPoint(), new PIMInterface(intf));
62 } 76 }
77 +
78 + // Schedule the periodic hello sender.
79 + helloScheduler.scheduleAtFixedRate(new Runnable() {
80 + @Override
81 + public void run() {
82 + for (PIMInterface pif : pimInterfaces.values()) {
83 + pif.sendHello();
84 + }
85 + }
86 + }, initialHelloDelay, pimHelloPeriod, TimeUnit.SECONDS);
63 } 87 }
64 88
65 @Deactivate 89 @Deactivate
66 public void deactivate() { 90 public void deactivate() {
91 +
92 + // Shutdown the periodic hello task.
93 + helloScheduler.shutdown();
94 +
67 log.info("Stopped"); 95 log.info("Stopped");
68 } 96 }
69 97
......
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.IPacket;
20 +import org.onlab.packet.IPv4;
21 +import org.onlab.packet.Ip4Address;
22 +import org.onlab.packet.MacAddress;
23 +import org.onlab.packet.PIM;
24 +
25 +public class PIMPacket {
26 +
27 + // Ethernet header
28 + private Ethernet ethHeader = new Ethernet();
29 +
30 + // IP header
31 + private IPv4 ipHeader = new IPv4();
32 +
33 + // PIM Header
34 + private PIM pimHeader = new PIM();
35 +
36 + // The pim type
37 + private byte pimType;
38 +
39 + // PIM MAC address
40 + private MacAddress pimDestinationMac = MacAddress.valueOf("01:00:5E:00:00:0d");
41 +
42 + /**
43 + * Create a PIM packet for a given PIM type.
44 + *
45 + * The resulting packet will have Ethernet and IPv4 headers with all defaults filled in.
46 + * The final packet will require a PIM header that corresponds to the PIM type set as
47 + * a payload.
48 + *
49 + * Additionally the source MAC and IPv4 address will need to be filled in for the
50 + * packet to be ready to serialize in most cases.
51 + *
52 + * @param type PIM.TYPE_XXXX where XXX is the PIM message type
53 + */
54 + public PIMPacket(byte type) {
55 + pimType = type;
56 + initDefaults();
57 + }
58 +
59 + /**
60 + * Fill in defaults for the Ethernet, IPv4 and PIM headers, then associate each
61 + * of these headers as payload and parent accordingly.
62 + */
63 + public void initDefaults() {
64 + // Prepopulate dst MACAddress and Ethernet Types. The Source MAC needs to be filled in.
65 + ethHeader.setDestinationMACAddress(pimDestinationMac);
66 + ethHeader.setEtherType(Ethernet.TYPE_IPV4);
67 +
68 + // Prepopulate the IP Type and Dest address. The Source IP address needs to be filled in.
69 + ipHeader.setDestinationAddress(PIM.PIM_ADDRESS.getIp4Address().toInt());
70 + ipHeader.setTtl((byte) 1);
71 + ipHeader.setProtocol(IPv4.PROTOCOL_PIM);
72 +
73 + // Establish the order between Ethernet and IP headers
74 + ethHeader.setPayload(ipHeader);
75 + ipHeader.setParent(ethHeader);
76 +
77 + // Prepopulate the PIM packet
78 + pimHeader.setPIMType(pimType);
79 +
80 + // Establish the order between IP and PIM headers
81 + ipHeader.setPayload(pimHeader);
82 + pimHeader.setParent(ipHeader);
83 + }
84 +
85 + /**
86 + * Set the source MAC address.
87 + *
88 + * @param src source MAC address
89 + */
90 + public void setSrcMacAddr(MacAddress src) {
91 + ethHeader.setSourceMACAddress(src);
92 + }
93 +
94 + /**
95 + * Set the source IPv4 address.
96 + *
97 + * @param ipSrcAddress the source IPv4 address
98 + */
99 + public void setSrcIpAddress(Ip4Address ipSrcAddress) {
100 + ipHeader.setSourceAddress(ipSrcAddress.toInt());
101 + }
102 +
103 + /**
104 + * Set the PIM payload.
105 + *
106 + * @param payload the PIM payload
107 + */
108 + public void setPIMPayload(IPacket payload) {
109 + pimHeader.setPayload(payload);
110 + payload.setParent(pimHeader);
111 + }
112 +
113 + /**
114 + * Get the ethernet header.
115 + *
116 + * @return the Ethernet header
117 + */
118 + public Ethernet getEthernet() {
119 + return ethHeader;
120 + }
121 +
122 + /**
123 + * Get the IPv4 header.
124 + *
125 + * @return the IPv4 header
126 + */
127 + public IPv4 getIpv4() {
128 + return ipHeader;
129 + }
130 +}