blob: 40eed7f3eaad2ec15e2f3939d39f9e99b0ba9182 [file] [log] [blame]
sangho0c2a3da2016-02-16 13:39:07 +09001/*
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 */
16package org.onosproject.openstacknetworking.routing;
17
18import org.onlab.packet.Ethernet;
19import org.onlab.packet.IPv4;
20import org.onlab.packet.MacAddress;
21import org.onlab.packet.TCP;
22import org.onlab.packet.UDP;
23import org.onosproject.net.DeviceId;
24import org.onosproject.net.PortNumber;
25import org.onosproject.net.flow.DefaultTrafficTreatment;
26import org.onosproject.net.flow.TrafficTreatment;
27import org.onosproject.net.packet.DefaultOutboundPacket;
28import org.onosproject.net.packet.InboundPacket;
29import org.onosproject.net.packet.PacketContext;
30import org.onosproject.net.packet.PacketService;
31import org.onosproject.openstacknetworking.OpenstackPort;
32import org.slf4j.Logger;
33import org.slf4j.LoggerFactory;
34
35import java.nio.ByteBuffer;
36
37import static com.google.common.base.Preconditions.checkNotNull;
38
39
40/**
41 * Handle NAT packet processing for Managing Flow Rules In Openstack Nodes.
42 */
43public 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}