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