sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 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 | |
| 18 | import org.onlab.packet.Ethernet; |
| 19 | import org.onlab.packet.IPv4; |
| 20 | import org.onlab.packet.MacAddress; |
| 21 | import org.onlab.packet.TCP; |
| 22 | import org.onlab.packet.UDP; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.net.PortNumber; |
| 25 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 26 | import org.onosproject.net.flow.TrafficTreatment; |
| 27 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 28 | import org.onosproject.net.packet.InboundPacket; |
| 29 | import org.onosproject.net.packet.PacketContext; |
| 30 | import org.onosproject.net.packet.PacketService; |
| 31 | import org.onosproject.openstacknetworking.OpenstackPort; |
| 32 | import org.slf4j.Logger; |
| 33 | import org.slf4j.LoggerFactory; |
| 34 | |
| 35 | import java.nio.ByteBuffer; |
| 36 | |
| 37 | import static com.google.common.base.Preconditions.checkNotNull; |
| 38 | |
| 39 | |
| 40 | /** |
| 41 | * Handle NAT packet processing for Managing Flow Rules In Openstack Nodes. |
| 42 | */ |
| 43 | public class OpenstackPnatHandler implements Runnable { |
| 44 | |
| 45 | volatile PacketContext context; |
| 46 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 47 | |
| 48 | protected PacketService packetService; |
| 49 | |
| 50 | private final OpenstackRoutingRulePopulator rulePopulator; |
| 51 | private final int portNum; |
| 52 | private final OpenstackPort openstackPort; |
| 53 | |
| 54 | OpenstackPnatHandler(OpenstackRoutingRulePopulator rulePopulator, PacketContext context, |
| 55 | int portNum, OpenstackPort openstackPort) { |
| 56 | this.rulePopulator = checkNotNull(rulePopulator); |
| 57 | this.context = checkNotNull(context); |
| 58 | this.portNum = checkNotNull(portNum); |
| 59 | this.openstackPort = checkNotNull(openstackPort); |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public void run() { |
| 64 | InboundPacket inboundPacket = context.inPacket(); |
| 65 | Ethernet ethernet = checkNotNull(inboundPacket.parsed()); |
| 66 | |
| 67 | //TODO: Considers IPV6 |
| 68 | if (ethernet.getEtherType() != Ethernet.TYPE_IPV4) { |
| 69 | log.warn("Now, we just consider IP version 4"); |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | packetOut(inboundPacket, portNum); |
| 74 | |
| 75 | rulePopulator.populatePnatFlowRules(inboundPacket, openstackPort, portNum, |
| 76 | getExternalInterfaceMacAddress(), getExternalRouterMacAddress()); |
| 77 | } |
| 78 | |
| 79 | private void packetOut(InboundPacket inboundPacket, int portNum) { |
| 80 | Ethernet ethernet = checkNotNull(inboundPacket.parsed()); |
| 81 | IPv4 iPacket = (IPv4) ethernet.getPayload(); |
| 82 | |
| 83 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 84 | |
| 85 | switch (iPacket.getProtocol()) { |
| 86 | case IPv4.PROTOCOL_TCP: |
| 87 | TCP tcpPacket = (TCP) iPacket.getPayload(); |
| 88 | tcpPacket.setSourcePort(portNum); |
| 89 | tcpPacket.resetChecksum(); |
| 90 | tcpPacket.setParent(iPacket); |
| 91 | iPacket.setPayload(tcpPacket); |
| 92 | break; |
| 93 | case IPv4.PROTOCOL_UDP: |
| 94 | UDP udpPacket = (UDP) iPacket.getPayload(); |
| 95 | udpPacket.setSourcePort(portNum); |
| 96 | udpPacket.resetChecksum(); |
| 97 | udpPacket.setParent(iPacket); |
| 98 | iPacket.setPayload(udpPacket); |
| 99 | break; |
| 100 | default: |
| 101 | break; |
| 102 | } |
| 103 | |
| 104 | iPacket.resetChecksum(); |
| 105 | iPacket.setPayload(ethernet); |
| 106 | ethernet.setSourceMACAddress(getExternalInterfaceMacAddress()) |
| 107 | .setDestinationMACAddress(getExternalRouterMacAddress()); |
| 108 | ethernet.resetChecksum(); |
| 109 | |
| 110 | treatment.setOutput(getExternalPort(inboundPacket.receivedFrom().deviceId())); |
| 111 | |
| 112 | packetService.emit(new DefaultOutboundPacket(inboundPacket.receivedFrom().deviceId(), |
| 113 | treatment.build(), ByteBuffer.wrap(ethernet.serialize()))); |
| 114 | } |
| 115 | |
| 116 | private PortNumber getExternalPort(DeviceId deviceId) { |
| 117 | // TODO |
| 118 | return null; |
| 119 | } |
| 120 | private MacAddress getExternalInterfaceMacAddress() { |
| 121 | // TODO |
| 122 | return null; |
| 123 | } |
| 124 | private MacAddress getExternalRouterMacAddress() { |
| 125 | // TODO |
| 126 | return null; |
| 127 | } |
| 128 | } |