2 * Copyright (c) 2014 Cisco Systems, 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.groupbasedpolicy.renderer.ofoverlay.flow;
11 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.ARP;
12 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.applyActionIns;
13 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.ethernetMatch;
14 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.gotoTableIns;
15 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.instructions;
16 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.nxLoadArpOpAction;
17 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.nxLoadArpShaAction;
18 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.nxLoadArpSpaAction;
19 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.nxLoadRegAction;
20 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.nxLoadTunIdAction;
21 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.nxMoveArpShaToArpThaAction;
22 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.nxMoveArpSpaToArpTpaAction;
23 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.nxMoveEthSrcToEthDstAction;
24 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.outputAction;
25 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.setDlDstAction;
26 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.setDlSrcAction;
27 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.setIpv4DstAction;
28 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.setIpv6DstAction;
30 import java.math.BigInteger;
31 import java.util.Collection;
32 import java.util.List;
34 import org.opendaylight.groupbasedpolicy.endpoint.EpKey;
35 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfWriter;
36 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
37 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.OrdinalFactory.EndpointFwdCtxOrdinals;
38 import org.opendaylight.groupbasedpolicy.resolver.PolicyInfo;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.napt.translations.fields.napt.translations.NaptTranslation;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer3Match;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatchBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6MatchBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg1;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg4;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg5;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg6;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
66 * Manage the table that assigns source endpoint group, bridge domain, and
67 * router domain to registers to be used by other tables.
69 public class IngressNatMapper extends FlowTable {
71 protected static final Logger LOG = LoggerFactory.getLogger(IngressNatMapper.class);
73 // TODO Li alagalah Improve UT coverage for this class.
74 public static short TABLE_ID;
76 public IngressNatMapper(OfContext ctx, short tableId) {
82 public short getTableId() {
87 public void sync(NodeId nodeId, PolicyInfo policyInfo, OfWriter ofWriter) throws Exception {
89 ofWriter.writeFlow(nodeId, TABLE_ID, dropFlow(Integer.valueOf(1), null, TABLE_ID));
91 // TODO Bug 3546 - Difficult: External port is unrelated to Tenant, L3C, L2BD..
93 Collection<Endpoint> endpointsForNode = ctx.getEndpointManager().getEndpointsForNode(nodeId);
94 Collection<EndpointL3> l3Endpoints = ctx.getEndpointManager().getL3EndpointsWithNat();
95 for (EndpointL3 l3Ep : l3Endpoints) {
96 if (l3Ep.getL2Context() != null && l3Ep.getMacAddress() !=null ) {
97 Endpoint ep = ctx.getEndpointManager().getEndpoint(new EpKey(l3Ep.getL2Context(), l3Ep.getMacAddress()));
98 if (endpointsForNode.contains(ep)) {
99 createNatFlow(l3Ep, nodeId, ofWriter, policyInfo);
105 private void createNatFlow(EndpointL3 l3Ep, NodeId nodeId, OfWriter ofWriter, PolicyInfo policyInfo) throws Exception {
106 List<NaptTranslation> naptAugL3Endpoint = ctx.getEndpointManager().getNaptAugL3Endpoint(l3Ep);
107 // Match on L3 Nat Augmentation in Destination, set to IPAddress/Mac, send to SourceMapper
108 if (naptAugL3Endpoint == null) {
112 for (NaptTranslation nat : naptAugL3Endpoint) {
113 Endpoint ep = ctx.getEndpointManager().getEndpoint(new EpKey(l3Ep.getL2Context(), l3Ep.getMacAddress()));
114 EndpointFwdCtxOrdinals epFwdCtxOrds = OrdinalFactory.getEndpointFwdCtxOrdinals(ctx, policyInfo, ep);
115 if (epFwdCtxOrds == null) {
116 LOG.debug("getEndpointFwdCtxOrdinals is null for EP {}", ep);
121 flow = buildNatFlow(nat.getIpAddress(), l3Ep.getIpAddress(), l3Ep.getMacAddress(), epFwdCtxOrds);
123 ofWriter.writeFlow(nodeId, TABLE_ID, flow);
125 flow = createOutsideArpFlow(nat.getIpAddress(), l3Ep.getMacAddress(), nodeId);
127 ofWriter.writeFlow(nodeId, TABLE_ID, flow);
134 private Flow buildNatFlow(IpAddress outsideDestAddress, IpAddress insideDestAddress, MacAddress toMac,
135 EndpointFwdCtxOrdinals epFwdCtxOrds) {
136 // TODO Auto-generated method stub
137 MatchBuilder mb = new MatchBuilder();
139 String outsideIpMatch;
142 Action setDestMac = setDlDstAction(toMac);
143 FlowId flowid = new FlowId(new StringBuilder().append("IngressNat")
145 .append(outsideDestAddress)
147 .append(insideDestAddress)
151 if (insideDestAddress.getIpv4Address() != null) {
152 setDestIp = setIpv4DstAction(insideDestAddress.getIpv4Address());
154 outsideIpMatch = outsideDestAddress.getIpv4Address().getValue() + "/32";
155 m = new Ipv4MatchBuilder().setIpv4Destination(new Ipv4Prefix(outsideIpMatch)).build();
156 mb.setEthernetMatch(ethernetMatch(null, null, FlowUtils.IPv4)).setLayer3Match(m);
157 } else if (insideDestAddress.getIpv6Address() != null) {
158 setDestIp = setIpv6DstAction(insideDestAddress.getIpv6Address());
159 outsideIpMatch = outsideDestAddress.getIpv6Address().getValue() + "/128";
160 m = new Ipv6MatchBuilder().setIpv6Destination(new Ipv6Prefix(outsideIpMatch)).build();
161 mb.setEthernetMatch(ethernetMatch(null, null, FlowUtils.IPv6)).setLayer3Match(m);
166 int egId = epFwdCtxOrds.getEpgId();
167 int bdId = epFwdCtxOrds.getBdId();
168 int fdId = epFwdCtxOrds.getFdId();
169 int l3Id = epFwdCtxOrds.getL3Id();
170 int cgId = epFwdCtxOrds.getCgId();
171 int tunnelId = epFwdCtxOrds.getTunnelId();
172 Action segReg = nxLoadRegAction(NxmNxReg0.class, BigInteger.valueOf(egId));
173 Action scgReg = nxLoadRegAction(NxmNxReg1.class, BigInteger.valueOf(cgId));
174 Action bdReg = nxLoadRegAction(NxmNxReg4.class, BigInteger.valueOf(bdId));
175 Action fdReg = nxLoadRegAction(NxmNxReg5.class, BigInteger.valueOf(fdId));
176 Action vrfReg = nxLoadRegAction(NxmNxReg6.class, BigInteger.valueOf(l3Id));
177 Action tunIdAction = nxLoadTunIdAction(BigInteger.valueOf(tunnelId), false);
179 FlowBuilder flowb = base().setPriority(Integer.valueOf(100))
181 .setMatch(mb.build())
184 applyActionIns(setDestIp, setDestMac, segReg, scgReg, bdReg, fdReg, vrfReg, tunIdAction),
185 gotoTableIns(ctx.getPolicyManager().getTABLEID_DESTINATION_MAPPER())));
186 return flowb.build();
189 private Flow createOutsideArpFlow(IpAddress outsideDestAddress, MacAddress toMac, NodeId nodeId) {
191 String ikey = outsideDestAddress.getIpv4Address().getValue();
192 BigInteger intMac = new BigInteger(1, bytesFromHexString(toMac.getValue()));
194 FlowId flowId = new FlowId(new StringBuffer().append("outside-ip-arp|").append(ikey).toString());
195 MatchBuilder mb = new MatchBuilder().setEthernetMatch(ethernetMatch(null, null, ARP)).setLayer3Match(
196 new ArpMatchBuilder().setArpOp(Integer.valueOf(1))
197 .setArpTargetTransportAddress(new Ipv4Prefix(ikey + "/32"))
200 FlowBuilder flowb = base().setPriority(150)
202 .setMatch(mb.build())
204 instructions(applyActionIns(nxMoveEthSrcToEthDstAction(), setDlSrcAction(toMac),
205 nxLoadArpOpAction(BigInteger.valueOf(2L)), nxMoveArpShaToArpThaAction(),
206 nxLoadArpShaAction(intMac), nxMoveArpSpaToArpTpaAction(), nxLoadArpSpaAction(ikey),
207 outputAction(new NodeConnectorId(nodeId.getValue() + ":INPORT")))));
208 return flowb.build();
211 static byte[] bytesFromHexString(String values) {
213 if (values != null) {
216 String[] octets = target.split(":");
218 byte[] ret = new byte[octets.length];
219 for (int i = 0; i < octets.length; i++) {
220 ret[i] = Integer.valueOf(octets[i], 16).byteValue();