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