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