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