Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.dhcprelay; |
| 17 | |
| 18 | import java.nio.ByteBuffer; |
| 19 | import java.util.Collection; |
Yi Tseng | 5479fb8 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 20 | import java.util.Collections; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 21 | import java.util.Dictionary; |
Yi Tseng | 5479fb8 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 22 | import java.util.List; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 23 | import java.util.Objects; |
| 24 | import java.util.Optional; |
| 25 | import java.util.Set; |
Yi Tseng | 7da339e | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 26 | import java.util.stream.Collectors; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 27 | import java.util.stream.Stream; |
| 28 | |
Yi Tseng | 7da339e | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 29 | import com.google.common.collect.ImmutableList; |
| 30 | import com.google.common.collect.Streams; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 31 | import org.apache.felix.scr.annotations.Activate; |
| 32 | import org.apache.felix.scr.annotations.Component; |
| 33 | import org.apache.felix.scr.annotations.Deactivate; |
| 34 | import org.apache.felix.scr.annotations.Modified; |
| 35 | import org.apache.felix.scr.annotations.Property; |
| 36 | import org.apache.felix.scr.annotations.Reference; |
| 37 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 38 | import org.apache.felix.scr.annotations.Service; |
| 39 | import org.onlab.packet.ARP; |
| 40 | import org.onlab.packet.DHCP; |
| 41 | import org.onlab.packet.DHCP6; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 42 | import org.onlab.packet.Ethernet; |
Yi Tseng | 4ec727d | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 43 | import org.onlab.packet.IPacket; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 44 | import org.onlab.packet.IPv4; |
Yi Tseng | 4ec727d | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 45 | import org.onlab.packet.IPv6; |
| 46 | import org.onlab.packet.Ip4Address; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 47 | import org.onlab.packet.MacAddress; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 48 | import org.onlab.packet.UDP; |
| 49 | import org.onlab.packet.VlanId; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 50 | import org.onlab.util.Tools; |
| 51 | import org.onosproject.cfg.ComponentConfigService; |
| 52 | import org.onosproject.core.ApplicationId; |
| 53 | import org.onosproject.core.CoreService; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 54 | import org.onosproject.dhcprelay.api.DhcpHandler; |
| 55 | import org.onosproject.dhcprelay.api.DhcpRelayService; |
Yi Tseng | 2fe8f3f | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 56 | import org.onosproject.dhcprelay.api.DhcpServerInfo; |
Yi Tseng | e72fbb5 | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 57 | import org.onosproject.dhcprelay.config.DefaultDhcpRelayConfig; |
Yi Tseng | 4ec727d | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 58 | import org.onosproject.dhcprelay.config.DhcpServerConfig; |
Yi Tseng | 5479fb8 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 59 | import org.onosproject.dhcprelay.config.IgnoreDhcpConfig; |
Yi Tseng | e72fbb5 | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 60 | import org.onosproject.dhcprelay.config.IndirectDhcpRelayConfig; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 61 | import org.onosproject.dhcprelay.store.DhcpRecord; |
| 62 | import org.onosproject.dhcprelay.store.DhcpRelayStore; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 63 | import org.onosproject.net.ConnectPoint; |
Yi Tseng | 76365d8 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 64 | import org.onosproject.net.Device; |
Yi Tseng | 4ec727d | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 65 | import org.onosproject.net.Host; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 66 | import org.onosproject.net.HostId; |
Yi Tseng | 4ec727d | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 67 | import org.onosproject.net.config.Config; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 68 | import org.onosproject.net.config.ConfigFactory; |
| 69 | import org.onosproject.net.config.NetworkConfigEvent; |
| 70 | import org.onosproject.net.config.NetworkConfigListener; |
| 71 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 72 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 73 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 74 | import org.onosproject.net.flow.TrafficSelector; |
| 75 | import org.onosproject.net.flow.TrafficTreatment; |
Yi Tseng | 76365d8 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 76 | import org.onosproject.net.device.DeviceEvent; |
| 77 | import org.onosproject.net.device.DeviceListener; |
| 78 | import org.onosproject.net.device.DeviceService; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 79 | import org.onosproject.net.host.HostService; |
Yi Tseng | 4ec727d | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 80 | import org.onosproject.net.intf.Interface; |
| 81 | import org.onosproject.net.intf.InterfaceService; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 82 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 83 | import org.onosproject.net.packet.OutboundPacket; |
| 84 | import org.onosproject.net.packet.PacketContext; |
| 85 | import org.onosproject.net.packet.PacketPriority; |
| 86 | import org.onosproject.net.packet.PacketProcessor; |
| 87 | import org.onosproject.net.packet.PacketService; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 88 | import org.osgi.service.component.ComponentContext; |
| 89 | import org.slf4j.Logger; |
| 90 | import org.slf4j.LoggerFactory; |
| 91 | |
| 92 | import com.google.common.collect.ImmutableSet; |
| 93 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 94 | import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY; |
Yi Tseng | 76365d8 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 95 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 96 | /** |
| 97 | * DHCP Relay Agent Application Component. |
| 98 | */ |
| 99 | @Component(immediate = true) |
| 100 | @Service |
| 101 | public class DhcpRelayManager implements DhcpRelayService { |
Yi Tseng | e72fbb5 | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 102 | public static final String DHCP_RELAY_APP = "org.onosproject.dhcprelay"; |
Yi Tseng | 325af8c | 2017-09-01 16:02:56 -0700 | [diff] [blame] | 103 | public static final String ROUTE_STORE_IMPL = |
| 104 | "org.onosproject.routeservice.store.RouteStoreImpl"; |
Yi Tseng | 7da339e | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 105 | |
Yi Tseng | 5479fb8 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 106 | private static final TrafficSelector ARP_SELECTOR = DefaultTrafficSelector.builder() |
| 107 | .matchEthType(Ethernet.TYPE_ARP) |
| 108 | .build(); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 109 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 110 | private final InternalConfigListener cfgListener = new InternalConfigListener(); |
| 111 | |
| 112 | private final Set<ConfigFactory> factories = ImmutableSet.of( |
Yi Tseng | e72fbb5 | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 113 | new ConfigFactory<ApplicationId, DefaultDhcpRelayConfig>(APP_SUBJECT_FACTORY, |
| 114 | DefaultDhcpRelayConfig.class, |
| 115 | DefaultDhcpRelayConfig.KEY, |
| 116 | true) { |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 117 | @Override |
Yi Tseng | e72fbb5 | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 118 | public DefaultDhcpRelayConfig createConfig() { |
| 119 | return new DefaultDhcpRelayConfig(); |
| 120 | } |
| 121 | }, |
| 122 | new ConfigFactory<ApplicationId, IndirectDhcpRelayConfig>(APP_SUBJECT_FACTORY, |
| 123 | IndirectDhcpRelayConfig.class, |
| 124 | IndirectDhcpRelayConfig.KEY, |
| 125 | true) { |
| 126 | @Override |
| 127 | public IndirectDhcpRelayConfig createConfig() { |
| 128 | return new IndirectDhcpRelayConfig(); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 129 | } |
Yi Tseng | 5479fb8 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 130 | }, |
| 131 | new ConfigFactory<ApplicationId, IgnoreDhcpConfig>(APP_SUBJECT_FACTORY, |
| 132 | IgnoreDhcpConfig.class, |
| 133 | IgnoreDhcpConfig.KEY, |
| 134 | true) { |
| 135 | @Override |
| 136 | public IgnoreDhcpConfig createConfig() { |
| 137 | return new IgnoreDhcpConfig(); |
| 138 | } |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 139 | } |
| 140 | ); |
| 141 | |
| 142 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 143 | protected NetworkConfigRegistry cfgService; |
| 144 | |
| 145 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 146 | protected CoreService coreService; |
| 147 | |
| 148 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 149 | protected PacketService packetService; |
| 150 | |
| 151 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 152 | protected HostService hostService; |
| 153 | |
| 154 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 155 | protected InterfaceService interfaceService; |
| 156 | |
| 157 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 158 | protected DhcpRelayStore dhcpRelayStore; |
| 159 | |
| 160 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 161 | protected ComponentConfigService compCfgService; |
| 162 | |
Yi Tseng | 5479fb8 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 163 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yi Tseng | 76365d8 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 164 | protected DeviceService deviceService; |
| 165 | |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 166 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY, |
Yi Tseng | 76365d8 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 167 | target = "(version=4)") |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 168 | protected DhcpHandler v4Handler; |
| 169 | |
| 170 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY, |
Yi Tseng | 76365d8 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 171 | target = "(version=6)") |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 172 | protected DhcpHandler v6Handler; |
| 173 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 174 | @Property(name = "arpEnabled", boolValue = true, |
Yi Tseng | 76365d8 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 175 | label = "Enable Address resolution protocol") |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 176 | protected boolean arpEnabled = true; |
| 177 | |
Yi Tseng | 76365d8 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 178 | protected DeviceListener deviceListener = new InternalDeviceListener(); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 179 | private DhcpRelayPacketProcessor dhcpRelayPacketProcessor = new DhcpRelayPacketProcessor(); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 180 | private ApplicationId appId; |
| 181 | |
| 182 | @Activate |
| 183 | protected void activate(ComponentContext context) { |
| 184 | //start the dhcp relay agent |
| 185 | appId = coreService.registerApplication(DHCP_RELAY_APP); |
| 186 | |
| 187 | cfgService.addListener(cfgListener); |
| 188 | factories.forEach(cfgService::registerConfigFactory); |
| 189 | //update the dhcp server configuration. |
| 190 | updateConfig(); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 191 | |
| 192 | //add the packet processor |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 193 | packetService.addProcessor(dhcpRelayPacketProcessor, PacketProcessor.director(0)); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 194 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 195 | modified(context); |
| 196 | |
Yi Tseng | 325af8c | 2017-09-01 16:02:56 -0700 | [diff] [blame] | 197 | // Enable distribute route store |
| 198 | compCfgService.preSetProperty(ROUTE_STORE_IMPL, |
| 199 | "distributed", Boolean.TRUE.toString()); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 200 | compCfgService.registerProperties(getClass()); |
Yi Tseng | 76365d8 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 201 | |
| 202 | deviceService.addListener(deviceListener); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 203 | log.info("DHCP-RELAY Started"); |
| 204 | } |
| 205 | |
| 206 | @Deactivate |
| 207 | protected void deactivate() { |
| 208 | cfgService.removeListener(cfgListener); |
| 209 | factories.forEach(cfgService::unregisterConfigFactory); |
| 210 | packetService.removeProcessor(dhcpRelayPacketProcessor); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 211 | cancelArpPackets(); |
Yi Tseng | dfa8ee2 | 2017-08-07 12:45:01 -0700 | [diff] [blame] | 212 | compCfgService.unregisterProperties(getClass(), false); |
Yi Tseng | 76365d8 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 213 | deviceService.removeListener(deviceListener); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 214 | log.info("DHCP-RELAY Stopped"); |
| 215 | } |
| 216 | |
| 217 | @Modified |
| 218 | protected void modified(ComponentContext context) { |
| 219 | Dictionary<?, ?> properties = context.getProperties(); |
| 220 | Boolean flag; |
| 221 | |
| 222 | flag = Tools.isPropertyEnabled(properties, "arpEnabled"); |
| 223 | if (flag != null) { |
| 224 | arpEnabled = flag; |
| 225 | log.info("Address resolution protocol is {}", |
| 226 | arpEnabled ? "enabled" : "disabled"); |
| 227 | } |
| 228 | |
| 229 | if (arpEnabled) { |
| 230 | requestArpPackets(); |
| 231 | } else { |
| 232 | cancelArpPackets(); |
| 233 | } |
| 234 | } |
| 235 | |
Yi Tseng | 7da339e | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 236 | private static List<TrafficSelector> buildClientDhcpSelectors() { |
| 237 | return Streams.concat(Dhcp4HandlerImpl.DHCP_SELECTORS.stream(), |
| 238 | Dhcp6HandlerImpl.DHCP_SELECTORS.stream()) |
| 239 | .collect(Collectors.toList()); |
| 240 | } |
| 241 | |
Yi Tseng | e72fbb5 | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 242 | /** |
| 243 | * Updates DHCP relay app configuration. |
| 244 | */ |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 245 | private void updateConfig() { |
Yi Tseng | e72fbb5 | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 246 | DefaultDhcpRelayConfig defaultConfig = |
| 247 | cfgService.getConfig(appId, DefaultDhcpRelayConfig.class); |
| 248 | IndirectDhcpRelayConfig indirectConfig = |
| 249 | cfgService.getConfig(appId, IndirectDhcpRelayConfig.class); |
Yi Tseng | 5479fb8 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 250 | IgnoreDhcpConfig ignoreDhcpConfig = |
| 251 | cfgService.getConfig(appId, IgnoreDhcpConfig.class); |
Yi Tseng | e72fbb5 | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 252 | |
| 253 | if (defaultConfig != null) { |
| 254 | updateConfig(defaultConfig); |
| 255 | } |
Yi Tseng | e72fbb5 | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 256 | if (indirectConfig != null) { |
| 257 | updateConfig(indirectConfig); |
| 258 | } |
Yi Tseng | 5479fb8 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 259 | if (ignoreDhcpConfig != null) { |
| 260 | updateConfig(ignoreDhcpConfig); |
| 261 | } |
Yi Tseng | e72fbb5 | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Updates DHCP relay app configuration with given configuration. |
| 266 | * |
| 267 | * @param config the configuration ot update |
| 268 | */ |
Yi Tseng | 5479fb8 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 269 | protected void updateConfig(Config config) { |
Yi Tseng | e72fbb5 | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 270 | if (config instanceof IndirectDhcpRelayConfig) { |
| 271 | IndirectDhcpRelayConfig indirectConfig = (IndirectDhcpRelayConfig) config; |
| 272 | v4Handler.setIndirectDhcpServerConfigs(indirectConfig.dhcpServerConfigs()); |
| 273 | v6Handler.setIndirectDhcpServerConfigs(indirectConfig.dhcpServerConfigs()); |
Yi Tseng | 4fa0583 | 2017-08-17 13:08:31 -0700 | [diff] [blame] | 274 | } else if (config instanceof DefaultDhcpRelayConfig) { |
| 275 | DefaultDhcpRelayConfig defaultConfig = (DefaultDhcpRelayConfig) config; |
| 276 | v4Handler.setDefaultDhcpServerConfigs(defaultConfig.dhcpServerConfigs()); |
| 277 | v6Handler.setDefaultDhcpServerConfigs(defaultConfig.dhcpServerConfigs()); |
Yi Tseng | e72fbb5 | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 278 | } |
Yi Tseng | 5479fb8 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 279 | if (config instanceof IgnoreDhcpConfig) { |
Yi Tseng | 7da339e | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 280 | v4Handler.updateIgnoreVlanConfig((IgnoreDhcpConfig) config); |
| 281 | v6Handler.updateIgnoreVlanConfig((IgnoreDhcpConfig) config); |
Yi Tseng | 5479fb8 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
| 285 | protected void removeConfig(Config config) { |
| 286 | if (config instanceof IndirectDhcpRelayConfig) { |
| 287 | v4Handler.setIndirectDhcpServerConfigs(Collections.emptyList()); |
| 288 | v6Handler.setIndirectDhcpServerConfigs(Collections.emptyList()); |
| 289 | } else if (config instanceof DefaultDhcpRelayConfig) { |
| 290 | v4Handler.setDefaultDhcpServerConfigs(Collections.emptyList()); |
| 291 | v6Handler.setDefaultDhcpServerConfigs(Collections.emptyList()); |
| 292 | } |
| 293 | if (config instanceof IgnoreDhcpConfig) { |
Yi Tseng | 7da339e | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 294 | v4Handler.updateIgnoreVlanConfig(null); |
| 295 | v6Handler.updateIgnoreVlanConfig(null); |
Yi Tseng | 5479fb8 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 299 | /** |
| 300 | * Request ARP packet in via PacketService. |
| 301 | */ |
| 302 | private void requestArpPackets() { |
Yi Tseng | 5479fb8 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 303 | packetService.requestPackets(ARP_SELECTOR, PacketPriority.CONTROL, appId); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Cancel requested ARP packets in via packet service. |
| 308 | */ |
| 309 | private void cancelArpPackets() { |
Yi Tseng | 5479fb8 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 310 | packetService.cancelPackets(ARP_SELECTOR, PacketPriority.CONTROL, appId); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | @Override |
| 314 | public Optional<DhcpRecord> getDhcpRecord(HostId hostId) { |
| 315 | return dhcpRelayStore.getDhcpRecord(hostId); |
| 316 | } |
| 317 | |
| 318 | @Override |
| 319 | public Collection<DhcpRecord> getDhcpRecords() { |
| 320 | return dhcpRelayStore.getDhcpRecords(); |
| 321 | } |
| 322 | |
Yi Tseng | 13a41a1 | 2017-07-26 13:45:01 -0700 | [diff] [blame] | 323 | @Override |
| 324 | public Optional<MacAddress> getDhcpServerMacAddress() { |
Yi Tseng | 4ec727d | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 325 | // TODO: depreated it |
| 326 | DefaultDhcpRelayConfig config = cfgService.getConfig(appId, DefaultDhcpRelayConfig.class); |
| 327 | DhcpServerConfig serverConfig = config.dhcpServerConfigs().get(0); |
| 328 | Ip4Address serverip = serverConfig.getDhcpServerIp4().get(); |
| 329 | return hostService.getHostsByIp(serverip) |
| 330 | .stream() |
| 331 | .map(Host::mac) |
| 332 | .findFirst(); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Yi Tseng | 2fe8f3f | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 335 | @Override |
| 336 | public List<DhcpServerInfo> getDefaultDhcpServerInfoList() { |
| 337 | return ImmutableList.<DhcpServerInfo>builder() |
| 338 | .addAll(v4Handler.getDefaultDhcpServerInfoList()) |
| 339 | .addAll(v6Handler.getDefaultDhcpServerInfoList()) |
| 340 | .build(); |
| 341 | } |
| 342 | |
| 343 | @Override |
| 344 | public List<DhcpServerInfo> getIndirectDhcpServerInfoList() { |
| 345 | return ImmutableList.<DhcpServerInfo>builder() |
| 346 | .addAll(v4Handler.getIndirectDhcpServerInfoList()) |
| 347 | .addAll(v6Handler.getIndirectDhcpServerInfoList()) |
| 348 | .build(); |
| 349 | } |
| 350 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 351 | /** |
| 352 | * Gets DHCP data from a packet. |
| 353 | * |
| 354 | * @param packet the packet |
| 355 | * @return the DHCP data; empty if it is not a DHCP packet |
| 356 | */ |
| 357 | private Optional<DHCP> findDhcp(Ethernet packet) { |
| 358 | return Stream.of(packet) |
| 359 | .filter(Objects::nonNull) |
| 360 | .map(Ethernet::getPayload) |
| 361 | .filter(p -> p instanceof IPv4) |
| 362 | .map(IPacket::getPayload) |
| 363 | .filter(Objects::nonNull) |
| 364 | .filter(p -> p instanceof UDP) |
| 365 | .map(IPacket::getPayload) |
| 366 | .filter(Objects::nonNull) |
| 367 | .filter(p -> p instanceof DHCP) |
| 368 | .map(p -> (DHCP) p) |
| 369 | .findFirst(); |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * Gets DHCPv6 data from a packet. |
| 374 | * |
| 375 | * @param packet the packet |
| 376 | * @return the DHCPv6 data; empty if it is not a DHCPv6 packet |
| 377 | */ |
| 378 | private Optional<DHCP6> findDhcp6(Ethernet packet) { |
| 379 | return Stream.of(packet) |
| 380 | .filter(Objects::nonNull) |
| 381 | .map(Ethernet::getPayload) |
| 382 | .filter(p -> p instanceof IPv6) |
| 383 | .map(IPacket::getPayload) |
| 384 | .filter(Objects::nonNull) |
| 385 | .filter(p -> p instanceof UDP) |
| 386 | .map(IPacket::getPayload) |
| 387 | .filter(Objects::nonNull) |
| 388 | .filter(p -> p instanceof DHCP6) |
| 389 | .map(p -> (DHCP6) p) |
| 390 | .findFirst(); |
| 391 | } |
| 392 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 393 | |
| 394 | private class DhcpRelayPacketProcessor implements PacketProcessor { |
| 395 | |
| 396 | @Override |
| 397 | public void process(PacketContext context) { |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 398 | // process the packet and get the payload |
| 399 | Ethernet packet = context.inPacket().parsed(); |
| 400 | if (packet == null) { |
| 401 | return; |
| 402 | } |
| 403 | |
| 404 | findDhcp(packet).ifPresent(dhcpPayload -> { |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 405 | v4Handler.processDhcpPacket(context, dhcpPayload); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 406 | }); |
| 407 | |
| 408 | findDhcp6(packet).ifPresent(dhcp6Payload -> { |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 409 | v6Handler.processDhcpPacket(context, dhcp6Payload); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 410 | }); |
| 411 | |
| 412 | if (packet.getEtherType() == Ethernet.TYPE_ARP && arpEnabled) { |
| 413 | ARP arpPacket = (ARP) packet.getPayload(); |
| 414 | VlanId vlanId = VlanId.vlanId(packet.getVlanID()); |
| 415 | Set<Interface> interfaces = interfaceService. |
| 416 | getInterfacesByPort(context.inPacket().receivedFrom()); |
| 417 | //ignore the packets if dhcp server interface is not configured on onos. |
| 418 | if (interfaces.isEmpty()) { |
| 419 | log.warn("server virtual interface not configured"); |
| 420 | return; |
| 421 | } |
| 422 | if ((arpPacket.getOpCode() != ARP.OP_REQUEST)) { |
| 423 | // handle request only |
| 424 | return; |
| 425 | } |
| 426 | MacAddress interfaceMac = interfaces.stream() |
| 427 | .filter(iface -> iface.vlan().equals(vlanId)) |
| 428 | .map(Interface::mac) |
| 429 | .filter(mac -> !mac.equals(MacAddress.NONE)) |
| 430 | .findFirst() |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 431 | .orElse(MacAddress.ONOS); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 432 | if (interfaceMac == null) { |
| 433 | // can't find interface mac address |
| 434 | return; |
| 435 | } |
| 436 | processArpPacket(context, packet, interfaceMac); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * Processes the ARP Payload and initiates a reply to the client. |
| 442 | * |
| 443 | * @param context the packet context |
| 444 | * @param packet the ethernet payload |
| 445 | * @param replyMac mac address to be replied |
| 446 | */ |
| 447 | private void processArpPacket(PacketContext context, Ethernet packet, MacAddress replyMac) { |
| 448 | ARP arpPacket = (ARP) packet.getPayload(); |
| 449 | ARP arpReply = (ARP) arpPacket.clone(); |
| 450 | arpReply.setOpCode(ARP.OP_REPLY); |
| 451 | |
| 452 | arpReply.setTargetProtocolAddress(arpPacket.getSenderProtocolAddress()); |
| 453 | arpReply.setTargetHardwareAddress(arpPacket.getSenderHardwareAddress()); |
| 454 | arpReply.setSenderProtocolAddress(arpPacket.getTargetProtocolAddress()); |
| 455 | arpReply.setSenderHardwareAddress(replyMac.toBytes()); |
| 456 | |
| 457 | // Ethernet Frame. |
| 458 | Ethernet ethReply = new Ethernet(); |
| 459 | ethReply.setSourceMACAddress(replyMac.toBytes()); |
| 460 | ethReply.setDestinationMACAddress(packet.getSourceMAC()); |
| 461 | ethReply.setEtherType(Ethernet.TYPE_ARP); |
| 462 | ethReply.setVlanID(packet.getVlanID()); |
| 463 | ethReply.setPayload(arpReply); |
| 464 | |
| 465 | ConnectPoint targetPort = context.inPacket().receivedFrom(); |
| 466 | TrafficTreatment t = DefaultTrafficTreatment.builder() |
| 467 | .setOutput(targetPort.port()).build(); |
| 468 | OutboundPacket o = new DefaultOutboundPacket( |
| 469 | targetPort.deviceId(), t, ByteBuffer.wrap(ethReply.serialize())); |
| 470 | if (log.isTraceEnabled()) { |
| 471 | log.trace("Relaying ARP packet {} to {}", packet, targetPort); |
| 472 | } |
| 473 | packetService.emit(o); |
| 474 | } |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | /** |
| 478 | * Listener for network config events. |
| 479 | */ |
| 480 | private class InternalConfigListener implements NetworkConfigListener { |
| 481 | @Override |
| 482 | public void event(NetworkConfigEvent event) { |
Yi Tseng | 5479fb8 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 483 | switch (event.type()) { |
| 484 | case CONFIG_UPDATED: |
| 485 | case CONFIG_ADDED: |
| 486 | event.config().ifPresent(config -> { |
| 487 | updateConfig(config); |
| 488 | log.info("{} updated", config.getClass().getSimpleName()); |
| 489 | }); |
| 490 | break; |
| 491 | case CONFIG_REMOVED: |
| 492 | event.prevConfig().ifPresent(config -> { |
| 493 | removeConfig(config); |
| 494 | log.info("{} removed", config.getClass().getSimpleName()); |
| 495 | }); |
| 496 | break; |
| 497 | default: |
| 498 | log.warn("Unsupported event type {}", event.type()); |
| 499 | break; |
Yi Tseng | e72fbb5 | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 500 | } |
Charles Chan | 91b371b | 2018-01-03 16:26:32 -0800 | [diff] [blame^] | 501 | } |
Yi Tseng | 5479fb8 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 502 | |
Charles Chan | 91b371b | 2018-01-03 16:26:32 -0800 | [diff] [blame^] | 503 | @Override |
| 504 | public boolean isRelevant(NetworkConfigEvent event) { |
| 505 | if (event.configClass().equals(DefaultDhcpRelayConfig.class) || |
| 506 | event.configClass().equals(IndirectDhcpRelayConfig.class) || |
| 507 | event.configClass().equals(IgnoreDhcpConfig.class)) { |
| 508 | return true; |
| 509 | } |
| 510 | log.debug("Ignore irrelevant event class {}", event.configClass().getName()); |
| 511 | return false; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 512 | } |
| 513 | } |
Yi Tseng | 76365d8 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 514 | |
| 515 | private class InternalDeviceListener implements DeviceListener { |
| 516 | |
| 517 | @Override |
| 518 | public void event(DeviceEvent event) { |
| 519 | Device device = event.subject(); |
| 520 | switch (event.type()) { |
| 521 | case DEVICE_ADDED: |
Yi Tseng | 7da339e | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 522 | updateIgnoreVlanConfigs(); |
Yi Tseng | 76365d8 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 523 | break; |
| 524 | case DEVICE_AVAILABILITY_CHANGED: |
| 525 | deviceAvailabilityChanged(device); |
Yi Tseng | 76365d8 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 526 | default: |
| 527 | break; |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | private void deviceAvailabilityChanged(Device device) { |
| 532 | if (deviceService.isAvailable(device.id())) { |
Yi Tseng | 7da339e | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 533 | updateIgnoreVlanConfigs(); |
Saurav Das | ba7eb1a | 2017-12-13 16:19:35 -0800 | [diff] [blame] | 534 | } else { |
| 535 | removeIgnoreVlanState(); |
Yi Tseng | 76365d8 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | |
Yi Tseng | 7da339e | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 539 | private void updateIgnoreVlanConfigs() { |
Yi Tseng | 76365d8 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 540 | IgnoreDhcpConfig config = cfgService.getConfig(appId, IgnoreDhcpConfig.class); |
Yi Tseng | 7da339e | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 541 | v4Handler.updateIgnoreVlanConfig(config); |
| 542 | v6Handler.updateIgnoreVlanConfig(config); |
Yi Tseng | 76365d8 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 543 | } |
Saurav Das | ba7eb1a | 2017-12-13 16:19:35 -0800 | [diff] [blame] | 544 | |
| 545 | private void removeIgnoreVlanState() { |
| 546 | IgnoreDhcpConfig config = cfgService.getConfig(appId, IgnoreDhcpConfig.class); |
| 547 | v4Handler.removeIgnoreVlanState(config); |
| 548 | v6Handler.removeIgnoreVlanState(config); |
| 549 | } |
Yi Tseng | 76365d8 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 550 | } |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 551 | } |