Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 1 | /* |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [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.opencord.dhcpl2relay; |
| 17 | |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 18 | import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_MessageType; |
| 19 | import static org.onlab.packet.MacAddress.valueOf; |
| 20 | import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY; |
| 21 | |
| 22 | import java.nio.ByteBuffer; |
| 23 | import java.util.ArrayList; |
| 24 | import java.util.Arrays; |
| 25 | import java.util.Dictionary; |
| 26 | import java.util.List; |
| 27 | import java.util.Map; |
| 28 | import java.util.Optional; |
| 29 | import java.util.Set; |
| 30 | import java.util.concurrent.atomic.AtomicReference; |
| 31 | import java.util.stream.Collectors; |
| 32 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 33 | import org.apache.felix.scr.annotations.Activate; |
| 34 | import org.apache.felix.scr.annotations.Component; |
| 35 | import org.apache.felix.scr.annotations.Deactivate; |
| 36 | import org.apache.felix.scr.annotations.Modified; |
| 37 | import org.apache.felix.scr.annotations.Property; |
| 38 | import org.apache.felix.scr.annotations.Reference; |
| 39 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 40 | import org.apache.felix.scr.annotations.Service; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 41 | import org.onlab.packet.DHCP; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 42 | import org.onlab.packet.DHCPPacketType; |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 43 | import org.onlab.packet.EthType.EtherType; |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 44 | import org.onlab.packet.Ethernet; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 45 | import org.onlab.packet.IPv4; |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 46 | import org.onlab.packet.IpAddress; |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 47 | import org.onlab.packet.MacAddress; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 48 | import org.onlab.packet.TpPort; |
| 49 | import org.onlab.packet.UDP; |
| 50 | import org.onlab.packet.VlanId; |
Jonathan Hart | edbf642 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 51 | import org.onlab.packet.dhcp.DhcpOption; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 52 | import org.onlab.util.Tools; |
| 53 | import org.onosproject.cfg.ComponentConfigService; |
| 54 | import org.onosproject.core.ApplicationId; |
| 55 | import org.onosproject.core.CoreService; |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 56 | import org.onosproject.event.AbstractListenerManager; |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 57 | import org.onosproject.mastership.MastershipEvent; |
| 58 | import org.onosproject.mastership.MastershipListener; |
| 59 | import org.onosproject.mastership.MastershipService; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 60 | import org.onosproject.net.AnnotationKeys; |
| 61 | import org.onosproject.net.ConnectPoint; |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 62 | import org.onosproject.net.Device; |
| 63 | import org.onosproject.net.DeviceId; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 64 | import org.onosproject.net.Host; |
| 65 | import org.onosproject.net.Port; |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 66 | import org.onosproject.net.PortNumber; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 67 | import org.onosproject.net.config.ConfigFactory; |
| 68 | import org.onosproject.net.config.NetworkConfigEvent; |
| 69 | import org.onosproject.net.config.NetworkConfigListener; |
| 70 | import org.onosproject.net.config.NetworkConfigRegistry; |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 71 | import org.onosproject.net.device.DeviceEvent; |
| 72 | import org.onosproject.net.device.DeviceListener; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 73 | import org.onosproject.net.device.DeviceService; |
| 74 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 75 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 76 | import org.onosproject.net.flow.TrafficSelector; |
| 77 | import org.onosproject.net.flow.TrafficTreatment; |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 78 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 79 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 80 | import org.onosproject.net.flowobjective.ForwardingObjective; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 81 | import org.onosproject.net.host.HostService; |
| 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; |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 88 | import org.opencord.dhcpl2relay.packet.DhcpOption82; |
Gamze Abaka | c806c6c | 2018-12-03 12:49:46 +0000 | [diff] [blame^] | 89 | import org.opencord.sadis.BaseInformationService; |
| 90 | import org.opencord.sadis.SadisService; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 91 | import org.opencord.sadis.SubscriberAndDeviceInformation; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 92 | import org.osgi.service.component.ComponentContext; |
| 93 | import org.slf4j.Logger; |
| 94 | import org.slf4j.LoggerFactory; |
| 95 | |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 96 | import com.google.common.collect.ImmutableSet; |
| 97 | import com.google.common.collect.Lists; |
| 98 | import com.google.common.collect.Maps; |
| 99 | import com.google.common.collect.Sets; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 100 | |
| 101 | /** |
| 102 | * DHCP Relay Agent Application Component. |
| 103 | */ |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 104 | @Service |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 105 | @Component(immediate = true) |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 106 | public class DhcpL2Relay |
| 107 | extends AbstractListenerManager<DhcpL2RelayEvent, DhcpL2RelayListener> |
| 108 | implements DhcpL2RelayService { |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 109 | |
| 110 | public static final String DHCP_L2RELAY_APP = "org.opencord.dhcpl2relay"; |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 111 | private static final String HOST_LOC_PROVIDER = |
| 112 | "org.onosproject.provider.host.impl.HostLocationProvider"; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 113 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 114 | private final InternalConfigListener cfgListener = |
| 115 | new InternalConfigListener(); |
| 116 | |
| 117 | private final Set<ConfigFactory> factories = ImmutableSet.of( |
| 118 | new ConfigFactory<ApplicationId, DhcpL2RelayConfig>(APP_SUBJECT_FACTORY, |
| 119 | DhcpL2RelayConfig.class, |
| 120 | "dhcpl2relay") { |
| 121 | @Override |
| 122 | public DhcpL2RelayConfig createConfig() { |
| 123 | return new DhcpL2RelayConfig(); |
| 124 | } |
| 125 | } |
| 126 | ); |
| 127 | |
| 128 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 129 | protected NetworkConfigRegistry cfgService; |
| 130 | |
| 131 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 132 | protected CoreService coreService; |
| 133 | |
| 134 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 135 | protected PacketService packetService; |
| 136 | |
| 137 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 138 | protected HostService hostService; |
| 139 | |
| 140 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 141 | protected ComponentConfigService componentConfigService; |
| 142 | |
| 143 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Gamze Abaka | c806c6c | 2018-12-03 12:49:46 +0000 | [diff] [blame^] | 144 | protected SadisService sadisService; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 145 | |
| 146 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 147 | protected DeviceService deviceService; |
| 148 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 149 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 150 | protected MastershipService mastershipService; |
| 151 | |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 152 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 153 | protected FlowObjectiveService flowObjectiveService; |
| 154 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 155 | @Property(name = "option82", boolValue = true, |
| 156 | label = "Add option 82 to relayed packets") |
| 157 | protected boolean option82 = true; |
| 158 | |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 159 | @Property(name = "enableDhcpBroadcastReplies", boolValue = false, |
Amit Ghosh | 2095dc6 | 2017-09-25 20:56:55 +0100 | [diff] [blame] | 160 | label = "Ask the DHCP Server to send back replies as L2 broadcast") |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 161 | protected boolean enableDhcpBroadcastReplies = false; |
| 162 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 163 | private DhcpRelayPacketProcessor dhcpRelayPacketProcessor = |
| 164 | new DhcpRelayPacketProcessor(); |
| 165 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 166 | private InnerMastershipListener changeListener = new InnerMastershipListener(); |
| 167 | private InnerDeviceListener deviceListener = new InnerDeviceListener(); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 168 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 169 | // connect points to the DHCP server |
| 170 | Set<ConnectPoint> dhcpConnectPoints; |
| 171 | private AtomicReference<ConnectPoint> dhcpServerConnectPoint = new AtomicReference<>(); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 172 | private MacAddress dhcpConnectMac = MacAddress.BROADCAST; |
| 173 | private ApplicationId appId; |
| 174 | |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 175 | static Map<String, DhcpAllocationInfo> allocationMap = Maps.newConcurrentMap(); |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 176 | private boolean modifyClientPktsSrcDstMac = false; |
| 177 | //Whether to use the uplink port of the OLTs to send/receive messages to the DHCP server |
| 178 | private boolean useOltUplink = false; |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 179 | |
Gamze Abaka | c806c6c | 2018-12-03 12:49:46 +0000 | [diff] [blame^] | 180 | private BaseInformationService<SubscriberAndDeviceInformation> subsService; |
| 181 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 182 | @Activate |
| 183 | protected void activate(ComponentContext context) { |
| 184 | //start the dhcp relay agent |
| 185 | appId = coreService.registerApplication(DHCP_L2RELAY_APP); |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 186 | // ensure that host-learning via dhcp includes IP addresses |
| 187 | componentConfigService.preSetProperty(HOST_LOC_PROVIDER, |
| 188 | "useDhcp", Boolean.TRUE.toString()); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 189 | componentConfigService.registerProperties(getClass()); |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 190 | eventDispatcher.addSink(DhcpL2RelayEvent.class, listenerRegistry); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 191 | |
| 192 | cfgService.addListener(cfgListener); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 193 | mastershipService.addListener(changeListener); |
| 194 | deviceService.addListener(deviceListener); |
| 195 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 196 | factories.forEach(cfgService::registerConfigFactory); |
| 197 | //update the dhcp server configuration. |
| 198 | updateConfig(); |
| 199 | //add the packet services. |
| 200 | packetService.addProcessor(dhcpRelayPacketProcessor, |
| 201 | PacketProcessor.director(0)); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 202 | if (context != null) { |
| 203 | modified(context); |
| 204 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 205 | |
Gamze Abaka | c806c6c | 2018-12-03 12:49:46 +0000 | [diff] [blame^] | 206 | subsService = sadisService.getSubscriberInfoService(); |
| 207 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 208 | log.info("DHCP-L2-RELAY Started"); |
| 209 | } |
| 210 | |
| 211 | @Deactivate |
| 212 | protected void deactivate() { |
| 213 | cfgService.removeListener(cfgListener); |
| 214 | factories.forEach(cfgService::unregisterConfigFactory); |
| 215 | packetService.removeProcessor(dhcpRelayPacketProcessor); |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 216 | cancelDhcpPktsFromServer(); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 217 | |
| 218 | componentConfigService.unregisterProperties(getClass(), false); |
Deepa Vaddireddy | 77a6ac7 | 2017-09-20 20:36:52 +0530 | [diff] [blame] | 219 | deviceService.removeListener(deviceListener); |
| 220 | mastershipService.removeListener(changeListener); |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 221 | eventDispatcher.removeSink(DhcpL2RelayEvent.class); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 222 | log.info("DHCP-L2-RELAY Stopped"); |
| 223 | } |
| 224 | |
| 225 | @Modified |
| 226 | protected void modified(ComponentContext context) { |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 227 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 228 | Dictionary<?, ?> properties = context.getProperties(); |
| 229 | |
| 230 | Boolean o = Tools.isPropertyEnabled(properties, "option82"); |
| 231 | if (o != null) { |
| 232 | option82 = o; |
| 233 | } |
Amit Ghosh | 2095dc6 | 2017-09-25 20:56:55 +0100 | [diff] [blame] | 234 | |
| 235 | o = Tools.isPropertyEnabled(properties, "enableDhcpBroadcastReplies"); |
| 236 | if (o != null) { |
| 237 | enableDhcpBroadcastReplies = o; |
| 238 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Checks if this app has been configured. |
| 243 | * |
| 244 | * @return true if all information we need have been initialized |
| 245 | */ |
| 246 | private boolean configured() { |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 247 | if (!useOltUplink) { |
| 248 | return dhcpServerConnectPoint.get() != null; |
| 249 | } |
| 250 | return true; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 251 | } |
| 252 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 253 | /** |
| 254 | * Selects a connect point through an available device for which it is the master. |
| 255 | */ |
| 256 | private void selectServerConnectPoint() { |
| 257 | synchronized (this) { |
| 258 | dhcpServerConnectPoint.set(null); |
| 259 | if (dhcpConnectPoints != null) { |
| 260 | // find a connect point through a device for which we are master |
| 261 | for (ConnectPoint cp: dhcpConnectPoints) { |
| 262 | if (mastershipService.isLocalMaster(cp.deviceId())) { |
| 263 | if (deviceService.isAvailable(cp.deviceId())) { |
| 264 | dhcpServerConnectPoint.set(cp); |
| 265 | } |
| 266 | log.info("DHCP connectPoint selected is {}", cp); |
| 267 | break; |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | log.info("DHCP Server connectPoint is {}", dhcpServerConnectPoint.get()); |
| 273 | |
| 274 | if (dhcpServerConnectPoint.get() == null) { |
| 275 | log.error("Master of none, can't relay DHCP Message to server"); |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Updates the network configuration. |
| 282 | */ |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 283 | private void updateConfig() { |
| 284 | DhcpL2RelayConfig cfg = cfgService.getConfig(appId, DhcpL2RelayConfig.class); |
| 285 | if (cfg == null) { |
| 286 | log.warn("Dhcp Server info not available"); |
| 287 | return; |
| 288 | } |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 289 | |
| 290 | dhcpConnectPoints = Sets.newConcurrentHashSet(cfg.getDhcpServerConnectPoint()); |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 291 | modifyClientPktsSrcDstMac = cfg.getModifySrcDstMacAddresses(); |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 292 | boolean prevUseOltUplink = useOltUplink; |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 293 | useOltUplink = cfg.getUseOltUplinkForServerPktInOut(); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 294 | |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 295 | if (useOltUplink) { |
| 296 | for (ConnectPoint cp : getUplinkPortsOfOlts()) { |
| 297 | log.debug("requestDhcpPackets: ConnectPoint: {}", cp); |
| 298 | requestDhcpPacketsFromConnectPoint(cp); |
| 299 | } |
| 300 | // check if previous config was different and so trap flows may |
| 301 | // need to be removed from other places |
| 302 | if (!prevUseOltUplink) { |
| 303 | if (dhcpServerConnectPoint.get() == null) { |
| 304 | log.warn("No dhcpServer connectPoint found .. cannot remove" |
| 305 | + " previously installed trap flows"); |
| 306 | return; |
| 307 | } |
| 308 | // XXX change to use packet service when priority can be set |
| 309 | DefaultForwardingObjective.Builder bldr = |
| 310 | trapDhcpPktsFromServer(dhcpServerConnectPoint.get().deviceId(), |
| 311 | dhcpServerConnectPoint.get().port()); |
| 312 | flowObjectiveService.forward(dhcpServerConnectPoint.get().deviceId(), |
| 313 | bldr.remove()); |
| 314 | } |
| 315 | |
| 316 | } else { |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 317 | selectServerConnectPoint(); |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 318 | log.info("dhcp server connect point: " + dhcpServerConnectPoint); |
| 319 | // create flow to trap packets coming from the server to this connectpoint |
| 320 | // which is typically not on the OLT (otherwise use the OLT's uplink) |
| 321 | if (dhcpServerConnectPoint.get() == null) { |
| 322 | log.warn("No dhcpServer connectPoint found, cannot install dhcp" |
| 323 | + " trap flows"); |
| 324 | return; |
| 325 | } |
| 326 | // XXX change to use packet service when priority can be set |
| 327 | DefaultForwardingObjective.Builder bldr = |
| 328 | trapDhcpPktsFromServer(dhcpServerConnectPoint.get().deviceId(), |
| 329 | dhcpServerConnectPoint.get().port()); |
| 330 | flowObjectiveService.forward(dhcpServerConnectPoint.get().deviceId(), |
| 331 | bldr.add()); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | private void cancelDhcpPktsFromServer() { |
| 336 | if (useOltUplink) { |
| 337 | for (ConnectPoint cp : getUplinkPortsOfOlts()) { |
| 338 | log.debug("cancelDhcpPackets: ConnectPoint: {}", cp); |
| 339 | cancelDhcpPacketsFromConnectPoint(cp); |
| 340 | } |
| 341 | } else { |
| 342 | // delete flow to trap packets coming from the server to this connectpoint |
| 343 | // which is typically not on the OLT (otherwise use the OLT's uplink) |
| 344 | if (dhcpServerConnectPoint.get() == null) { |
| 345 | log.warn("No dhcpServer connectPoint found.. cannot remove dhcp" |
| 346 | + " trap flows"); |
| 347 | return; |
| 348 | } |
| 349 | // XXX change to use packet service when priority can be set |
| 350 | DefaultForwardingObjective.Builder bldr = |
| 351 | trapDhcpPktsFromServer(dhcpServerConnectPoint.get().deviceId(), |
| 352 | dhcpServerConnectPoint.get().port()); |
| 353 | flowObjectiveService.forward(dhcpServerConnectPoint.get().deviceId(), |
| 354 | bldr.remove()); |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 355 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 356 | |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | private DefaultForwardingObjective.Builder trapDhcpPktsFromServer(DeviceId devId, |
| 360 | PortNumber port) { |
| 361 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 362 | .punt() |
| 363 | .wipeDeferred() |
| 364 | .build(); |
| 365 | TrafficSelector downstream = DefaultTrafficSelector.builder() |
| 366 | .matchInPort(port) |
| 367 | .matchEthType(EtherType.IPV4.ethType().toShort()) |
| 368 | .matchIPProtocol(IPv4.PROTOCOL_UDP) |
| 369 | .matchUdpSrc(TpPort.tpPort(67)) |
| 370 | .matchUdpDst(TpPort.tpPort(68)) |
| 371 | .build(); |
| 372 | |
| 373 | DefaultForwardingObjective.Builder bldr = DefaultForwardingObjective.builder() |
| 374 | .withPriority(61000) // XXX packet service does not allow setting this |
| 375 | .withSelector(downstream) |
| 376 | .fromApp(appId) |
| 377 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 378 | .withTreatment(treatment) |
| 379 | .makePermanent(); |
| 380 | return bldr; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | /** |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 384 | * Returns all the uplink ports of OLTs configured in SADIS. |
| 385 | * Only ports visible in ONOS and for which this instance is master |
| 386 | * are returned |
| 387 | */ |
| 388 | private List<ConnectPoint> getUplinkPortsOfOlts() { |
| 389 | List<ConnectPoint> cps = new ArrayList<>(); |
| 390 | |
| 391 | // find all the olt devices and if their uplink ports are visible |
| 392 | Iterable<Device> devices = deviceService.getDevices(); |
| 393 | for (Device d : devices) { |
| 394 | // check if this device is provisioned in Sadis |
| 395 | |
| 396 | log.debug("getUplinkPortsOfOlts: Checking mastership of {}", d); |
| 397 | // do only for devices for which we are the master |
| 398 | if (!mastershipService.isLocalMaster(d.id())) { |
| 399 | continue; |
| 400 | } |
| 401 | |
| 402 | String devSerialNo = d.serialNumber(); |
| 403 | SubscriberAndDeviceInformation deviceInfo = subsService.get(devSerialNo); |
| 404 | log.debug("getUplinkPortsOfOlts: Found device: {}", deviceInfo); |
| 405 | if (deviceInfo != null) { |
| 406 | // check if the uplink port with that number is available on the device |
| 407 | PortNumber pNum = PortNumber.portNumber(deviceInfo.uplinkPort()); |
| 408 | Port port = deviceService.getPort(d.id(), pNum); |
| 409 | log.debug("getUplinkPortsOfOlts: Found port: {}", port); |
| 410 | if (port != null) { |
| 411 | cps.add(new ConnectPoint(d.id(), pNum)); |
| 412 | } |
| 413 | } |
| 414 | } |
| 415 | return cps; |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * Returns whether the passed port is the uplink port of the olt device. |
| 420 | */ |
| 421 | private boolean isUplinkPortOfOlt(DeviceId dId, Port p) { |
| 422 | log.debug("isUplinkPortOfOlt: DeviceId: {} Port: {}", dId, p); |
| 423 | // do only for devices for which we are the master |
| 424 | if (!mastershipService.isLocalMaster(dId)) { |
| 425 | return false; |
| 426 | } |
| 427 | |
| 428 | Device d = deviceService.getDevice(dId); |
| 429 | SubscriberAndDeviceInformation deviceInfo = subsService.get(d.serialNumber()); |
| 430 | |
| 431 | if (deviceInfo != null) { |
| 432 | return (deviceInfo.uplinkPort() == p.number().toLong()); |
| 433 | } |
| 434 | |
| 435 | return false; |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * Returns the connectPoint which is the uplink port of the OLT. |
| 440 | */ |
| 441 | private ConnectPoint getUplinkConnectPointOfOlt(DeviceId dId) { |
| 442 | |
| 443 | Device d = deviceService.getDevice(dId); |
| 444 | SubscriberAndDeviceInformation deviceInfo = subsService.get(d.serialNumber()); |
| 445 | log.debug("getUplinkConnectPointOfOlt DeviceId: {} devInfo: {}", dId, deviceInfo); |
| 446 | if (deviceInfo != null) { |
| 447 | PortNumber pNum = PortNumber.portNumber(deviceInfo.uplinkPort()); |
| 448 | Port port = deviceService.getPort(d.id(), pNum); |
| 449 | if (port != null) { |
| 450 | return new ConnectPoint(d.id(), pNum); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | return null; |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * Request DHCP packet from particular connect point via PacketService. |
| 459 | */ |
| 460 | private void requestDhcpPacketsFromConnectPoint(ConnectPoint cp) { |
| 461 | TrafficSelector.Builder selectorServer = DefaultTrafficSelector.builder() |
| 462 | .matchEthType(Ethernet.TYPE_IPV4) |
| 463 | .matchInPort(cp.port()) |
| 464 | .matchIPProtocol(IPv4.PROTOCOL_UDP) |
| 465 | .matchUdpSrc(TpPort.tpPort(UDP.DHCP_SERVER_PORT)); |
| 466 | packetService.requestPackets(selectorServer.build(), |
| 467 | PacketPriority.CONTROL, appId, Optional.of(cp.deviceId())); |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * Cancel DHCP packet from particular connect point via PacketService. |
| 472 | */ |
| 473 | private void cancelDhcpPacketsFromConnectPoint(ConnectPoint cp) { |
| 474 | TrafficSelector.Builder selectorServer = DefaultTrafficSelector.builder() |
| 475 | .matchEthType(Ethernet.TYPE_IPV4) |
| 476 | .matchInPort(cp.port()) |
| 477 | .matchIPProtocol(IPv4.PROTOCOL_UDP) |
| 478 | .matchUdpSrc(TpPort.tpPort(UDP.DHCP_SERVER_PORT)); |
| 479 | packetService.cancelPackets(selectorServer.build(), |
| 480 | PacketPriority.CONTROL, appId, Optional.of(cp.deviceId())); |
| 481 | } |
| 482 | |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 483 | public static Map<String, DhcpAllocationInfo> allocationMap() { |
| 484 | return allocationMap; |
| 485 | } |
| 486 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 487 | private SubscriberAndDeviceInformation getDevice(PacketContext context) { |
| 488 | String serialNo = deviceService.getDevice(context.inPacket(). |
| 489 | receivedFrom().deviceId()).serialNumber(); |
| 490 | |
| 491 | return subsService.get(serialNo); |
| 492 | } |
| 493 | |
| 494 | private SubscriberAndDeviceInformation getDevice(ConnectPoint cp) { |
| 495 | String serialNo = deviceService.getDevice(cp.deviceId()). |
| 496 | serialNumber(); |
| 497 | |
| 498 | return subsService.get(serialNo); |
| 499 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 500 | |
| 501 | private MacAddress relayAgentMacAddress(PacketContext context) { |
| 502 | |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 503 | SubscriberAndDeviceInformation device = this.getDevice(context); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 504 | if (device == null) { |
| 505 | log.warn("Device not found for {}", context.inPacket(). |
| 506 | receivedFrom()); |
| 507 | return null; |
| 508 | } |
| 509 | |
| 510 | return device.hardwareIdentifier(); |
| 511 | } |
| 512 | |
| 513 | private String nasPortId(PacketContext context) { |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 514 | return nasPortId(context.inPacket().receivedFrom()); |
| 515 | } |
| 516 | |
| 517 | private String nasPortId(ConnectPoint cp) { |
| 518 | Port p = deviceService.getPort(cp); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 519 | return p.annotations().value(AnnotationKeys.PORT_NAME); |
| 520 | } |
| 521 | |
| 522 | private SubscriberAndDeviceInformation getSubscriber(PacketContext context) { |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 523 | return subsService.get(nasPortId(context)); |
| 524 | } |
| 525 | |
| 526 | private VlanId cTag(PacketContext context) { |
| 527 | SubscriberAndDeviceInformation sub = getSubscriber(context); |
| 528 | if (sub == null) { |
| 529 | log.warn("Subscriber info not found for {}", context.inPacket(). |
| 530 | receivedFrom()); |
| 531 | return VlanId.NONE; |
| 532 | } |
| 533 | return sub.cTag(); |
| 534 | } |
| 535 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 536 | private VlanId cTag(ConnectPoint cp) { |
| 537 | String portId = nasPortId(cp); |
| 538 | SubscriberAndDeviceInformation sub = subsService.get(portId); |
| 539 | if (sub == null) { |
| 540 | log.warn("Subscriber info not found for {} looking for C-TAG", cp); |
| 541 | return VlanId.NONE; |
| 542 | } |
| 543 | return sub.cTag(); |
| 544 | } |
| 545 | |
| 546 | private VlanId sTag(ConnectPoint cp) { |
| 547 | String portId = nasPortId(cp); |
| 548 | SubscriberAndDeviceInformation sub = subsService.get(portId); |
| 549 | if (sub == null) { |
| 550 | log.warn("Subscriber info not found for {} looking for S-TAG", cp); |
| 551 | return VlanId.NONE; |
| 552 | } |
| 553 | return sub.sTag(); |
| 554 | } |
| 555 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 556 | private VlanId sTag(PacketContext context) { |
| 557 | SubscriberAndDeviceInformation sub = getSubscriber(context); |
| 558 | if (sub == null) { |
| 559 | log.warn("Subscriber info not found for {}", context.inPacket(). |
| 560 | receivedFrom()); |
| 561 | return VlanId.NONE; |
| 562 | } |
| 563 | return sub.sTag(); |
| 564 | } |
| 565 | |
| 566 | private class DhcpRelayPacketProcessor implements PacketProcessor { |
| 567 | |
| 568 | @Override |
| 569 | public void process(PacketContext context) { |
| 570 | if (!configured()) { |
| 571 | log.warn("Missing DHCP relay config. Abort packet processing"); |
| 572 | return; |
| 573 | } |
| 574 | |
| 575 | // process the packet and get the payload |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 576 | Ethernet packet = context.inPacket().parsed(); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 577 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 578 | if (packet == null) { |
| 579 | log.warn("Packet is null"); |
| 580 | return; |
| 581 | } |
| 582 | |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 583 | if (packet.getEtherType() == Ethernet.TYPE_IPV4) { |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 584 | IPv4 ipv4Packet = (IPv4) packet.getPayload(); |
| 585 | |
| 586 | if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_UDP) { |
| 587 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| 588 | if (udpPacket.getSourcePort() == UDP.DHCP_CLIENT_PORT || |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 589 | udpPacket.getSourcePort() == UDP.DHCP_SERVER_PORT) { |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 590 | DHCP dhcpPayload = (DHCP) udpPacket.getPayload(); |
| 591 | //This packet is dhcp. |
| 592 | processDhcpPacket(context, packet, dhcpPayload); |
| 593 | } |
| 594 | } |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | //forward the packet to ConnectPoint where the DHCP server is attached. |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 599 | private void forwardPacket(Ethernet packet, PacketContext context) { |
| 600 | ConnectPoint toSendTo = null; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 601 | |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 602 | if (!useOltUplink) { |
| 603 | toSendTo = dhcpServerConnectPoint.get(); |
| 604 | } else { |
| 605 | toSendTo = getUplinkConnectPointOfOlt(context.inPacket(). |
| 606 | receivedFrom().deviceId()); |
| 607 | } |
| 608 | |
| 609 | if (toSendTo != null) { |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 610 | TrafficTreatment t = DefaultTrafficTreatment.builder() |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 611 | .setOutput(toSendTo.port()).build(); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 612 | OutboundPacket o = new DefaultOutboundPacket( |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 613 | toSendTo.deviceId(), t, |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 614 | ByteBuffer.wrap(packet.serialize())); |
| 615 | if (log.isTraceEnabled()) { |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 616 | log.trace("Relaying packet to dhcp server at {} {}", |
| 617 | toSendTo, packet); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 618 | } |
| 619 | packetService.emit(o); |
| 620 | } else { |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 621 | log.error("No connect point to send msg to DHCP Server"); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 625 | // get the type of the DHCP packet |
| 626 | private DHCPPacketType getDhcpPacketType(DHCP dhcpPayload) { |
| 627 | |
Jonathan Hart | edbf642 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 628 | for (DhcpOption option : dhcpPayload.getOptions()) { |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 629 | if (option.getCode() == OptionCode_MessageType.getValue()) { |
| 630 | byte[] data = option.getData(); |
| 631 | return DHCPPacketType.getType(data[0]); |
| 632 | } |
| 633 | } |
| 634 | return null; |
| 635 | } |
| 636 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 637 | //process the dhcp packet before sending to server |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 638 | private void processDhcpPacket(PacketContext context, Ethernet packet, |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 639 | DHCP dhcpPayload) { |
| 640 | if (dhcpPayload == null) { |
| 641 | log.warn("DHCP payload is null"); |
| 642 | return; |
| 643 | } |
| 644 | |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 645 | DHCPPacketType incomingPacketType = getDhcpPacketType(dhcpPayload); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 646 | |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 647 | log.info("Received DHCP Packet of type {} from {}", |
| 648 | incomingPacketType, context.inPacket().receivedFrom()); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 649 | |
| 650 | switch (incomingPacketType) { |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 651 | case DHCPDISCOVER: |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 652 | Ethernet ethernetPacketDiscover = |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 653 | processDhcpPacketFromClient(context, packet); |
| 654 | if (ethernetPacketDiscover != null) { |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 655 | forwardPacket(ethernetPacketDiscover, context); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 656 | } |
| 657 | break; |
| 658 | case DHCPOFFER: |
| 659 | //reply to dhcp client. |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 660 | Ethernet ethernetPacketOffer = |
| 661 | processDhcpPacketFromServer(context, packet); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 662 | if (ethernetPacketOffer != null) { |
| 663 | sendReply(ethernetPacketOffer, dhcpPayload); |
| 664 | } |
| 665 | break; |
| 666 | case DHCPREQUEST: |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 667 | Ethernet ethernetPacketRequest = |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 668 | processDhcpPacketFromClient(context, packet); |
| 669 | if (ethernetPacketRequest != null) { |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 670 | forwardPacket(ethernetPacketRequest, context); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 671 | } |
| 672 | break; |
| 673 | case DHCPACK: |
| 674 | //reply to dhcp client. |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 675 | Ethernet ethernetPacketAck = |
| 676 | processDhcpPacketFromServer(context, packet); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 677 | if (ethernetPacketAck != null) { |
| 678 | sendReply(ethernetPacketAck, dhcpPayload); |
| 679 | } |
| 680 | break; |
| 681 | default: |
| 682 | break; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 683 | } |
| 684 | } |
| 685 | |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 686 | private Ethernet processDhcpPacketFromClient(PacketContext context, |
| 687 | Ethernet ethernetPacket) { |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 688 | if (log.isTraceEnabled()) { |
| 689 | log.trace("DHCP packet received from client at {} {}", |
| 690 | context.inPacket().receivedFrom(), ethernetPacket); |
| 691 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 692 | |
| 693 | MacAddress relayAgentMac = relayAgentMacAddress(context); |
| 694 | if (relayAgentMac == null) { |
| 695 | log.warn("RelayAgent MAC not found "); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 696 | return null; |
| 697 | } |
| 698 | |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 699 | Ethernet etherReply = ethernetPacket; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 700 | |
| 701 | IPv4 ipv4Packet = (IPv4) etherReply.getPayload(); |
| 702 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| 703 | DHCP dhcpPacket = (DHCP) udpPacket.getPayload(); |
| 704 | |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 705 | if (enableDhcpBroadcastReplies) { |
| 706 | // We want the reply to come back as a L2 broadcast |
| 707 | dhcpPacket.setFlags((short) 0x8000); |
| 708 | } |
| 709 | |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 710 | MacAddress clientMac = MacAddress.valueOf(dhcpPacket.getClientHardwareAddress()); |
| 711 | IpAddress clientIp = IpAddress.valueOf(dhcpPacket.getClientIPAddress()); |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 712 | |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 713 | SubscriberAndDeviceInformation entry = getSubscriber(context); |
| 714 | if (entry == null) { |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 715 | log.warn("Dropping packet as subscriber entry is not available"); |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 716 | return null; |
| 717 | } |
| 718 | |
| 719 | DhcpAllocationInfo info = new DhcpAllocationInfo( |
| 720 | context.inPacket().receivedFrom(), dhcpPacket.getPacketType(), |
| 721 | entry.nasPortId(), clientMac, clientIp); |
| 722 | |
| 723 | allocationMap.put(entry.nasPortId(), info); |
| 724 | |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 725 | post(new DhcpL2RelayEvent(DhcpL2RelayEvent.Type.UPDATED, info, |
| 726 | context.inPacket().receivedFrom())); |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 727 | |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 728 | if (option82) { |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 729 | DHCP dhcpPacketWithOption82 = addOption82(dhcpPacket, entry); |
| 730 | udpPacket.setPayload(dhcpPacketWithOption82); |
| 731 | } |
| 732 | |
| 733 | ipv4Packet.setPayload(udpPacket); |
| 734 | etherReply.setPayload(ipv4Packet); |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 735 | if (modifyClientPktsSrcDstMac) { |
| 736 | etherReply.setSourceMACAddress(relayAgentMac); |
| 737 | etherReply.setDestinationMACAddress(dhcpConnectMac); |
| 738 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 739 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 740 | etherReply.setPriorityCode(ethernetPacket.getPriorityCode()); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 741 | etherReply.setVlanID(cTag(context).toShort()); |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 742 | etherReply.setQinQTPID(Ethernet.TYPE_VLAN); |
| 743 | etherReply.setQinQVID(sTag(context).toShort()); |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 744 | log.info("Finished processing packet.. relaying to dhcpServer"); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 745 | return etherReply; |
| 746 | } |
| 747 | |
| 748 | //build the DHCP offer/ack with proper client port. |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 749 | private Ethernet processDhcpPacketFromServer(PacketContext context, |
| 750 | Ethernet ethernetPacket) { |
| 751 | if (log.isTraceEnabled()) { |
| 752 | log.trace("DHCP packet received from server at {} {}", |
| 753 | context.inPacket().receivedFrom(), ethernetPacket); |
| 754 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 755 | // get dhcp header. |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 756 | Ethernet etherReply = (Ethernet) ethernetPacket.clone(); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 757 | IPv4 ipv4Packet = (IPv4) etherReply.getPayload(); |
| 758 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| 759 | DHCP dhcpPayload = (DHCP) udpPacket.getPayload(); |
| 760 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 761 | MacAddress dstMac = valueOf(dhcpPayload.getClientHardwareAddress()); |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 762 | ConnectPoint subsCp = getConnectPointOfClient(dstMac); |
Amit Ghosh | 2095dc6 | 2017-09-25 20:56:55 +0100 | [diff] [blame] | 763 | // If we can't find the subscriber, can't process further |
| 764 | if (subsCp == null) { |
| 765 | return null; |
| 766 | } |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 767 | // if it's an ACK packet store the information for display purpose |
| 768 | if (getDhcpPacketType(dhcpPayload) == DHCPPacketType.DHCPACK) { |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 769 | |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 770 | String portId = nasPortId(subsCp); |
| 771 | SubscriberAndDeviceInformation sub = subsService.get(portId); |
| 772 | if (sub != null) { |
Jonathan Hart | edbf642 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 773 | List<DhcpOption> options = dhcpPayload.getOptions(); |
| 774 | List<DhcpOption> circuitIds = options.stream() |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 775 | .filter(option -> option.getCode() == DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue()) |
| 776 | .collect(Collectors.toList()); |
| 777 | |
| 778 | String circuitId = "None"; |
| 779 | if (circuitIds.size() == 1) { |
Amit Ghosh | 2095dc6 | 2017-09-25 20:56:55 +0100 | [diff] [blame] | 780 | byte[] array = circuitIds.get(0).getData(); |
| 781 | |
| 782 | try { |
| 783 | // we leave the first two bytes as they are the id and length |
| 784 | circuitId = new String(Arrays.copyOfRange(array, 2, array.length), "UTF-8"); |
| 785 | } catch (Exception e) { } |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | IpAddress ip = IpAddress.valueOf(dhcpPayload.getYourIPAddress()); |
| 789 | |
| 790 | //storeDHCPAllocationInfo |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 791 | DhcpAllocationInfo info = new DhcpAllocationInfo(subsCp, |
| 792 | dhcpPayload.getPacketType(), circuitId, dstMac, ip); |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 793 | |
| 794 | allocationMap.put(sub.nasPortId(), info); |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 795 | |
| 796 | post(new DhcpL2RelayEvent(DhcpL2RelayEvent.Type.UPDATED, info, subsCp)); |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 797 | } |
| 798 | } // end storing of info |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 799 | |
| 800 | // we leave the srcMac from the original packet |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 801 | etherReply.setDestinationMACAddress(dstMac); |
| 802 | etherReply.setQinQVID(sTag(subsCp).toShort()); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 803 | etherReply.setPriorityCode(ethernetPacket.getPriorityCode()); |
| 804 | etherReply.setVlanID((cTag(subsCp).toShort())); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 805 | |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 806 | if (option82) { |
| 807 | udpPacket.setPayload(removeOption82(dhcpPayload)); |
| 808 | } else { |
| 809 | udpPacket.setPayload(dhcpPayload); |
| 810 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 811 | ipv4Packet.setPayload(udpPacket); |
| 812 | etherReply.setPayload(ipv4Packet); |
| 813 | |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 814 | log.info("Finished processing packet.. relaying to client"); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 815 | return etherReply; |
| 816 | } |
| 817 | |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 818 | /* |
| 819 | * Get ConnectPoint of the Client based on it's MAC address |
| 820 | */ |
| 821 | private ConnectPoint getConnectPointOfClient(MacAddress dstMac) { |
| 822 | Set<Host> hosts = hostService.getHostsByMac(dstMac); |
| 823 | if (hosts == null || hosts.isEmpty()) { |
| 824 | log.warn("Cannot determine host for DHCP client: {}. Aborting " |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 825 | + "relay for dhcp packet from server", |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 826 | dstMac); |
| 827 | return null; |
| 828 | } |
| 829 | for (Host h : hosts) { |
| 830 | // if more than one, |
| 831 | // find the connect point which has an valid entry in SADIS |
| 832 | ConnectPoint cp = new ConnectPoint(h.location().deviceId(), |
| 833 | h.location().port()); |
| 834 | |
| 835 | if (sTag(cp) != VlanId.NONE) { |
| 836 | return cp; |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | return null; |
| 841 | } |
| 842 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 843 | //send the response to the requester host. |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 844 | private void sendReply(Ethernet ethPacket, DHCP dhcpPayload) { |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 845 | MacAddress descMac = valueOf(dhcpPayload.getClientHardwareAddress()); |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 846 | ConnectPoint subCp = getConnectPointOfClient(descMac); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 847 | |
| 848 | // Send packet out to requester if the host information is available |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 849 | if (subCp != null) { |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 850 | log.info("Sending DHCP packet to client at {}", subCp); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 851 | TrafficTreatment t = DefaultTrafficTreatment.builder() |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 852 | .setOutput(subCp.port()).build(); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 853 | OutboundPacket o = new DefaultOutboundPacket( |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 854 | subCp.deviceId(), t, ByteBuffer.wrap(ethPacket.serialize())); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 855 | if (log.isTraceEnabled()) { |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 856 | log.trace("Relaying packet to dhcp client at {} {}", subCp, |
| 857 | ethPacket); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 858 | } |
| 859 | packetService.emit(o); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 860 | } else { |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 861 | log.error("Dropping DHCP packet because can't find host for {}", descMac); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 862 | } |
| 863 | } |
| 864 | } |
| 865 | |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 866 | private DHCP addOption82(DHCP dhcpPacket, SubscriberAndDeviceInformation entry) { |
| 867 | log.debug("option82data {} ", entry); |
| 868 | |
Jonathan Hart | edbf642 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 869 | List<DhcpOption> options = Lists.newArrayList(dhcpPacket.getOptions()); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 870 | DhcpOption82 option82 = new DhcpOption82(); |
| 871 | option82.setAgentCircuitId(entry.circuitId()); |
| 872 | option82.setAgentRemoteId(entry.remoteId()); |
Jonathan Hart | edbf642 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 873 | DhcpOption option = new DhcpOption() |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 874 | .setCode(DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue()) |
| 875 | .setData(option82.toByteArray()) |
| 876 | .setLength(option82.length()); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 877 | |
| 878 | options.add(options.size() - 1, option); |
| 879 | dhcpPacket.setOptions(options); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 880 | |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 881 | return dhcpPacket; |
| 882 | |
| 883 | } |
| 884 | |
| 885 | private DHCP removeOption82(DHCP dhcpPacket) { |
Jonathan Hart | edbf642 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 886 | List<DhcpOption> options = dhcpPacket.getOptions(); |
| 887 | List<DhcpOption> newoptions = options.stream() |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 888 | .filter(option -> option.getCode() != DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue()) |
| 889 | .collect(Collectors.toList()); |
| 890 | |
| 891 | return dhcpPacket.setOptions(newoptions); |
| 892 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 893 | /** |
| 894 | * Listener for network config events. |
| 895 | */ |
| 896 | private class InternalConfigListener implements NetworkConfigListener { |
| 897 | |
| 898 | @Override |
| 899 | public void event(NetworkConfigEvent event) { |
| 900 | |
| 901 | if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED || |
| 902 | event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) && |
| 903 | event.configClass().equals(DhcpL2RelayConfig.class)) { |
| 904 | updateConfig(); |
| 905 | log.info("Reconfigured"); |
| 906 | } |
| 907 | } |
| 908 | } |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 909 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 910 | /** |
| 911 | * Handles Mastership changes for the devices which connect |
| 912 | * to the DHCP server. |
| 913 | */ |
| 914 | private class InnerMastershipListener implements MastershipListener { |
| 915 | @Override |
| 916 | public void event(MastershipEvent event) { |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 917 | if (!useOltUplink) { |
| 918 | if (dhcpServerConnectPoint.get() != null && |
| 919 | dhcpServerConnectPoint.get().deviceId(). |
| 920 | equals(event.subject())) { |
| 921 | log.trace("Mastership Event recevived for {}", event.subject()); |
| 922 | // mastership of the device for our connect point has changed |
| 923 | // reselect |
| 924 | selectServerConnectPoint(); |
| 925 | } |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 926 | } |
| 927 | } |
| 928 | } |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 929 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 930 | /** |
| 931 | * Handles Device status change for the devices which connect |
| 932 | * to the DHCP server. |
| 933 | */ |
| 934 | private class InnerDeviceListener implements DeviceListener { |
| 935 | @Override |
| 936 | public void event(DeviceEvent event) { |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 937 | if (log.isTraceEnabled() && |
| 938 | !event.type().equals(DeviceEvent.Type.PORT_STATS_UPDATED)) { |
| 939 | log.trace("Device Event received for {} event {}", |
| 940 | event.subject(), event.type()); |
| 941 | } |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 942 | if (!useOltUplink) { |
| 943 | if (dhcpServerConnectPoint.get() == null) { |
| 944 | switch (event.type()) { |
| 945 | case DEVICE_ADDED: |
| 946 | case DEVICE_AVAILABILITY_CHANGED: |
| 947 | // some device is available check if we can get one |
| 948 | selectServerConnectPoint(); |
| 949 | break; |
| 950 | default: |
| 951 | break; |
| 952 | } |
| 953 | return; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 954 | } |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 955 | if (dhcpServerConnectPoint.get().deviceId(). |
| 956 | equals(event.subject().id())) { |
| 957 | switch (event.type()) { |
| 958 | case DEVICE_AVAILABILITY_CHANGED: |
| 959 | case DEVICE_REMOVED: |
| 960 | case DEVICE_SUSPENDED: |
| 961 | // state of our device has changed, check if we need |
| 962 | // to re-select |
| 963 | selectServerConnectPoint(); |
| 964 | break; |
| 965 | default: |
| 966 | break; |
| 967 | } |
| 968 | } |
| 969 | } else { |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 970 | switch (event.type()) { |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 971 | case PORT_ADDED: |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 972 | if (useOltUplink && isUplinkPortOfOlt(event.subject().id(), event.port())) { |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 973 | requestDhcpPacketsFromConnectPoint(new ConnectPoint(event.subject().id(), |
| 974 | event.port().number())); |
| 975 | } |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 976 | break; |
| 977 | default: |
| 978 | break; |
| 979 | } |
| 980 | } |
| 981 | } |
| 982 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 983 | } |