2 * Copyright (c) 2014, 2015 Red Hat, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.services;
11 import java.math.BigInteger;
12 import java.net.InetAddress;
13 import java.net.Inet6Address;
14 import java.net.UnknownHostException;
15 import java.util.List;
17 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
18 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
19 import org.opendaylight.ovsdb.openstack.netvirt.api.InboundNatProvider;
20 import org.opendaylight.ovsdb.openstack.netvirt.api.Status;
21 import org.opendaylight.ovsdb.openstack.netvirt.api.StatusCode;
22 import org.opendaylight.ovsdb.openstack.netvirt.providers.ConfigInterface;
23 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.AbstractServiceInstance;
24 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.OF13Provider;
25 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.Service;
26 import org.opendaylight.ovsdb.utils.mdsal.openflow.ActionUtils;
27 import org.opendaylight.ovsdb.utils.mdsal.openflow.InstructionUtils;
28 import org.opendaylight.ovsdb.utils.mdsal.openflow.MatchUtils;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
41 import com.google.common.collect.Lists;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg3;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxRegCaseBuilder;
45 import org.osgi.framework.BundleContext;
46 import org.osgi.framework.ServiceReference;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
50 public class InboundNatService extends AbstractServiceInstance implements ConfigInterface, InboundNatProvider {
51 private static final Logger LOG = LoggerFactory.getLogger(InboundNatService.class);
52 public static final Class<? extends NxmNxReg> REG_FIELD = NxmNxReg3.class;
54 public InboundNatService() {
55 super(Service.INBOUND_NAT);
58 public InboundNatService(Service service) {
63 public Status programIpRewriteRule(Long dpid, Long inPort, String destSegId, InetAddress matchAddress,
64 InetAddress rewriteAddress, Action action) {
65 String nodeName = Constants.OPENFLOW_NODE_PREFIX + dpid;
67 MatchBuilder matchBuilder = new MatchBuilder();
68 NodeBuilder nodeBuilder = OF13Provider.createNodeBuilder(nodeName);
70 // Instructions List Stores Individual Instructions
71 InstructionsBuilder isb = new InstructionsBuilder();
72 List<Instruction> instructions = Lists.newArrayList();
73 InstructionBuilder ib = new InstructionBuilder();
75 MatchUtils.createInPortMatch(matchBuilder, dpid, inPort);
76 MatchUtils.createDstL3IPv4Match(matchBuilder, MatchUtils.iPv4PrefixFromIPv4Address(matchAddress.getHostAddress()));
78 // Set register to indicate that rewrite took place
79 ActionBuilder actionBuilder = new ActionBuilder();
80 actionBuilder.setAction(ActionUtils.nxLoadRegAction(new DstNxRegCaseBuilder().setNxReg(REG_FIELD).build(),
81 new BigInteger(destSegId)));
83 // Set Dest IP address and set REG_FIELD
84 InstructionUtils.createNwDstInstructions(ib,
85 MatchUtils.iPv4PrefixFromIPv4Address(rewriteAddress.getHostAddress()), actionBuilder);
87 ib.setKey(new InstructionKey(0));
88 instructions.add(ib.build());
91 ib = getMutablePipelineInstructionBuilder();
93 ib.setKey(new InstructionKey(1));
94 instructions.add(ib.build());
96 FlowBuilder flowBuilder = new FlowBuilder();
97 flowBuilder.setMatch(matchBuilder.build());
98 flowBuilder.setInstructions(isb.setInstruction(instructions).build());
100 String flowId = "InboundNAT_" + inPort + "_" + destSegId + "_" + matchAddress.getHostAddress();
101 flowBuilder.setId(new FlowId(flowId));
102 FlowKey key = new FlowKey(new FlowId(flowId));
103 flowBuilder.setBarrier(true);
104 flowBuilder.setTableId(this.getTable());
105 flowBuilder.setKey(key);
106 flowBuilder.setPriority(1024);
107 flowBuilder.setFlowName(flowId);
108 flowBuilder.setHardTimeout(0);
109 flowBuilder.setIdleTimeout(0);
111 if (action.equals(Action.ADD)) {
112 writeFlow(flowBuilder, nodeBuilder);
114 removeFlow(flowBuilder, nodeBuilder);
117 // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
118 return new Status(StatusCode.SUCCESS);
122 public Status programIpRewriteExclusion(Long dpid, String segmentationId, String excludedCidr,
124 String nodeName = Constants.OPENFLOW_NODE_PREFIX + dpid;
126 MatchBuilder matchBuilder = new MatchBuilder();
127 NodeBuilder nodeBuilder = OF13Provider.createNodeBuilder(nodeName);
129 // Instructions List Stores Individual Instructions
130 InstructionsBuilder isb = new InstructionsBuilder();
131 List<Instruction> instructions = Lists.newArrayList();
132 InstructionBuilder ib;
134 MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId));
135 String ipAddress = excludedCidr.substring(0, excludedCidr.indexOf("/"));
136 InetAddress inetAddress;
138 inetAddress = InetAddress.getByName(ipAddress);
139 } catch (UnknownHostException e) {
140 return new Status(StatusCode.BADREQUEST);
142 if (inetAddress instanceof Inet6Address) {
143 // WORKAROUND: For now ipv6 is not supported
144 // TODO: implement ipv6 cidr case
145 LOG.debug("ipv6 cidr is not implemented yet. cidr {}",
147 return new Status(StatusCode.NOTIMPLEMENTED);
149 MatchUtils.createDstL3IPv4Match(matchBuilder, new Ipv4Prefix(excludedCidr));
152 ib = getMutablePipelineInstructionBuilder();
154 ib.setKey(new InstructionKey(0));
155 instructions.add(ib.build());
157 FlowBuilder flowBuilder = new FlowBuilder();
158 flowBuilder.setMatch(matchBuilder.build());
159 flowBuilder.setInstructions(isb.setInstruction(instructions).build());
161 String flowId = "InboundNATExclusion_" + segmentationId + "_" + excludedCidr;
162 flowBuilder.setId(new FlowId(flowId));
163 FlowKey key = new FlowKey(new FlowId(flowId));
164 flowBuilder.setBarrier(true);
165 flowBuilder.setTableId(this.getTable());
166 flowBuilder.setKey(key);
167 flowBuilder.setPriority(1024);
168 flowBuilder.setFlowName(flowId);
169 flowBuilder.setHardTimeout(0);
170 flowBuilder.setIdleTimeout(0);
172 if (action.equals(Action.ADD)) {
173 writeFlow(flowBuilder, nodeBuilder);
175 removeFlow(flowBuilder, nodeBuilder);
178 // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
179 return new Status(StatusCode.SUCCESS);
183 public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
184 super.setDependencies(bundleContext.getServiceReference(InboundNatProvider.class.getName()), this);
188 public void setDependencies(Object impl) {}