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;
33 import org.opendaylight.groupbasedpolicy.dto.EpKey;
34 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
35 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfWriter;
36 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.OrdinalFactory.EndpointFwdCtxOrdinals;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.l3endpoint.rev151217.NatAddress;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer3Match;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatchBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6MatchBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg1;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg4;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg5;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg6;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
64 * Manage the table that assigns source endpoint group, bridge domain, and
65 * router domain to registers to be used by other tables.
67 public class IngressNatMapper extends FlowTable {
69 protected static final Logger LOG = LoggerFactory.getLogger(IngressNatMapper.class);
71 // TODO Li alagalah Improve UT coverage for this class.
72 public static short TABLE_ID;
74 public IngressNatMapper(OfContext ctx, short tableId) {
80 public short getTableId() {
85 public void sync(NodeId nodeId, OfWriter ofWriter) throws Exception {
87 ofWriter.writeFlow(nodeId, TABLE_ID, dropFlow(Integer.valueOf(1), null, TABLE_ID));
89 // TODO Bug 3546 - Difficult: External port is unrelated to Tenant, L3C, L2BD..
91 Collection<Endpoint> endpointsForNode = ctx.getEndpointManager().getEndpointsForNode(nodeId);
92 Collection<EndpointL3> l3Endpoints = ctx.getEndpointManager().getL3EndpointsWithNat();
93 for (EndpointL3 l3Ep : l3Endpoints) {
94 if (l3Ep.getL2Context() != null && l3Ep.getMacAddress() !=null ) {
95 Endpoint ep = ctx.getEndpointManager().getEndpoint(new EpKey(l3Ep.getL2Context(), l3Ep.getMacAddress()));
96 if (endpointsForNode.contains(ep)) {
97 createNatFlow(l3Ep, nodeId, ofWriter);
103 private void createNatFlow(EndpointL3 l3Ep, NodeId nodeId, OfWriter ofWriter) throws Exception {
104 NatAddress natAugL3Endpoint = l3Ep.getAugmentation(NatAddress.class);
105 // Match on L3 Nat Augmentation in Destination, set to IPAddress/Mac, send to SourceMapper
106 if (natAugL3Endpoint == null) {
109 Endpoint ep = ctx.getEndpointManager().getEndpoint(new EpKey(l3Ep.getL2Context(), l3Ep.getMacAddress()));
110 EndpointFwdCtxOrdinals epFwdCtxOrds = OrdinalFactory.getEndpointFwdCtxOrdinals(ctx, ep);
111 if (epFwdCtxOrds == null) {
112 LOG.info("getEndpointFwdCtxOrdinals is null for EP {}", ep);
115 Flow flow = buildNatFlow(natAugL3Endpoint.getNatAddress(), l3Ep.getIpAddress(), l3Ep.getMacAddress(), epFwdCtxOrds);
117 ofWriter.writeFlow(nodeId, TABLE_ID, flow);
119 flow = createOutsideArpFlow(natAugL3Endpoint.getNatAddress(), l3Ep.getMacAddress(), nodeId);
121 ofWriter.writeFlow(nodeId, TABLE_ID, flow);
125 private Flow buildNatFlow(IpAddress outsideDestAddress, IpAddress insideDestAddress, MacAddress toMac,
126 EndpointFwdCtxOrdinals epFwdCtxOrds) {
127 // TODO Auto-generated method stub
128 MatchBuilder mb = new MatchBuilder();
130 String outsideIpMatch;
133 Action setDestMac = setDlDstAction(toMac);
134 FlowId flowid = new FlowId(new StringBuilder().append("IngressNat")
136 .append(outsideDestAddress)
138 .append(insideDestAddress)
142 if (insideDestAddress.getIpv4Address() != null) {
143 setDestIp = setIpv4DstAction(insideDestAddress.getIpv4Address());
145 outsideIpMatch = outsideDestAddress.getIpv4Address().getValue() + "/32";
146 m = new Ipv4MatchBuilder().setIpv4Destination(new Ipv4Prefix(outsideIpMatch)).build();
147 mb.setEthernetMatch(ethernetMatch(null, null, FlowUtils.IPv4)).setLayer3Match(m);
148 } else if (insideDestAddress.getIpv6Address() != null) {
149 setDestIp = setIpv6DstAction(insideDestAddress.getIpv6Address());
150 outsideIpMatch = outsideDestAddress.getIpv6Address().getValue() + "/128";
151 m = new Ipv6MatchBuilder().setIpv6Destination(new Ipv6Prefix(outsideIpMatch)).build();
152 mb.setEthernetMatch(ethernetMatch(null, null, FlowUtils.IPv6)).setLayer3Match(m);
157 int egId = epFwdCtxOrds.getEpgId();
158 int bdId = epFwdCtxOrds.getBdId();
159 int fdId = epFwdCtxOrds.getFdId();
160 int l3Id = epFwdCtxOrds.getL3Id();
161 int cgId = epFwdCtxOrds.getCgId();
162 int tunnelId = epFwdCtxOrds.getTunnelId();
163 Action segReg = nxLoadRegAction(NxmNxReg0.class, BigInteger.valueOf(egId));
164 Action scgReg = nxLoadRegAction(NxmNxReg1.class, BigInteger.valueOf(cgId));
165 Action bdReg = nxLoadRegAction(NxmNxReg4.class, BigInteger.valueOf(bdId));
166 Action fdReg = nxLoadRegAction(NxmNxReg5.class, BigInteger.valueOf(fdId));
167 Action vrfReg = nxLoadRegAction(NxmNxReg6.class, BigInteger.valueOf(l3Id));
168 Action tunIdAction = nxLoadTunIdAction(BigInteger.valueOf(tunnelId), false);
170 FlowBuilder flowb = base().setPriority(Integer.valueOf(100))
172 .setMatch(mb.build())
175 applyActionIns(setDestIp, setDestMac, segReg, scgReg, bdReg, fdReg, vrfReg, tunIdAction),
176 gotoTableIns(ctx.getPolicyManager().getTABLEID_DESTINATION_MAPPER())));
177 return flowb.build();
180 private Flow createOutsideArpFlow(IpAddress outsideDestAddress, MacAddress toMac, NodeId nodeId) {
182 String ikey = outsideDestAddress.getIpv4Address().getValue();
183 BigInteger intMac = new BigInteger(1, bytesFromHexString(toMac.getValue()));
185 FlowId flowId = new FlowId(new StringBuffer().append("outside-ip-arp|").append(ikey).toString());
186 MatchBuilder mb = new MatchBuilder().setEthernetMatch(ethernetMatch(null, null, ARP)).setLayer3Match(
187 new ArpMatchBuilder().setArpOp(Integer.valueOf(1))
188 .setArpTargetTransportAddress(new Ipv4Prefix(ikey + "/32"))
191 FlowBuilder flowb = base().setPriority(150)
193 .setMatch(mb.build())
195 instructions(applyActionIns(nxMoveEthSrcToEthDstAction(), setDlSrcAction(toMac),
196 nxLoadArpOpAction(BigInteger.valueOf(2L)), nxMoveArpShaToArpThaAction(),
197 nxLoadArpShaAction(intMac), nxMoveArpSpaToArpTpaAction(), nxLoadArpSpaAction(ikey),
198 outputAction(new NodeConnectorId(nodeId.getValue() + ":INPORT")))));
199 return flowb.build();
202 static byte[] bytesFromHexString(String values) {
204 if (values != null) {
207 String[] octets = target.split(":");
209 byte[] ret = new byte[octets.length];
210 for (int i = 0; i < octets.length; i++) {
211 ret[i] = Integer.valueOf(octets[i], 16).byteValue();