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; |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 37 | import org.onosproject.scalablegateway.api.ScalableGatewayService; |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame] | 38 | import org.onosproject.openstacknetworking.Constants; |
| 39 | import org.onosproject.openstacknode.OpenstackNodeService; |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 40 | import org.slf4j.Logger; |
| 41 | |
| 42 | import java.nio.ByteBuffer; |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 43 | import java.util.List; |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 44 | import java.util.Optional; |
| 45 | |
| 46 | import static com.google.common.base.Preconditions.checkNotNull; |
| 47 | import static org.slf4j.LoggerFactory.getLogger; |
| 48 | |
| 49 | /** |
| 50 | * Handle ARP packet sent from Openstack Gateway nodes. |
| 51 | */ |
| 52 | public class OpenstackRoutingArpHandler { |
| 53 | protected final Logger log = getLogger(getClass()); |
| 54 | |
| 55 | private final PacketService packetService; |
| 56 | private final OpenstackInterfaceService openstackService; |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 57 | private final ScalableGatewayService gatewayService; |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame] | 58 | private final OpenstackNodeService nodeService; |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 59 | private static final String NETWORK_ROUTER_GATEWAY = "network:router_gateway"; |
hyungseo Ryu | 38b5f18 | 2016-06-14 16:42:27 +0900 | [diff] [blame] | 60 | private static final String NETWORK_FLOATING_IP = "network:floatingip"; |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 61 | |
| 62 | /** |
| 63 | * Default constructor. |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame] | 64 | * |
| 65 | * @param packetService packet service |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 66 | * @param openstackService openstackInterface service |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame] | 67 | * @param gatewayService gateway service |
| 68 | * @param nodeService openstackNodeService |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 69 | */ |
| 70 | OpenstackRoutingArpHandler(PacketService packetService, OpenstackInterfaceService openstackService, |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame] | 71 | OpenstackNodeService nodeService, ScalableGatewayService gatewayService) { |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 72 | this.packetService = packetService; |
| 73 | this.openstackService = checkNotNull(openstackService); |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame] | 74 | this.nodeService = nodeService; |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 75 | this.gatewayService = gatewayService; |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Requests ARP packet to GatewayNode. |
| 80 | * |
| 81 | * @param appId application id |
| 82 | */ |
| 83 | public void requestPacket(ApplicationId appId) { |
| 84 | |
| 85 | TrafficSelector arpSelector = DefaultTrafficSelector.builder() |
| 86 | .matchEthType(EthType.EtherType.ARP.ethType().toShort()) |
| 87 | .build(); |
| 88 | |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 89 | getExternalInfo().forEach(deviceId -> |
| 90 | packetService.requestPackets(arpSelector, |
| 91 | PacketPriority.CONTROL, |
| 92 | appId, |
| 93 | Optional.of(deviceId)) |
| 94 | ); |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Handles ARP packet. |
| 99 | * |
| 100 | * @param context packet context |
| 101 | * @param ethernet ethernet |
| 102 | */ |
| 103 | public void processArpPacketFromRouter(PacketContext context, Ethernet ethernet) { |
| 104 | checkNotNull(context, "context can not be null"); |
| 105 | checkNotNull(ethernet, "ethernet can not be null"); |
| 106 | |
Daniel Park | f78306f | 2016-04-06 17:57:48 +0900 | [diff] [blame] | 107 | |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 108 | ARP arp = (ARP) ethernet.getPayload(); |
| 109 | |
Daniel Park | f78306f | 2016-04-06 17:57:48 +0900 | [diff] [blame] | 110 | log.debug("arpEvent called from {} to {}", |
| 111 | Ip4Address.valueOf(arp.getSenderProtocolAddress()).toString(), |
| 112 | Ip4Address.valueOf(arp.getTargetProtocolAddress()).toString()); |
| 113 | |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 114 | if (arp.getOpCode() != ARP.OP_REQUEST) { |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | IpAddress targetIp = Ip4Address.valueOf(arp.getTargetProtocolAddress()); |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 119 | |
hyungseo Ryu | 38b5f18 | 2016-06-14 16:42:27 +0900 | [diff] [blame] | 120 | if (getTargetMacForTargetIp(targetIp.getIp4Address()) == MacAddress.NONE) { |
| 121 | return; |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 122 | } |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame] | 123 | // FIXME: Set the correct gateway |
| 124 | MacAddress targetMac = Constants.GW_EXT_INT_MAC; |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 125 | |
| 126 | Ethernet ethReply = ARP.buildArpReply(targetIp.getIp4Address(), |
| 127 | targetMac, ethernet); |
| 128 | |
| 129 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 130 | .setOutput(context.inPacket().receivedFrom().port()) |
| 131 | .build(); |
| 132 | |
| 133 | packetService.emit(new DefaultOutboundPacket( |
| 134 | context.inPacket().receivedFrom().deviceId(), |
| 135 | treatment, |
| 136 | ByteBuffer.wrap(ethReply.serialize()))); |
| 137 | } |
| 138 | |
| 139 | private MacAddress getTargetMacForTargetIp(Ip4Address targetIp) { |
| 140 | OpenstackPort port = openstackService.ports().stream() |
hyungseo Ryu | 38b5f18 | 2016-06-14 16:42:27 +0900 | [diff] [blame] | 141 | .filter(p -> p.deviceOwner().equals(NETWORK_ROUTER_GATEWAY) || |
| 142 | p.deviceOwner().equals(NETWORK_FLOATING_IP)) |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 143 | .filter(p -> p.fixedIps().containsValue(targetIp.getIp4Address())) |
| 144 | .findAny().orElse(null); |
| 145 | |
| 146 | if (port == null) { |
| 147 | return MacAddress.NONE; |
| 148 | } |
| 149 | return port.macAddress(); |
| 150 | } |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 151 | |
| 152 | private List<DeviceId> getExternalInfo() { |
| 153 | List<DeviceId> externalInfoList = Lists.newArrayList(); |
| 154 | gatewayService.getGatewayDeviceIds().forEach(externalInfoList::add); |
| 155 | return externalInfoList; |
| 156 | } |
Daniel Park | 81a61a1 | 2016-02-26 08:24:44 +0900 | [diff] [blame] | 157 | } |