Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.openstacknetworking.routing; |
| 17 | |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame^] | 18 | import com.google.common.collect.Lists; |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 19 | import org.onlab.packet.ARP; |
| 20 | import org.onlab.packet.EthType; |
| 21 | import org.onlab.packet.Ethernet; |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 22 | import org.onlab.packet.Ip4Address; |
| 23 | import org.onlab.packet.IpAddress; |
| 24 | import org.onlab.packet.MacAddress; |
| 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 28 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 29 | import org.onosproject.net.flow.TrafficSelector; |
| 30 | import org.onosproject.net.flow.TrafficTreatment; |
| 31 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 32 | import org.onosproject.net.packet.PacketContext; |
| 33 | import org.onosproject.net.packet.PacketPriority; |
| 34 | import org.onosproject.net.packet.PacketService; |
| 35 | import org.onosproject.openstackinterface.OpenstackInterfaceService; |
| 36 | import org.onosproject.openstackinterface.OpenstackPort; |
sangho | 4890754 | 2016-03-28 16:07:07 +0900 | [diff] [blame] | 37 | import org.onosproject.openstacknetworking.OpenstackNetworkingConfig; |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame^] | 38 | import org.onosproject.scalablegateway.api.ScalableGatewayService; |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 39 | import org.slf4j.Logger; |
| 40 | |
| 41 | import java.nio.ByteBuffer; |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame^] | 42 | import java.util.List; |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 43 | import java.util.Optional; |
| 44 | |
| 45 | import static com.google.common.base.Preconditions.checkNotNull; |
| 46 | import static org.slf4j.LoggerFactory.getLogger; |
| 47 | |
| 48 | /** |
| 49 | * Handle ARP packet sent from Openstack Gateway nodes. |
| 50 | */ |
| 51 | public class OpenstackRoutingArpHandler { |
| 52 | protected final Logger log = getLogger(getClass()); |
| 53 | |
| 54 | private final PacketService packetService; |
| 55 | private final OpenstackInterfaceService openstackService; |
sangho | 4890754 | 2016-03-28 16:07:07 +0900 | [diff] [blame] | 56 | private final OpenstackNetworkingConfig config; |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame^] | 57 | private final ScalableGatewayService gatewayService; |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 58 | private static final String NETWORK_ROUTER_GATEWAY = "network:router_gateway"; |
hyungseo Ryu | 38b5f18 | 2016-06-14 16:42:27 +0900 | [diff] [blame] | 59 | private static final String NETWORK_FLOATING_IP = "network:floatingip"; |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * Default constructor. |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame^] | 63 | * @param packetService packet service |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 64 | * @param openstackService openstackInterface service |
| 65 | * @param config openstackRoutingConfig |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame^] | 66 | * @param gatewayService |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 67 | */ |
| 68 | OpenstackRoutingArpHandler(PacketService packetService, OpenstackInterfaceService openstackService, |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame^] | 69 | OpenstackNetworkingConfig config, ScalableGatewayService gatewayService) { |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 70 | this.packetService = packetService; |
| 71 | this.openstackService = checkNotNull(openstackService); |
| 72 | this.config = checkNotNull(config); |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame^] | 73 | this.gatewayService = gatewayService; |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Requests ARP packet to GatewayNode. |
| 78 | * |
| 79 | * @param appId application id |
| 80 | */ |
| 81 | public void requestPacket(ApplicationId appId) { |
| 82 | |
| 83 | TrafficSelector arpSelector = DefaultTrafficSelector.builder() |
| 84 | .matchEthType(EthType.EtherType.ARP.ethType().toShort()) |
| 85 | .build(); |
| 86 | |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame^] | 87 | getExternalInfo().forEach(deviceId -> |
| 88 | packetService.requestPackets(arpSelector, |
| 89 | PacketPriority.CONTROL, |
| 90 | appId, |
| 91 | Optional.of(deviceId)) |
| 92 | ); |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Handles ARP packet. |
| 97 | * |
| 98 | * @param context packet context |
| 99 | * @param ethernet ethernet |
| 100 | */ |
| 101 | public void processArpPacketFromRouter(PacketContext context, Ethernet ethernet) { |
| 102 | checkNotNull(context, "context can not be null"); |
| 103 | checkNotNull(ethernet, "ethernet can not be null"); |
| 104 | |
Daniel Park | f78306f | 2016-04-06 17:57:48 +0900 | [diff] [blame] | 105 | |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 106 | ARP arp = (ARP) ethernet.getPayload(); |
| 107 | |
Daniel Park | f78306f | 2016-04-06 17:57:48 +0900 | [diff] [blame] | 108 | log.debug("arpEvent called from {} to {}", |
| 109 | Ip4Address.valueOf(arp.getSenderProtocolAddress()).toString(), |
| 110 | Ip4Address.valueOf(arp.getTargetProtocolAddress()).toString()); |
| 111 | |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 112 | if (arp.getOpCode() != ARP.OP_REQUEST) { |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | IpAddress targetIp = Ip4Address.valueOf(arp.getTargetProtocolAddress()); |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 117 | |
hyungseo Ryu | 38b5f18 | 2016-06-14 16:42:27 +0900 | [diff] [blame] | 118 | if (getTargetMacForTargetIp(targetIp.getIp4Address()) == MacAddress.NONE) { |
| 119 | return; |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 120 | } |
hyungseo Ryu | 38b5f18 | 2016-06-14 16:42:27 +0900 | [diff] [blame] | 121 | MacAddress targetMac = MacAddress.valueOf(config.gatewayExternalInterfaceMac()); |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 122 | |
| 123 | Ethernet ethReply = ARP.buildArpReply(targetIp.getIp4Address(), |
| 124 | targetMac, ethernet); |
| 125 | |
| 126 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 127 | .setOutput(context.inPacket().receivedFrom().port()) |
| 128 | .build(); |
| 129 | |
| 130 | packetService.emit(new DefaultOutboundPacket( |
| 131 | context.inPacket().receivedFrom().deviceId(), |
| 132 | treatment, |
| 133 | ByteBuffer.wrap(ethReply.serialize()))); |
| 134 | } |
| 135 | |
| 136 | private MacAddress getTargetMacForTargetIp(Ip4Address targetIp) { |
| 137 | OpenstackPort port = openstackService.ports().stream() |
hyungseo Ryu | 38b5f18 | 2016-06-14 16:42:27 +0900 | [diff] [blame] | 138 | .filter(p -> p.deviceOwner().equals(NETWORK_ROUTER_GATEWAY) || |
| 139 | p.deviceOwner().equals(NETWORK_FLOATING_IP)) |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 140 | .filter(p -> p.fixedIps().containsValue(targetIp.getIp4Address())) |
| 141 | .findAny().orElse(null); |
| 142 | |
| 143 | if (port == null) { |
| 144 | return MacAddress.NONE; |
| 145 | } |
| 146 | return port.macAddress(); |
| 147 | } |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame^] | 148 | |
| 149 | private List<DeviceId> getExternalInfo() { |
| 150 | List<DeviceId> externalInfoList = Lists.newArrayList(); |
| 151 | gatewayService.getGatewayDeviceIds().forEach(externalInfoList::add); |
| 152 | return externalInfoList; |
| 153 | } |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 154 | } |