Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 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.dhcprelay; |
| 17 | |
| 18 | import java.nio.ByteBuffer; |
| 19 | import java.util.Collection; |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 20 | import java.util.Collections; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 21 | import java.util.Dictionary; |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 22 | import java.util.List; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 23 | import java.util.Objects; |
| 24 | import java.util.Optional; |
| 25 | import java.util.Set; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 26 | import java.util.stream.Collectors; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 27 | import java.util.stream.Stream; |
| 28 | |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 29 | import com.google.common.collect.ImmutableList; |
Ruchi Sahota | 196a9ca | 2019-03-01 16:56:07 +0000 | [diff] [blame] | 30 | import com.google.common.collect.ImmutableSet; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 31 | import com.google.common.collect.Streams; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 32 | import org.apache.felix.scr.annotations.Activate; |
| 33 | import org.apache.felix.scr.annotations.Component; |
| 34 | import org.apache.felix.scr.annotations.Deactivate; |
| 35 | import org.apache.felix.scr.annotations.Modified; |
| 36 | import org.apache.felix.scr.annotations.Property; |
| 37 | import org.apache.felix.scr.annotations.Reference; |
| 38 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 39 | import org.apache.felix.scr.annotations.Service; |
| 40 | import org.onlab.packet.ARP; |
| 41 | import org.onlab.packet.DHCP; |
| 42 | import org.onlab.packet.DHCP6; |
| 43 | import org.onlab.packet.IPacket; |
| 44 | import org.onlab.packet.IPv6; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 45 | import org.onlab.packet.Ethernet; |
| 46 | import org.onlab.packet.IPv4; |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 47 | import org.onlab.packet.Ip4Address; |
Anjali K K | f3963f1 | 2019-04-16 19:27:30 +0000 | [diff] [blame] | 48 | import org.onlab.packet.Ip6Address; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 49 | import org.onlab.packet.MacAddress; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 50 | import org.onlab.packet.IpPrefix; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 51 | import org.onlab.packet.UDP; |
| 52 | import org.onlab.packet.VlanId; |
Harshada Chaundkar | 669bdf6 | 2019-05-28 21:12:05 +0000 | [diff] [blame^] | 53 | |
Anjali K K | f3963f1 | 2019-04-16 19:27:30 +0000 | [diff] [blame] | 54 | import org.onlab.packet.ndp.NeighborSolicitation; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 55 | import org.onlab.util.Tools; |
| 56 | import org.onosproject.cfg.ComponentConfigService; |
| 57 | import org.onosproject.core.ApplicationId; |
| 58 | import org.onosproject.core.CoreService; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 59 | import org.onosproject.dhcprelay.api.DhcpHandler; |
| 60 | import org.onosproject.dhcprelay.api.DhcpRelayService; |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 61 | import org.onosproject.dhcprelay.api.DhcpServerInfo; |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 62 | import org.onosproject.dhcprelay.config.DefaultDhcpRelayConfig; |
Taras Lemkin | 371ac5e | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 63 | import org.onosproject.dhcprelay.config.DhcpServerConfig; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 64 | import org.onosproject.dhcprelay.config.EnableDhcpFpmConfig; |
Anjali K K | f3963f1 | 2019-04-16 19:27:30 +0000 | [diff] [blame] | 65 | import org.onosproject.dhcprelay.config.HostAutoRelearnConfig; |
Taras Lemkin | 371ac5e | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 66 | import org.onosproject.dhcprelay.config.IndirectDhcpRelayConfig; |
| 67 | import org.onosproject.dhcprelay.config.IgnoreDhcpConfig; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 68 | import org.onosproject.dhcprelay.store.DhcpRecord; |
| 69 | import org.onosproject.dhcprelay.store.DhcpRelayStore; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 70 | import org.onosproject.dhcprelay.store.DhcpFpmPrefixStore; |
Harshada Chaundkar | 669bdf6 | 2019-05-28 21:12:05 +0000 | [diff] [blame^] | 71 | import org.onosproject.mastership.MastershipService; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 72 | import org.onosproject.routing.fpm.api.FpmRecord; |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 73 | import org.onosproject.net.Device; |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 74 | import org.onosproject.net.Host; |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 75 | import org.onosproject.net.config.Config; |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 76 | import org.onosproject.net.device.DeviceEvent; |
| 77 | import org.onosproject.net.device.DeviceListener; |
| 78 | import org.onosproject.net.device.DeviceService; |
Ray Milkey | facf286 | 2017-08-03 11:58:29 -0700 | [diff] [blame] | 79 | import org.onosproject.net.intf.Interface; |
| 80 | import org.onosproject.net.intf.InterfaceService; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 81 | import org.onosproject.net.ConnectPoint; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 82 | import org.onosproject.net.HostId; |
Anjali K K | f3963f1 | 2019-04-16 19:27:30 +0000 | [diff] [blame] | 83 | import org.onosproject.net.HostLocation; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 84 | import org.onosproject.net.config.ConfigFactory; |
| 85 | import org.onosproject.net.config.NetworkConfigEvent; |
| 86 | import org.onosproject.net.config.NetworkConfigListener; |
| 87 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 88 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 89 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 90 | import org.onosproject.net.flow.TrafficSelector; |
| 91 | import org.onosproject.net.flow.TrafficTreatment; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 92 | import org.onosproject.net.host.HostService; |
Anjali K K | f3963f1 | 2019-04-16 19:27:30 +0000 | [diff] [blame] | 93 | import org.onosproject.net.Port; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 94 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 95 | import org.onosproject.net.packet.OutboundPacket; |
| 96 | import org.onosproject.net.packet.PacketContext; |
| 97 | import org.onosproject.net.packet.PacketPriority; |
| 98 | import org.onosproject.net.packet.PacketProcessor; |
| 99 | import org.onosproject.net.packet.PacketService; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 100 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 101 | import org.osgi.service.component.ComponentContext; |
| 102 | import org.slf4j.Logger; |
| 103 | import org.slf4j.LoggerFactory; |
Kalhee Kim | e5bb709 | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 104 | import java.util.concurrent.Executors; |
Ruchi Sahota | 196a9ca | 2019-03-01 16:56:07 +0000 | [diff] [blame] | 105 | import java.util.concurrent.ExecutorService; |
Kalhee Kim | e5bb709 | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 106 | import java.util.concurrent.ScheduledExecutorService; |
| 107 | import java.util.concurrent.TimeUnit; |
Anjali K K | f3963f1 | 2019-04-16 19:27:30 +0000 | [diff] [blame] | 108 | import java.util.concurrent.CopyOnWriteArraySet; |
| 109 | |
Kalhee Kim | e5bb709 | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 110 | import static org.onlab.util.Tools.groupedThreads; |
Ruchi Sahota | 196a9ca | 2019-03-01 16:56:07 +0000 | [diff] [blame] | 111 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 112 | import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY; |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 113 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 114 | /** |
| 115 | * DHCP Relay Agent Application Component. |
| 116 | */ |
| 117 | @Component(immediate = true) |
| 118 | @Service |
| 119 | public class DhcpRelayManager implements DhcpRelayService { |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 120 | public static final String DHCP_RELAY_APP = "org.onosproject.dhcprelay"; |
Charles Chan | c1daa8f | 2019-01-23 15:03:17 -0800 | [diff] [blame] | 121 | public static final String ROUTE_STORE_IMPL = "org.onosproject.routeservice.store.RouteStoreImpl"; |
Charles Chan | c1daa8f | 2019-01-23 15:03:17 -0800 | [diff] [blame] | 122 | private static final int DEFAULT_POOL_SIZE = 32; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 123 | |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 124 | private static final TrafficSelector ARP_SELECTOR = DefaultTrafficSelector.builder() |
| 125 | .matchEthType(Ethernet.TYPE_ARP) |
| 126 | .build(); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 127 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 128 | private final InternalConfigListener cfgListener = new InternalConfigListener(); |
Anjali K K | f3963f1 | 2019-04-16 19:27:30 +0000 | [diff] [blame] | 129 | protected CopyOnWriteArraySet hostAutoRelearnEnabledDevices = new CopyOnWriteArraySet(); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 130 | |
| 131 | private final Set<ConfigFactory> factories = ImmutableSet.of( |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 132 | new ConfigFactory<ApplicationId, DefaultDhcpRelayConfig>(APP_SUBJECT_FACTORY, |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 133 | DefaultDhcpRelayConfig.class, |
| 134 | DefaultDhcpRelayConfig.KEY, |
| 135 | true) { |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 136 | @Override |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 137 | public DefaultDhcpRelayConfig createConfig() { |
| 138 | return new DefaultDhcpRelayConfig(); |
| 139 | } |
| 140 | }, |
| 141 | new ConfigFactory<ApplicationId, IndirectDhcpRelayConfig>(APP_SUBJECT_FACTORY, |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 142 | IndirectDhcpRelayConfig.class, |
| 143 | IndirectDhcpRelayConfig.KEY, |
| 144 | true) { |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 145 | @Override |
| 146 | public IndirectDhcpRelayConfig createConfig() { |
| 147 | return new IndirectDhcpRelayConfig(); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 148 | } |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 149 | }, |
| 150 | new ConfigFactory<ApplicationId, IgnoreDhcpConfig>(APP_SUBJECT_FACTORY, |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 151 | IgnoreDhcpConfig.class, |
| 152 | IgnoreDhcpConfig.KEY, |
| 153 | true) { |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 154 | @Override |
| 155 | public IgnoreDhcpConfig createConfig() { |
| 156 | return new IgnoreDhcpConfig(); |
| 157 | } |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 158 | }, |
| 159 | new ConfigFactory<ApplicationId, EnableDhcpFpmConfig>(APP_SUBJECT_FACTORY, |
| 160 | EnableDhcpFpmConfig.class, |
| 161 | EnableDhcpFpmConfig.KEY, |
| 162 | false) { |
| 163 | @Override |
| 164 | public EnableDhcpFpmConfig createConfig() { |
| 165 | return new EnableDhcpFpmConfig(); |
| 166 | } |
Anjali K K | f3963f1 | 2019-04-16 19:27:30 +0000 | [diff] [blame] | 167 | }, |
| 168 | new ConfigFactory<ApplicationId, HostAutoRelearnConfig>(APP_SUBJECT_FACTORY, |
| 169 | HostAutoRelearnConfig.class, |
| 170 | HostAutoRelearnConfig.KEY, |
| 171 | true) { |
| 172 | @Override |
| 173 | public HostAutoRelearnConfig createConfig() { |
| 174 | return new HostAutoRelearnConfig(); |
| 175 | } |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 176 | } |
| 177 | ); |
| 178 | |
Kalhee Kim | e5bb709 | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 179 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 180 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 181 | protected NetworkConfigRegistry cfgService; |
| 182 | |
| 183 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Harshada Chaundkar | 669bdf6 | 2019-05-28 21:12:05 +0000 | [diff] [blame^] | 184 | protected MastershipService mastershipService; |
| 185 | |
| 186 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 187 | protected CoreService coreService; |
| 188 | |
| 189 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 190 | protected PacketService packetService; |
| 191 | |
| 192 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 193 | protected HostService hostService; |
| 194 | |
| 195 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 196 | protected InterfaceService interfaceService; |
| 197 | |
| 198 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 199 | protected DhcpRelayStore dhcpRelayStore; |
| 200 | |
| 201 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 202 | protected ComponentConfigService compCfgService; |
| 203 | |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 204 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 205 | protected DeviceService deviceService; |
| 206 | |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 207 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 208 | protected DhcpFpmPrefixStore dhcpFpmPrefixStore; |
| 209 | |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 210 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY, |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 211 | target = "(version=4)") |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 212 | protected DhcpHandler v4Handler; |
| 213 | |
| 214 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY, |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 215 | target = "(version=6)") |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 216 | protected DhcpHandler v6Handler; |
| 217 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 218 | @Property(name = "arpEnabled", boolValue = true, |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 219 | label = "Enable Address resolution protocol") |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 220 | protected boolean arpEnabled = true; |
| 221 | |
Kalhee Kim | e5bb709 | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 222 | @Property(name = "dhcpPollInterval", intValue = 24 * 3600, |
| 223 | label = "dhcp relay poll interval") |
| 224 | protected int dhcpPollInterval = 24 * 3600; |
| 225 | |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 226 | @Property(name = "dhcpFpmEnabled", boolValue = false, |
| 227 | label = "Enable DhcpRelay Fpm") |
| 228 | protected boolean dhcpFpmEnabled = false; |
| 229 | |
Anjali K K | f3963f1 | 2019-04-16 19:27:30 +0000 | [diff] [blame] | 230 | @Property(name = "dhcpHostRelearnProbeInterval", intValue = 500, |
| 231 | label = "dhcp host relearn probe interval in millis") |
| 232 | protected int dhcpHostRelearnProbeInterval = 500; |
| 233 | |
| 234 | @Property(name = "dhcpHostRelearnProbeCount", intValue = 3, |
| 235 | label = "dhcp host relearn probe count") |
| 236 | protected int dhcpHostRelearnProbeCount = 3; |
| 237 | |
Kalhee Kim | e5bb709 | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 238 | private ScheduledExecutorService timerExecutor; |
Anjali K K | f3963f1 | 2019-04-16 19:27:30 +0000 | [diff] [blame] | 239 | private ScheduledExecutorService executorService = null; |
Ruchi Sahota | 196a9ca | 2019-03-01 16:56:07 +0000 | [diff] [blame] | 240 | protected ExecutorService devEventExecutor; |
Charles Chan | c1daa8f | 2019-01-23 15:03:17 -0800 | [diff] [blame] | 241 | private ExecutorService packetExecutor; |
Kalhee Kim | e5bb709 | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 242 | |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 243 | protected DeviceListener deviceListener = new InternalDeviceListener(); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 244 | private DhcpRelayPacketProcessor dhcpRelayPacketProcessor = new DhcpRelayPacketProcessor(); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 245 | private ApplicationId appId; |
| 246 | |
Anjali K K | f3963f1 | 2019-04-16 19:27:30 +0000 | [diff] [blame] | 247 | private static final int POOL_SIZE = 10; |
| 248 | private static final int HOST_PROBE_INIT_DELAY = 500; |
| 249 | |
Kalhee Kim | e5bb709 | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 250 | /** |
| 251 | * One second timer. |
| 252 | */ |
| 253 | class Dhcp6Timer implements Runnable { |
| 254 | @Override |
| 255 | public void run() { |
| 256 | v6Handler.timeTick(); |
| 257 | } |
| 258 | }; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 259 | |
Anjali K K | f3963f1 | 2019-04-16 19:27:30 +0000 | [diff] [blame] | 260 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 261 | @Activate |
| 262 | protected void activate(ComponentContext context) { |
| 263 | //start the dhcp relay agent |
| 264 | appId = coreService.registerApplication(DHCP_RELAY_APP); |
| 265 | |
| 266 | cfgService.addListener(cfgListener); |
| 267 | factories.forEach(cfgService::registerConfigFactory); |
| 268 | //update the dhcp server configuration. |
| 269 | updateConfig(); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 270 | |
| 271 | //add the packet processor |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 272 | packetService.addProcessor(dhcpRelayPacketProcessor, PacketProcessor.director(0)); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 273 | |
Kalhee Kim | e5bb709 | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 274 | timerExecutor = Executors.newScheduledThreadPool(1, |
Charles Chan | c1daa8f | 2019-01-23 15:03:17 -0800 | [diff] [blame] | 275 | groupedThreads("onos/dhcprelay", "config-reloader-%d", log)); |
| 276 | timerExecutor.scheduleAtFixedRate(new Dhcp6Timer(), 0, dhcpPollInterval, TimeUnit.SECONDS); |
| 277 | packetExecutor = Executors.newFixedThreadPool(DEFAULT_POOL_SIZE, |
| 278 | groupedThreads("onos/dhcprelay", "packet-%d", log)); |
Kalhee Kim | e5bb709 | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 279 | |
Ruchi Sahota | 196a9ca | 2019-03-01 16:56:07 +0000 | [diff] [blame] | 280 | devEventExecutor = newSingleThreadScheduledExecutor( |
| 281 | groupedThreads("onos/dhcprelay-dev-events", "events-%d", log)); |
| 282 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 283 | modified(context); |
| 284 | |
Yi Tseng | 06799d6 | 2017-09-01 16:02:56 -0700 | [diff] [blame] | 285 | // Enable distribute route store |
| 286 | compCfgService.preSetProperty(ROUTE_STORE_IMPL, |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 287 | "distributed", Boolean.TRUE.toString()); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 288 | compCfgService.registerProperties(getClass()); |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 289 | |
Anjali K K | f3963f1 | 2019-04-16 19:27:30 +0000 | [diff] [blame] | 290 | executorService = Executors.newScheduledThreadPool(POOL_SIZE); |
| 291 | |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 292 | deviceService.addListener(deviceListener); |
Kalhee Kim | e5bb709 | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 293 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 294 | log.info("DHCP-RELAY Started"); |
| 295 | } |
| 296 | |
| 297 | @Deactivate |
| 298 | protected void deactivate() { |
| 299 | cfgService.removeListener(cfgListener); |
| 300 | factories.forEach(cfgService::unregisterConfigFactory); |
| 301 | packetService.removeProcessor(dhcpRelayPacketProcessor); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 302 | cancelArpPackets(); |
Yi Tseng | dfa8ee2 | 2017-08-07 12:45:01 -0700 | [diff] [blame] | 303 | compCfgService.unregisterProperties(getClass(), false); |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 304 | deviceService.removeListener(deviceListener); |
Kalhee Kim | e5bb709 | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 305 | timerExecutor.shutdown(); |
Ruchi Sahota | 196a9ca | 2019-03-01 16:56:07 +0000 | [diff] [blame] | 306 | devEventExecutor.shutdownNow(); |
| 307 | devEventExecutor = null; |
Charles Chan | c1daa8f | 2019-01-23 15:03:17 -0800 | [diff] [blame] | 308 | packetExecutor.shutdown(); |
| 309 | timerExecutor = null; |
| 310 | packetExecutor = null; |
Anjali K K | f3963f1 | 2019-04-16 19:27:30 +0000 | [diff] [blame] | 311 | executorService.shutdown(); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 312 | log.info("DHCP-RELAY Stopped"); |
| 313 | } |
| 314 | |
| 315 | @Modified |
| 316 | protected void modified(ComponentContext context) { |
| 317 | Dictionary<?, ?> properties = context.getProperties(); |
| 318 | Boolean flag; |
| 319 | |
| 320 | flag = Tools.isPropertyEnabled(properties, "arpEnabled"); |
| 321 | if (flag != null) { |
| 322 | arpEnabled = flag; |
| 323 | log.info("Address resolution protocol is {}", |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 324 | arpEnabled ? "enabled" : "disabled"); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | if (arpEnabled) { |
| 328 | requestArpPackets(); |
| 329 | } else { |
| 330 | cancelArpPackets(); |
| 331 | } |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 332 | |
Kalhee Kim | e5bb709 | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 333 | int intervalVal = Tools.getIntegerProperty(properties, "dhcpPollInterval"); |
| 334 | log.info("DhcpRelay poll interval new {} old {}", intervalVal, dhcpPollInterval); |
| 335 | if (intervalVal != dhcpPollInterval) { |
| 336 | timerExecutor.shutdown(); |
| 337 | dhcpPollInterval = intervalVal; |
| 338 | timerExecutor = Executors.newScheduledThreadPool(1, |
| 339 | groupedThreads("dhcpRelay", |
| 340 | "config-reloader-%d", log)); |
| 341 | timerExecutor.scheduleAtFixedRate(new Dhcp6Timer(), |
| 342 | 0, |
| 343 | dhcpPollInterval > 1 ? dhcpPollInterval : 1, |
| 344 | TimeUnit.SECONDS); |
| 345 | v6Handler.setDhcp6PollInterval(dhcpPollInterval); |
| 346 | } |
| 347 | |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 348 | flag = Tools.isPropertyEnabled(properties, "dhcpFpmEnabled"); |
| 349 | if (flag != null) { |
| 350 | boolean oldValue = dhcpFpmEnabled; |
| 351 | dhcpFpmEnabled = flag; |
| 352 | log.info("DhcpRelay FPM is {}", |
| 353 | dhcpFpmEnabled ? "enabled" : "disabled"); |
| 354 | |
| 355 | if (dhcpFpmEnabled && !oldValue) { |
| 356 | log.info("Dhcp Fpm is enabled."); |
| 357 | processDhcpFpmRoutes(true); |
| 358 | } |
| 359 | if (!dhcpFpmEnabled && oldValue) { |
| 360 | log.info("Dhcp Fpm is disabled."); |
| 361 | processDhcpFpmRoutes(false); |
| 362 | } |
| 363 | v6Handler.setDhcpFpmEnabled(dhcpFpmEnabled); |
| 364 | } |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 367 | private static List<TrafficSelector> buildClientDhcpSelectors() { |
| 368 | return Streams.concat(Dhcp4HandlerImpl.DHCP_SELECTORS.stream(), |
| 369 | Dhcp6HandlerImpl.DHCP_SELECTORS.stream()) |
| 370 | .collect(Collectors.toList()); |
| 371 | } |
| 372 | |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 373 | /** |
| 374 | * Updates DHCP relay app configuration. |
| 375 | */ |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 376 | private void updateConfig() { |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 377 | DefaultDhcpRelayConfig defaultConfig = |
| 378 | cfgService.getConfig(appId, DefaultDhcpRelayConfig.class); |
| 379 | IndirectDhcpRelayConfig indirectConfig = |
| 380 | cfgService.getConfig(appId, IndirectDhcpRelayConfig.class); |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 381 | IgnoreDhcpConfig ignoreDhcpConfig = |
| 382 | cfgService.getConfig(appId, IgnoreDhcpConfig.class); |
Anjali K K | f3963f1 | 2019-04-16 19:27:30 +0000 | [diff] [blame] | 383 | HostAutoRelearnConfig hostAutoRelearnConfig = |
| 384 | cfgService.getConfig(appId, HostAutoRelearnConfig.class); |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 385 | |
| 386 | if (defaultConfig != null) { |
| 387 | updateConfig(defaultConfig); |
| 388 | } |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 389 | if (indirectConfig != null) { |
| 390 | updateConfig(indirectConfig); |
| 391 | } |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 392 | if (ignoreDhcpConfig != null) { |
| 393 | updateConfig(ignoreDhcpConfig); |
| 394 | } |
Anjali K K | f3963f1 | 2019-04-16 19:27:30 +0000 | [diff] [blame] | 395 | if (hostAutoRelearnConfig != null) { |
| 396 | updateConfig(hostAutoRelearnConfig); |
| 397 | } |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | /** |
| 401 | * Updates DHCP relay app configuration with given configuration. |
| 402 | * |
| 403 | * @param config the configuration ot update |
| 404 | */ |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 405 | protected void updateConfig(Config config) { |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 406 | if (config instanceof IndirectDhcpRelayConfig) { |
| 407 | IndirectDhcpRelayConfig indirectConfig = (IndirectDhcpRelayConfig) config; |
| 408 | v4Handler.setIndirectDhcpServerConfigs(indirectConfig.dhcpServerConfigs()); |
| 409 | v6Handler.setIndirectDhcpServerConfigs(indirectConfig.dhcpServerConfigs()); |
Yi Tseng | 3df7f9d | 2017-08-17 13:08:31 -0700 | [diff] [blame] | 410 | } else if (config instanceof DefaultDhcpRelayConfig) { |
| 411 | DefaultDhcpRelayConfig defaultConfig = (DefaultDhcpRelayConfig) config; |
| 412 | v4Handler.setDefaultDhcpServerConfigs(defaultConfig.dhcpServerConfigs()); |
| 413 | v6Handler.setDefaultDhcpServerConfigs(defaultConfig.dhcpServerConfigs()); |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 414 | } |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 415 | if (config instanceof IgnoreDhcpConfig) { |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 416 | v4Handler.updateIgnoreVlanConfig((IgnoreDhcpConfig) config); |
| 417 | v6Handler.updateIgnoreVlanConfig((IgnoreDhcpConfig) config); |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 418 | } |
Anjali K K | f3963f1 | 2019-04-16 19:27:30 +0000 | [diff] [blame] | 419 | if (config instanceof HostAutoRelearnConfig) { |
| 420 | setHostAutoRelearnConfig((HostAutoRelearnConfig) config); |
| 421 | } |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | protected void removeConfig(Config config) { |
| 425 | if (config instanceof IndirectDhcpRelayConfig) { |
| 426 | v4Handler.setIndirectDhcpServerConfigs(Collections.emptyList()); |
| 427 | v6Handler.setIndirectDhcpServerConfigs(Collections.emptyList()); |
| 428 | } else if (config instanceof DefaultDhcpRelayConfig) { |
| 429 | v4Handler.setDefaultDhcpServerConfigs(Collections.emptyList()); |
| 430 | v6Handler.setDefaultDhcpServerConfigs(Collections.emptyList()); |
| 431 | } |
| 432 | if (config instanceof IgnoreDhcpConfig) { |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 433 | v4Handler.updateIgnoreVlanConfig(null); |
| 434 | v6Handler.updateIgnoreVlanConfig(null); |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 438 | private void processDhcpFpmRoutes(Boolean add) { |
| 439 | // needs to restore/remove fpm |
| 440 | } |
| 441 | |
| 442 | public boolean isDhcpFpmEnabled() { |
| 443 | return dhcpFpmEnabled; |
| 444 | } |
| 445 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 446 | /** |
| 447 | * Request ARP packet in via PacketService. |
| 448 | */ |
| 449 | private void requestArpPackets() { |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 450 | packetService.requestPackets(ARP_SELECTOR, PacketPriority.CONTROL, appId); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | /** |
| 454 | * Cancel requested ARP packets in via packet service. |
| 455 | */ |
| 456 | private void cancelArpPackets() { |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 457 | packetService.cancelPackets(ARP_SELECTOR, PacketPriority.CONTROL, appId); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | @Override |
| 461 | public Optional<DhcpRecord> getDhcpRecord(HostId hostId) { |
| 462 | return dhcpRelayStore.getDhcpRecord(hostId); |
| 463 | } |
| 464 | |
| 465 | @Override |
| 466 | public Collection<DhcpRecord> getDhcpRecords() { |
| 467 | return dhcpRelayStore.getDhcpRecords(); |
| 468 | } |
Anjali K K | f3963f1 | 2019-04-16 19:27:30 +0000 | [diff] [blame] | 469 | |
Kalhee Kim | e5bb709 | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 470 | @Override |
| 471 | public void updateDhcpRecord(HostId hostId, DhcpRecord dhcpRecord) { |
| 472 | dhcpRelayStore.updateDhcpRecord(hostId, dhcpRecord); |
| 473 | } |
Anjali K K | f3963f1 | 2019-04-16 19:27:30 +0000 | [diff] [blame] | 474 | |
Yi Tseng | 13a41a1 | 2017-07-26 13:45:01 -0700 | [diff] [blame] | 475 | @Override |
| 476 | public Optional<MacAddress> getDhcpServerMacAddress() { |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 477 | // TODO: depreated it |
| 478 | DefaultDhcpRelayConfig config = cfgService.getConfig(appId, DefaultDhcpRelayConfig.class); |
| 479 | DhcpServerConfig serverConfig = config.dhcpServerConfigs().get(0); |
| 480 | Ip4Address serverip = serverConfig.getDhcpServerIp4().get(); |
| 481 | return hostService.getHostsByIp(serverip) |
| 482 | .stream() |
| 483 | .map(Host::mac) |
| 484 | .findFirst(); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 487 | @Override |
| 488 | public List<DhcpServerInfo> getDefaultDhcpServerInfoList() { |
| 489 | return ImmutableList.<DhcpServerInfo>builder() |
| 490 | .addAll(v4Handler.getDefaultDhcpServerInfoList()) |
| 491 | .addAll(v6Handler.getDefaultDhcpServerInfoList()) |
| 492 | .build(); |
| 493 | } |
| 494 | |
| 495 | @Override |
| 496 | public List<DhcpServerInfo> getIndirectDhcpServerInfoList() { |
| 497 | return ImmutableList.<DhcpServerInfo>builder() |
| 498 | .addAll(v4Handler.getIndirectDhcpServerInfoList()) |
| 499 | .addAll(v6Handler.getIndirectDhcpServerInfoList()) |
| 500 | .build(); |
| 501 | } |
| 502 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 503 | /** |
| 504 | * Gets DHCP data from a packet. |
| 505 | * |
| 506 | * @param packet the packet |
| 507 | * @return the DHCP data; empty if it is not a DHCP packet |
| 508 | */ |
| 509 | private Optional<DHCP> findDhcp(Ethernet packet) { |
| 510 | return Stream.of(packet) |
| 511 | .filter(Objects::nonNull) |
| 512 | .map(Ethernet::getPayload) |
| 513 | .filter(p -> p instanceof IPv4) |
| 514 | .map(IPacket::getPayload) |
| 515 | .filter(Objects::nonNull) |
| 516 | .filter(p -> p instanceof UDP) |
| 517 | .map(IPacket::getPayload) |
| 518 | .filter(Objects::nonNull) |
| 519 | .filter(p -> p instanceof DHCP) |
| 520 | .map(p -> (DHCP) p) |
| 521 | .findFirst(); |
| 522 | } |
| 523 | |
| 524 | /** |
| 525 | * Gets DHCPv6 data from a packet. |
| 526 | * |
| 527 | * @param packet the packet |
| 528 | * @return the DHCPv6 data; empty if it is not a DHCPv6 packet |
| 529 | */ |
| 530 | private Optional<DHCP6> findDhcp6(Ethernet packet) { |
| 531 | return Stream.of(packet) |
| 532 | .filter(Objects::nonNull) |
| 533 | .map(Ethernet::getPayload) |
| 534 | .filter(p -> p instanceof IPv6) |
| 535 | .map(IPacket::getPayload) |
| 536 | .filter(Objects::nonNull) |
| 537 | .filter(p -> p instanceof UDP) |
| 538 | .map(IPacket::getPayload) |
| 539 | .filter(Objects::nonNull) |
| 540 | .filter(p -> p instanceof DHCP6) |
| 541 | .map(p -> (DHCP6) p) |
| 542 | .findFirst(); |
| 543 | } |
| 544 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 545 | |
| 546 | private class DhcpRelayPacketProcessor implements PacketProcessor { |
Anjali K K | f3963f1 | 2019-04-16 19:27:30 +0000 | [diff] [blame] | 547 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 548 | @Override |
| 549 | public void process(PacketContext context) { |
Charles Chan | c1daa8f | 2019-01-23 15:03:17 -0800 | [diff] [blame] | 550 | packetExecutor.execute(() -> processInternal(context)); |
| 551 | } |
| 552 | |
| 553 | private void processInternal(PacketContext context) { |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 554 | // process the packet and get the payload |
| 555 | Ethernet packet = context.inPacket().parsed(); |
| 556 | if (packet == null) { |
| 557 | return; |
| 558 | } |
| 559 | |
| 560 | findDhcp(packet).ifPresent(dhcpPayload -> { |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 561 | v4Handler.processDhcpPacket(context, dhcpPayload); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 562 | }); |
| 563 | |
| 564 | findDhcp6(packet).ifPresent(dhcp6Payload -> { |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 565 | v6Handler.processDhcpPacket(context, dhcp6Payload); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 566 | }); |
| 567 | |
| 568 | if (packet.getEtherType() == Ethernet.TYPE_ARP && arpEnabled) { |
| 569 | ARP arpPacket = (ARP) packet.getPayload(); |
| 570 | VlanId vlanId = VlanId.vlanId(packet.getVlanID()); |
| 571 | Set<Interface> interfaces = interfaceService. |
| 572 | getInterfacesByPort(context.inPacket().receivedFrom()); |
| 573 | //ignore the packets if dhcp server interface is not configured on onos. |
| 574 | if (interfaces.isEmpty()) { |
| 575 | log.warn("server virtual interface not configured"); |
| 576 | return; |
| 577 | } |
| 578 | if ((arpPacket.getOpCode() != ARP.OP_REQUEST)) { |
| 579 | // handle request only |
| 580 | return; |
| 581 | } |
| 582 | MacAddress interfaceMac = interfaces.stream() |
| 583 | .filter(iface -> iface.vlan().equals(vlanId)) |
| 584 | .map(Interface::mac) |
| 585 | .filter(mac -> !mac.equals(MacAddress.NONE)) |
| 586 | .findFirst() |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 587 | .orElse(MacAddress.ONOS); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 588 | if (interfaceMac == null) { |
| 589 | // can't find interface mac address |
| 590 | return; |
| 591 | } |
| 592 | processArpPacket(context, packet, interfaceMac); |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | /** |
| 597 | * Processes the ARP Payload and initiates a reply to the client. |
| 598 | * |
| 599 | * @param context the packet context |
| 600 | * @param packet the ethernet payload |
| 601 | * @param replyMac mac address to be replied |
| 602 | */ |
| 603 | private void processArpPacket(PacketContext context, Ethernet packet, MacAddress replyMac) { |
| 604 | ARP arpPacket = (ARP) packet.getPayload(); |
Ray Milkey | f0c4761 | 2017-09-28 11:29:38 -0700 | [diff] [blame] | 605 | ARP arpReply = arpPacket.duplicate(); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 606 | arpReply.setOpCode(ARP.OP_REPLY); |
| 607 | |
| 608 | arpReply.setTargetProtocolAddress(arpPacket.getSenderProtocolAddress()); |
| 609 | arpReply.setTargetHardwareAddress(arpPacket.getSenderHardwareAddress()); |
| 610 | arpReply.setSenderProtocolAddress(arpPacket.getTargetProtocolAddress()); |
| 611 | arpReply.setSenderHardwareAddress(replyMac.toBytes()); |
| 612 | |
| 613 | // Ethernet Frame. |
| 614 | Ethernet ethReply = new Ethernet(); |
| 615 | ethReply.setSourceMACAddress(replyMac.toBytes()); |
| 616 | ethReply.setDestinationMACAddress(packet.getSourceMAC()); |
| 617 | ethReply.setEtherType(Ethernet.TYPE_ARP); |
| 618 | ethReply.setVlanID(packet.getVlanID()); |
| 619 | ethReply.setPayload(arpReply); |
| 620 | |
| 621 | ConnectPoint targetPort = context.inPacket().receivedFrom(); |
| 622 | TrafficTreatment t = DefaultTrafficTreatment.builder() |
| 623 | .setOutput(targetPort.port()).build(); |
| 624 | OutboundPacket o = new DefaultOutboundPacket( |
| 625 | targetPort.deviceId(), t, ByteBuffer.wrap(ethReply.serialize())); |
| 626 | if (log.isTraceEnabled()) { |
| 627 | log.trace("Relaying ARP packet {} to {}", packet, targetPort); |
| 628 | } |
| 629 | packetService.emit(o); |
| 630 | } |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | /** |
| 634 | * Listener for network config events. |
| 635 | */ |
| 636 | private class InternalConfigListener implements NetworkConfigListener { |
| 637 | @Override |
| 638 | public void event(NetworkConfigEvent event) { |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 639 | switch (event.type()) { |
| 640 | case CONFIG_UPDATED: |
| 641 | case CONFIG_ADDED: |
| 642 | event.config().ifPresent(config -> { |
| 643 | updateConfig(config); |
| 644 | log.info("{} updated", config.getClass().getSimpleName()); |
| 645 | }); |
| 646 | break; |
| 647 | case CONFIG_REMOVED: |
| 648 | event.prevConfig().ifPresent(config -> { |
| 649 | removeConfig(config); |
| 650 | log.info("{} removed", config.getClass().getSimpleName()); |
| 651 | }); |
| 652 | break; |
Charles Chan | 6be77d8 | 2018-04-21 00:44:29 -0700 | [diff] [blame] | 653 | case CONFIG_REGISTERED: |
| 654 | case CONFIG_UNREGISTERED: |
| 655 | break; |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 656 | default: |
| 657 | log.warn("Unsupported event type {}", event.type()); |
| 658 | break; |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 659 | } |
Charles Chan | 285cb77 | 2018-01-03 16:26:32 -0800 | [diff] [blame] | 660 | } |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 661 | |
Charles Chan | 285cb77 | 2018-01-03 16:26:32 -0800 | [diff] [blame] | 662 | @Override |
| 663 | public boolean isRelevant(NetworkConfigEvent event) { |
| 664 | if (event.configClass().equals(DefaultDhcpRelayConfig.class) || |
| 665 | event.configClass().equals(IndirectDhcpRelayConfig.class) || |
Anjali K K | f3963f1 | 2019-04-16 19:27:30 +0000 | [diff] [blame] | 666 | event.configClass().equals(IgnoreDhcpConfig.class) || |
| 667 | event.configClass().equals(HostAutoRelearnConfig.class)) { |
Charles Chan | 285cb77 | 2018-01-03 16:26:32 -0800 | [diff] [blame] | 668 | return true; |
| 669 | } |
| 670 | log.debug("Ignore irrelevant event class {}", event.configClass().getName()); |
| 671 | return false; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 672 | } |
| 673 | } |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 674 | |
| 675 | private class InternalDeviceListener implements DeviceListener { |
| 676 | |
| 677 | @Override |
| 678 | public void event(DeviceEvent event) { |
Ruchi Sahota | 196a9ca | 2019-03-01 16:56:07 +0000 | [diff] [blame] | 679 | if (devEventExecutor != null) { |
Anjali K K | f3963f1 | 2019-04-16 19:27:30 +0000 | [diff] [blame] | 680 | final Device device = event.subject(); |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 681 | switch (event.type()) { |
| 682 | case DEVICE_ADDED: |
Ruchi Sahota | 196a9ca | 2019-03-01 16:56:07 +0000 | [diff] [blame] | 683 | devEventExecutor.execute(this::updateIgnoreVlanConfigs); |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 684 | break; |
| 685 | case DEVICE_AVAILABILITY_CHANGED: |
Ruchi Sahota | 196a9ca | 2019-03-01 16:56:07 +0000 | [diff] [blame] | 686 | devEventExecutor.execute(() -> deviceAvailabilityChanged(device)); |
| 687 | break; |
Anjali K K | f3963f1 | 2019-04-16 19:27:30 +0000 | [diff] [blame] | 688 | case PORT_UPDATED: |
| 689 | Port port = event.port(); |
| 690 | devEventExecutor.execute(() -> portUpdatedEventHandler(device, port)); |
| 691 | break; |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 692 | default: |
| 693 | break; |
| 694 | } |
Ruchi Sahota | 196a9ca | 2019-03-01 16:56:07 +0000 | [diff] [blame] | 695 | } |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 696 | } |
| 697 | |
Anjali K K | f3963f1 | 2019-04-16 19:27:30 +0000 | [diff] [blame] | 698 | private void portUpdatedEventHandler(Device device, Port port) { |
Harshada Chaundkar | 669bdf6 | 2019-05-28 21:12:05 +0000 | [diff] [blame^] | 699 | if (!mastershipService.isLocalMaster(device.id())) { |
| 700 | log.warn("This instance is not the master for the device {}", device.id()); |
| 701 | return; |
| 702 | } |
Anjali K K | f3963f1 | 2019-04-16 19:27:30 +0000 | [diff] [blame] | 703 | if (hostAutoRelearnEnabledDevices.contains(device.id()) && port.isEnabled()) { |
| 704 | ConnectPoint cp = new ConnectPoint(device.id(), port.number()); |
| 705 | HostLocation hostLocation = new HostLocation(cp, 0); |
| 706 | Set<DhcpRecord> records = dhcpRelayStore.getDhcpRecords() |
| 707 | .stream() |
| 708 | .filter(i -> i.directlyConnected()) |
| 709 | .filter(i -> i.locations().contains(hostLocation)) |
| 710 | .collect(Collectors.toSet()); |
| 711 | |
| 712 | for (DhcpRecord i : records) { |
| 713 | //found a dhcprecord matching the connect point of the port event |
Harshada Chaundkar | 669bdf6 | 2019-05-28 21:12:05 +0000 | [diff] [blame^] | 714 | log.debug("portUpdatedEventHandler:DHCP record {}, sending msg on CP {} Mac {} Vlan{} DeviceId {}", |
| 715 | i, cp, i.macAddress(), i.vlanId(), device.id()); |
Anjali K K | f3963f1 | 2019-04-16 19:27:30 +0000 | [diff] [blame] | 716 | if (i.ip4Address().isPresent()) { |
| 717 | log.warn("Sending host relearn probe for v4 not supported for Mac {} Vlan{} ip {}", |
| 718 | i.macAddress(), i.vlanId(), i.ip4Address()); |
| 719 | } else if (i.ip6Address().isPresent()) { |
| 720 | sendHostRelearnProbe(cp, i.macAddress(), i.vlanId(), i.ip6Address()); |
| 721 | } |
| 722 | } |
| 723 | } |
| 724 | } |
| 725 | |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 726 | private void deviceAvailabilityChanged(Device device) { |
| 727 | if (deviceService.isAvailable(device.id())) { |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 728 | updateIgnoreVlanConfigs(); |
Saurav Das | fa9004b | 2017-12-13 16:19:35 -0800 | [diff] [blame] | 729 | } else { |
| 730 | removeIgnoreVlanState(); |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 731 | } |
| 732 | } |
| 733 | |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 734 | private void updateIgnoreVlanConfigs() { |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 735 | IgnoreDhcpConfig config = cfgService.getConfig(appId, IgnoreDhcpConfig.class); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 736 | v4Handler.updateIgnoreVlanConfig(config); |
| 737 | v6Handler.updateIgnoreVlanConfig(config); |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 738 | } |
Saurav Das | fa9004b | 2017-12-13 16:19:35 -0800 | [diff] [blame] | 739 | |
| 740 | private void removeIgnoreVlanState() { |
| 741 | IgnoreDhcpConfig config = cfgService.getConfig(appId, IgnoreDhcpConfig.class); |
| 742 | v4Handler.removeIgnoreVlanState(config); |
| 743 | v6Handler.removeIgnoreVlanState(config); |
| 744 | } |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 745 | } |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 746 | |
Anjali K K | f3963f1 | 2019-04-16 19:27:30 +0000 | [diff] [blame] | 747 | private void setHostAutoRelearnConfig(HostAutoRelearnConfig config) { |
| 748 | hostAutoRelearnEnabledDevices.clear(); |
| 749 | if (config == null) { |
| 750 | return; |
| 751 | } |
| 752 | hostAutoRelearnEnabledDevices.addAll(config.hostAutoRelearnEnabledDevices()); |
| 753 | } |
| 754 | |
| 755 | // Packet transmission class. |
| 756 | private class PktTransmitter implements Runnable { |
| 757 | |
| 758 | MacAddress mac; |
| 759 | VlanId vlanId; |
| 760 | Ip6Address ipv6Address; |
| 761 | ConnectPoint connectPoint; |
Anjali K K | f3963f1 | 2019-04-16 19:27:30 +0000 | [diff] [blame] | 762 | PktTransmitter(MacAddress mac, VlanId vlanId, Ip6Address ipv6Address, ConnectPoint connectPoint) { |
| 763 | this.mac = mac; |
| 764 | this.vlanId = vlanId; |
| 765 | this.ipv6Address = ipv6Address; |
| 766 | this.connectPoint = connectPoint; |
| 767 | } |
| 768 | |
| 769 | @Override |
| 770 | public void run() { |
| 771 | log.debug("Host Relearn probe packet transmission activated for Mac {} Vlan {} Ip {} ConnectPt {}", |
| 772 | mac, vlanId, ipv6Address, connectPoint); |
| 773 | if (mac == null || vlanId == null || ipv6Address == null || connectPoint == null) { |
| 774 | return; |
| 775 | } |
| 776 | |
Harshada Chaundkar | 669bdf6 | 2019-05-28 21:12:05 +0000 | [diff] [blame^] | 777 | Interface senderInterface = interfaceService.getInterfacesByPort(connectPoint) |
| 778 | .stream().filter(iface -> Dhcp6HandlerUtil.interfaceContainsVlan(iface, vlanId)) |
| 779 | .findFirst().orElse(null); |
| 780 | if (senderInterface == null) { |
| 781 | log.warn("Cannot get sender interface for from packet, abort... vlan {}", vlanId.toString()); |
| 782 | } |
| 783 | MacAddress senderMacAddress = senderInterface.mac(); |
| 784 | byte[] senderIpAddress = IPv6.getLinkLocalAddress(senderMacAddress.toBytes()); |
| 785 | byte[] destIp = IPv6.getSolicitNodeAddress(ipv6Address.toOctets()); |
| 786 | |
Anjali K K | f3963f1 | 2019-04-16 19:27:30 +0000 | [diff] [blame] | 787 | Ethernet ethernet = NeighborSolicitation.buildNdpSolicit( |
| 788 | this.ipv6Address, |
| 789 | Ip6Address.valueOf(senderIpAddress), |
Harshada Chaundkar | 669bdf6 | 2019-05-28 21:12:05 +0000 | [diff] [blame^] | 790 | Ip6Address.valueOf(destIp), //destip |
| 791 | senderMacAddress, |
Anjali K K | f3963f1 | 2019-04-16 19:27:30 +0000 | [diff] [blame] | 792 | this.mac, |
| 793 | this.vlanId); |
Anjali K K | f3963f1 | 2019-04-16 19:27:30 +0000 | [diff] [blame] | 794 | sendHostRelearnProbeToConnectPoint(ethernet, connectPoint); |
| 795 | |
| 796 | log.debug("Host Relearn Probe transmission completed."); |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | //Create packet and schedule transmitter thread. |
| 801 | private void sendHostRelearnProbe(ConnectPoint connectPoint, MacAddress mac, VlanId vlanId, |
| 802 | Optional<Ip6Address> ipv6Address) { |
| 803 | PktTransmitter nsTransmitter = new PktTransmitter(mac, vlanId, ipv6Address.get(), connectPoint); |
| 804 | executorService.schedule(nsTransmitter, HOST_PROBE_INIT_DELAY, TimeUnit.MILLISECONDS); |
| 805 | } |
| 806 | |
| 807 | // Send Host Relearn Probe packets to ConnectPoint |
| 808 | private void sendHostRelearnProbeToConnectPoint(Ethernet nsPacket, ConnectPoint connectPoint) { |
| 809 | TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(connectPoint.port()).build(); |
| 810 | OutboundPacket outboundPacket = new DefaultOutboundPacket(connectPoint.deviceId(), |
| 811 | treatment, ByteBuffer.wrap(nsPacket.serialize())); |
| 812 | int counter = 0; |
| 813 | try { |
| 814 | while (counter < dhcpHostRelearnProbeCount) { |
| 815 | packetService.emit(outboundPacket); |
| 816 | counter++; |
| 817 | Thread.sleep(dhcpHostRelearnProbeInterval); |
| 818 | } |
| 819 | } catch (Exception e) { |
| 820 | log.error("Exception while emmiting packet {}", e.getMessage(), e); |
| 821 | } |
| 822 | } |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 823 | |
| 824 | |
| 825 | public Optional<FpmRecord> getFpmRecord(IpPrefix prefix) { |
| 826 | return dhcpFpmPrefixStore.getFpmRecord(prefix); |
| 827 | } |
| 828 | |
| 829 | public Collection<FpmRecord> getFpmRecords() { |
| 830 | return dhcpFpmPrefixStore.getFpmRecords(); |
| 831 | } |
| 832 | |
| 833 | @Override |
| 834 | public void addFpmRecord(IpPrefix prefix, FpmRecord fpmRecord) { |
| 835 | dhcpFpmPrefixStore.addFpmRecord(prefix, fpmRecord); |
| 836 | } |
| 837 | |
| 838 | @Override |
| 839 | public Optional<FpmRecord> removeFpmRecord(IpPrefix prefix) { |
| 840 | return dhcpFpmPrefixStore.removeFpmRecord(prefix); |
| 841 | } |
Kalhee Kim | e5bb709 | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 842 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 843 | } |