2 * Copyright (c) 2017 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
8 package org.opendaylight.netvirt.natservice.internal;
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.List;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.genius.mdsalutil.ActionInfo;
15 import org.opendaylight.genius.mdsalutil.InstructionInfo;
16 import org.opendaylight.genius.mdsalutil.MatchInfo;
17 import org.opendaylight.genius.mdsalutil.MatchInfoBase;
18 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
19 import org.opendaylight.genius.mdsalutil.NwConstants;
20 import org.opendaylight.genius.mdsalutil.actions.ActionNxConntrack;
21 import org.opendaylight.genius.mdsalutil.actions.ActionNxConntrack.NxCtAction;
22 import org.opendaylight.genius.mdsalutil.actions.ActionNxLoadInPort;
23 import org.opendaylight.genius.mdsalutil.actions.ActionNxLoadMetadata;
24 import org.opendaylight.genius.mdsalutil.actions.ActionNxResubmit;
25 import org.opendaylight.genius.mdsalutil.actions.ActionSetFieldEthernetSource;
26 import org.opendaylight.genius.mdsalutil.instructions.InstructionApplyActions;
27 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
28 import org.opendaylight.genius.mdsalutil.matches.MatchEthernetType;
29 import org.opendaylight.genius.mdsalutil.matches.MatchIpv4Destination;
30 import org.opendaylight.genius.mdsalutil.matches.MatchMetadata;
31 import org.opendaylight.genius.mdsalutil.matches.MatchTunnelId;
32 import org.opendaylight.genius.mdsalutil.nxmatches.NxMatchCtState;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.OdlInterfaceRpcService;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.ItmRpcService;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.Routers;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.routers.ExternalIps;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.types.rev160517.IpPrefixOrAddress;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.NxActionNatFlags;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.NxActionNatRangePresent;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
46 public abstract class ConntrackBasedSnatService extends AbstractSnatService {
47 private static final Logger LOG = LoggerFactory.getLogger(ConntrackBasedSnatService.class);
49 protected static final int TRACKED_NEW_CT_STATE = 0x21;
50 protected static final int TRACKED_NEW_CT_MASK = 0x21;
51 protected static final int SNAT_CT_STATE = 0x40;
52 protected static final int SNAT_CT_STATE_MASK = 0x40;
53 protected static final int DNAT_CT_STATE = 0x80;
54 protected static final int DNAT_CT_STATE_MASK = 0x80;
56 public ConntrackBasedSnatService(DataBroker dataBroker, IMdsalApiManager mdsalManager, ItmRpcService itmManager,
57 OdlInterfaceRpcService interfaceManager, IdManagerService idManager,
58 NAPTSwitchSelector naptSwitchSelector) {
59 super(dataBroker, mdsalManager, itmManager, interfaceManager, idManager, naptSwitchSelector);
63 protected void installSnatSpecificEntriesForNaptSwitch(Routers routers, BigInteger dpnId, int addOrRemove) {
64 LOG.info("installSnatSpecificEntriesForNaptSwitch: called for router {}",
65 routers.getRouterName());
66 String routerName = routers.getRouterName();
67 Long routerId = NatUtil.getVpnId(getDataBroker(), routerName);
68 int elanId = NatUtil.getElanInstanceByName(routers.getNetworkId().getValue(), getDataBroker())
69 .getElanTag().intValue();
70 /* Install Outbound NAT entries */
72 installSnatMissEntryForPrimrySwch(dpnId, routerId, elanId, addOrRemove);
73 installTerminatingServiceTblEntry(dpnId, routerId, elanId, addOrRemove);
75 String extGwMacAddress = NatUtil.getExtGwMacAddFromRouterName(getDataBroker(), routerName);
76 createOutboundTblTrackEntry(dpnId, routerId, extGwMacAddress, addOrRemove);
77 List<ExternalIps> externalIps = routers.getExternalIps();
78 if (externalIps.isEmpty()) {
79 LOG.error("AbstractSnatService: installSnatCommonEntriesForNaptSwitch no externalIP present"
84 //The logic now handle only one external IP per router, others if present will be ignored.
85 String externalIp = externalIps.get(0).getIpAddress();
86 Uuid externalSubnetId = externalIps.get(0).getSubnetId();
87 long extSubnetId = NatConstants.INVALID_ID;
88 if (addOrRemove == NwConstants.ADD_FLOW) {
89 extSubnetId = NatUtil.getExternalSubnetVpnId(getDataBroker(),externalSubnetId);
91 createOutboundTblEntry(dpnId, routerId, externalIp, elanId, extGwMacAddress, addOrRemove);
92 installNaptPfibFlow(routers, dpnId, routerId, extSubnetId, addOrRemove);
94 //Install Inbound NAT entries
95 installInboundEntry(dpnId, routerId, externalIp, elanId, extSubnetId, addOrRemove);
96 installNaptPfibEntry(dpnId, routerId, addOrRemove);
101 protected void installSnatSpecificEntriesForNonNaptSwitch(Routers routers, BigInteger dpnId, int addOrRemove) {
102 // Nothing to to do here.
106 protected void installSnatMissEntryForPrimrySwch(BigInteger dpnId, Long routerId, int elanId, int addOrRemove) {
107 LOG.info("installSnatSpecificEntriesForNaptSwitch : called for the primary NAPT switch dpnId {}", dpnId);
108 List<MatchInfo> matches = new ArrayList<>();
109 matches.add(MatchEthernetType.IPV4);
110 matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(routerId), MetaDataUtil.METADATA_MASK_VRFID));
111 List<InstructionInfo> instructions = new ArrayList<>();
112 List<ActionInfo> actionsInfos = new ArrayList<>();
113 List<NxCtAction> ctActionsList = new ArrayList<>();
114 NxCtAction nxCtAction = new ActionNxConntrack.NxNat(0, 0, 0,null, null,0, 0);
115 ctActionsList.add(nxCtAction);
116 ActionNxConntrack actionNxConntrack = new ActionNxConntrack(0, 0, elanId,
117 NwConstants.OUTBOUND_NAPT_TABLE,ctActionsList);
119 actionsInfos.add(actionNxConntrack);
120 instructions.add(new InstructionApplyActions(actionsInfos));
122 String flowRef = getFlowRef(dpnId, NwConstants.PSNAT_TABLE, routerId);
123 syncFlow(dpnId, NwConstants.PSNAT_TABLE, flowRef, NatConstants.DEFAULT_PSNAT_FLOW_PRIORITY, flowRef,
124 NwConstants.COOKIE_SNAT_TABLE, matches, instructions, addOrRemove);
127 protected void installTerminatingServiceTblEntry(BigInteger dpnId, Long routerId, int elanId, int addOrRemove) {
128 LOG.info("installTerminatingServiceTblEntry : creating entry for Terminating Service Table "
129 + "for switch {}, routerId {}", dpnId, routerId);
130 List<MatchInfo> matches = new ArrayList<>();
131 matches.add(MatchEthernetType.IPV4);
132 matches.add(new MatchTunnelId(BigInteger.valueOf(routerId)));
135 List<ActionInfo> actionsInfos = new ArrayList<>();
136 List<NxCtAction> ctActionsList = new ArrayList<>();
137 NxCtAction nxCtAction = new ActionNxConntrack.NxNat(0, 0, 0,null, null,0, 0);
138 ctActionsList.add(nxCtAction);
139 ActionNxConntrack actionNxConntrack = new ActionNxConntrack(0, 0, elanId, NwConstants
140 .OUTBOUND_NAPT_TABLE,ctActionsList);
141 ActionNxLoadMetadata actionLoadMeta = new ActionNxLoadMetadata(MetaDataUtil
142 .getVpnIdMetadata(routerId.longValue()), LOAD_START, LOAD_END);
143 actionsInfos.add(actionLoadMeta);
144 actionsInfos.add(actionNxConntrack);
145 List<InstructionInfo> instructions = new ArrayList<>();
146 instructions.add(new InstructionApplyActions(actionsInfos));
147 String flowRef = getFlowRef(dpnId, NwConstants.INTERNAL_TUNNEL_TABLE, routerId.longValue());
148 syncFlow(dpnId, NwConstants.INTERNAL_TUNNEL_TABLE, flowRef, NatConstants.DEFAULT_TS_FLOW_PRIORITY, flowRef,
149 NwConstants.COOKIE_SNAT_TABLE, matches, instructions, addOrRemove);
153 protected void createOutboundTblTrackEntry(BigInteger dpnId, Long routerId, String extGwMacAddress,
155 LOG.info("createOutboundTblTrackEntry : called for switch {}, routerId {}", dpnId, routerId);
156 List<MatchInfoBase> matches = new ArrayList<>();
157 matches.add(MatchEthernetType.IPV4);
158 matches.add(new NxMatchCtState(SNAT_CT_STATE, SNAT_CT_STATE_MASK));
159 matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(routerId), MetaDataUtil.METADATA_MASK_VRFID));
160 ArrayList<ActionInfo> listActionInfo = new ArrayList<>();
161 if (addOrRemove == NwConstants.ADD_FLOW) {
162 listActionInfo.add(new ActionSetFieldEthernetSource(new MacAddress(extGwMacAddress)));
164 ArrayList<InstructionInfo> instructionInfo = new ArrayList<>();
165 listActionInfo.add(new ActionNxResubmit(NwConstants.NAPT_PFIB_TABLE));
166 instructionInfo.add(new InstructionApplyActions(listActionInfo));
168 String flowRef = getFlowRef(dpnId, NwConstants.OUTBOUND_NAPT_TABLE, routerId);
170 syncFlow(dpnId, NwConstants.OUTBOUND_NAPT_TABLE, flowRef, NatConstants.SNAT_TRK_FLOW_PRIORITY, flowRef,
171 NwConstants.COOKIE_SNAT_TABLE, matches, instructionInfo, addOrRemove);
175 protected void createOutboundTblEntry(BigInteger dpnId, long routerId, String externalIp,
176 int elanId, String extGwMacAddress, int addOrRemove) {
177 LOG.info("createOutboundTblEntry : dpId {} and routerId {}", dpnId, routerId);
178 List<MatchInfoBase> matches = new ArrayList<>();
179 matches.add(MatchEthernetType.IPV4);
180 matches.add(new NxMatchCtState(TRACKED_NEW_CT_STATE, TRACKED_NEW_CT_MASK));
181 matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(routerId), MetaDataUtil.METADATA_MASK_VRFID));
182 List<ActionInfo> actionsInfos = new ArrayList<>();
183 if (addOrRemove == NwConstants.ADD_FLOW) {
184 actionsInfos.add(new ActionSetFieldEthernetSource(new MacAddress(extGwMacAddress)));
186 List<NxCtAction> ctActionsListCommit = new ArrayList<>();
187 int rangePresent = NxActionNatRangePresent.NXNATRANGEIPV4MIN.getIntValue();
188 int flags = NxActionNatFlags.NXNATFSRC.getIntValue();
189 NxCtAction nxCtActionCommit = new ActionNxConntrack.NxNat(0, flags, rangePresent,
190 new IpPrefixOrAddress(externalIp.toCharArray()).getIpAddress(),
192 ctActionsListCommit.add(nxCtActionCommit);
193 int ctCommitFlag = 1;
194 ActionNxConntrack actionNxConntrackSubmit = new ActionNxConntrack(ctCommitFlag, 0, elanId,
195 NwConstants.NAPT_PFIB_TABLE, ctActionsListCommit);
196 actionsInfos.add(actionNxConntrackSubmit);
197 List<InstructionInfo> instructions = new ArrayList<>();
198 instructions.add(new InstructionApplyActions(actionsInfos));
199 String flowRef = getFlowRef(dpnId, NwConstants.OUTBOUND_NAPT_TABLE, routerId);
200 syncFlow(dpnId, NwConstants.OUTBOUND_NAPT_TABLE, flowRef, NatConstants.SNAT_NEW_FLOW_PRIORITY,
201 flowRef, NwConstants.COOKIE_SNAT_TABLE, matches, instructions, addOrRemove);
204 protected void installNaptPfibFlow(Routers routers, BigInteger dpnId, long routerId,
205 long extSubnetId, int addOrRemove) {
206 Long extNetId = NatUtil.getVpnId(getDataBroker(), routers.getNetworkId().getValue());
207 LOG.info("installNaptPfibFlow : dpId {}, extNetId {}", dpnId, extNetId);
208 List<MatchInfoBase> matches = new ArrayList<>();
209 matches.add(MatchEthernetType.IPV4);
210 matches.add(new NxMatchCtState(SNAT_CT_STATE, SNAT_CT_STATE_MASK));
211 matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(routerId), MetaDataUtil.METADATA_MASK_VRFID));
212 List<ActionInfo> listActionInfo = new ArrayList<>();
213 if (addOrRemove == NwConstants.ADD_FLOW) {
214 if (extSubnetId == NatConstants.INVALID_ID) {
215 LOG.error("installNaptPfibFlow : external subnet id is invalid.");
218 ActionNxLoadMetadata actionLoadMeta = new ActionNxLoadMetadata(MetaDataUtil
219 .getVpnIdMetadata(extSubnetId), LOAD_START, LOAD_END);
220 listActionInfo.add(actionLoadMeta);
222 ArrayList<InstructionInfo> instructions = new ArrayList<>();
223 listActionInfo.add(new ActionNxLoadInPort(BigInteger.ZERO));
224 listActionInfo.add(new ActionNxResubmit(NwConstants.L3_FIB_TABLE));
225 instructions.add(new InstructionApplyActions(listActionInfo));
226 String flowRef = getFlowRef(dpnId, NwConstants.NAPT_PFIB_TABLE, routerId);
227 flowRef = flowRef + "OUTBOUND";
228 syncFlow(dpnId, NwConstants.NAPT_PFIB_TABLE, flowRef, NatConstants.SNAT_TRK_FLOW_PRIORITY,
229 flowRef, NwConstants.COOKIE_SNAT_TABLE, matches, instructions, addOrRemove);
232 protected void installInboundEntry(BigInteger dpnId, long routerId, String externalIp, int elanId, long extSubnetId,
234 LOG.info("installInboundEntry : dpId {} and routerId {}", dpnId, routerId);
235 List<MatchInfoBase> matches = new ArrayList<>();
236 matches.add(MatchEthernetType.IPV4);
237 matches.add(new MatchIpv4Destination(externalIp,"32"));
238 if (addOrRemove == NwConstants.ADD_FLOW) {
239 if (extSubnetId == NatConstants.INVALID_ID) {
240 LOG.error("installInboundEntry : external subnet id is invalid.");
243 matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(extSubnetId),
244 MetaDataUtil.METADATA_MASK_VRFID));
246 List<ActionInfo> actionsInfos = new ArrayList<>();
247 List<NxCtAction> ctActionsList = new ArrayList<>();
248 NxCtAction nxCtAction = new ActionNxConntrack.NxNat(0, 0, 0,null, null,0, 0);
249 ActionNxLoadMetadata actionLoadMeta = new ActionNxLoadMetadata(MetaDataUtil
250 .getVpnIdMetadata(routerId), LOAD_START, LOAD_END);
251 actionsInfos.add(actionLoadMeta);
252 ctActionsList.add(nxCtAction);
253 ActionNxConntrack actionNxConntrack = new ActionNxConntrack(0, 0, elanId, NwConstants
254 .NAPT_PFIB_TABLE,ctActionsList);
256 actionsInfos.add(actionNxConntrack);
257 List<InstructionInfo> instructions = new ArrayList<>();
258 instructions.add(new InstructionApplyActions(actionsInfos));
259 String flowRef = getFlowRef(dpnId, NwConstants.INBOUND_NAPT_TABLE, routerId);
260 flowRef = flowRef + "OUTBOUND";
261 syncFlow(dpnId, NwConstants.INBOUND_NAPT_TABLE, flowRef, NatConstants.DEFAULT_TS_FLOW_PRIORITY, flowRef,
262 NwConstants.COOKIE_SNAT_TABLE, matches, instructions, addOrRemove);
265 protected void installNaptPfibEntry(BigInteger dpnId, long routerId, int addOrRemove) {
266 LOG.info("installNaptPfibEntry : called for dpnId {} and routerId {} ", dpnId, routerId);
267 List<MatchInfoBase> matches = new ArrayList<>();
268 matches.add(MatchEthernetType.IPV4);
269 matches.add(new NxMatchCtState(DNAT_CT_STATE, DNAT_CT_STATE_MASK));
270 matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(routerId), MetaDataUtil.METADATA_MASK_VRFID));
272 ArrayList<ActionInfo> listActionInfo = new ArrayList<>();
273 ArrayList<InstructionInfo> instructionInfo = new ArrayList<>();
274 listActionInfo.add(new ActionNxLoadInPort(BigInteger.ZERO));
275 listActionInfo.add(new ActionNxResubmit(NwConstants.L3_FIB_TABLE));
276 instructionInfo.add(new InstructionApplyActions(listActionInfo));
279 String flowRef = getFlowRef(dpnId, NwConstants.NAPT_PFIB_TABLE, routerId);
280 flowRef = flowRef + "INBOUND";
281 syncFlow(dpnId, NwConstants.NAPT_PFIB_TABLE, flowRef, NatConstants.DEFAULT_PSNAT_FLOW_PRIORITY, flowRef,
282 NwConstants.COOKIE_SNAT_TABLE, matches, instructionInfo, addOrRemove);