2 * Copyright © 2017 Ericsson India Global Services Pvt Ltd. 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.Collections;
13 import java.util.List;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
17 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
18 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
19 import org.opendaylight.genius.mdsalutil.ActionInfo;
20 import org.opendaylight.genius.mdsalutil.BucketInfo;
21 import org.opendaylight.genius.mdsalutil.GroupEntity;
22 import org.opendaylight.genius.mdsalutil.InstructionInfo;
23 import org.opendaylight.genius.mdsalutil.MDSALUtil;
24 import org.opendaylight.genius.mdsalutil.MatchInfo;
25 import org.opendaylight.genius.mdsalutil.MatchInfoBase;
26 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
27 import org.opendaylight.genius.mdsalutil.NwConstants;
28 import org.opendaylight.genius.mdsalutil.actions.ActionGroup;
29 import org.opendaylight.genius.mdsalutil.actions.ActionNxConntrack;
30 import org.opendaylight.genius.mdsalutil.actions.ActionNxConntrack.NxCtAction;
31 import org.opendaylight.genius.mdsalutil.actions.ActionNxLoadInPort;
32 import org.opendaylight.genius.mdsalutil.actions.ActionNxResubmit;
33 import org.opendaylight.genius.mdsalutil.actions.ActionSetFieldMeta;
34 import org.opendaylight.genius.mdsalutil.actions.ActionSetFieldTunnelId;
35 import org.opendaylight.genius.mdsalutil.instructions.InstructionApplyActions;
36 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
37 import org.opendaylight.genius.mdsalutil.matches.MatchEthernetType;
38 import org.opendaylight.genius.mdsalutil.matches.MatchIpv4Destination;
39 import org.opendaylight.genius.mdsalutil.matches.MatchMetadata;
40 import org.opendaylight.genius.mdsalutil.matches.MatchTunnelId;
41 import org.opendaylight.genius.mdsalutil.nxmatches.NxMatchCtState;
42 import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
43 import org.opendaylight.netvirt.elanmanager.api.IElanService;
44 import org.opendaylight.netvirt.fibmanager.api.IFibManager;
45 import org.opendaylight.netvirt.vpnmanager.api.IVpnFootprintService;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.OdlInterfaceRpcService;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.ItmRpcService;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupTypes;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ProviderTypes;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.Routers;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.routers.ExternalIps;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.types.rev160517.IpPrefixOrAddress;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.NxActionNatFlags;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.NxActionNatRangePresent;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
60 public class VxlanGreConntrackBasedSnatService extends ConntrackBasedSnatService {
62 private static final Logger LOG = LoggerFactory.getLogger(VxlanGreConntrackBasedSnatService.class);
63 private final ExternalRoutersListener externalRouterListener;
64 private final IElanService elanManager;
65 private final ManagedNewTransactionRunner txRunner;
67 public VxlanGreConntrackBasedSnatService(DataBroker dataBroker, IMdsalApiManager mdsalManager,
68 ItmRpcService itmManager, OdlInterfaceRpcService odlInterfaceRpcService,
69 IdManagerService idManager, NAPTSwitchSelector naptSwitchSelector,
70 ExternalRoutersListener externalRouterListener, IElanService elanManager,
71 IInterfaceManager interfaceManager,
72 IVpnFootprintService vpnFootprintService,
73 IFibManager fibManager) {
74 super(dataBroker, mdsalManager, itmManager, idManager, naptSwitchSelector, odlInterfaceRpcService,
75 interfaceManager, vpnFootprintService, fibManager);
76 this.externalRouterListener = externalRouterListener;
77 this.elanManager = elanManager;
78 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
82 public boolean handleSnatAllSwitch(Routers routers, BigInteger primarySwitchId, int addOrRemove) {
83 ProviderTypes extNwProviderType = NatUtil.getProviderTypefromNetworkId(dataBroker, routers.getNetworkId());
84 LOG.debug("VxlanGreConntrackBasedSnatService: handleSnatAllSwitch ProviderTypes {}", extNwProviderType);
85 if (extNwProviderType == ProviderTypes.FLAT || extNwProviderType == ProviderTypes.VLAN) {
86 LOG.debug("handleSnatAllSwitch : Skip FLAT/VLAN provider networks.");
89 return super.handleSnatAllSwitch(routers, primarySwitchId, addOrRemove);
93 public boolean handleSnat(Routers routers, BigInteger primarySwitchId, BigInteger dpnId, int addOrRemove) {
94 ProviderTypes extNwProviderType = NatUtil.getProviderTypefromNetworkId(dataBroker, routers.getNetworkId());
95 LOG.debug("VxlanGreConntrackBasedSnatService: handleSnat ProviderTypes {}", extNwProviderType);
96 if (extNwProviderType == ProviderTypes.FLAT || extNwProviderType == ProviderTypes.VLAN) {
97 LOG.debug("handleSnat : Skip FLAT/VLAN provider networks.");
100 return super.handleSnat(routers, primarySwitchId, dpnId, addOrRemove);
104 protected void installSnatSpecificEntriesForNaptSwitch(Routers routers, BigInteger dpnId, int addOrRemove) {
105 LOG.info("installSnatSpecificEntriesForNaptSwitch for router {}",
106 routers.getRouterName());
107 String routerName = routers.getRouterName();
108 Long routerId = NatUtil.getVpnId(dataBroker, routerName);
109 int elanId = NatUtil.getElanInstanceByName(routers.getNetworkId().getValue(), dataBroker)
110 .getElanTag().intValue();
111 /* Install Outbound NAT entries */
113 installSnatMissEntryForPrimrySwch(dpnId, routerId, elanId, addOrRemove);
114 installTerminatingServiceTblEntryForVxlanGre(dpnId, routerName, routerId, elanId, addOrRemove);
115 //Long extNetVpnId = NatUtil.getNetworkVpnIdFromRouterId(dataBroker, routerId);
116 Uuid vpnUuid = NatUtil.getVpnIdfromNetworkId(dataBroker, routers.getNetworkId());
117 if (vpnUuid == null) {
118 LOG.error("installSnatSpecificEntriesForNaptSwitch: Unable to retrieve external vpn_id for "
119 + "external network {} with routerId {}", routers.getNetworkId(), routerId);
122 Long extNetVpnId = NatUtil.getVpnId(dataBroker, vpnUuid.getValue());
123 /*//Long extNetVpnId = NatUtil.getAssociatedVPN(dataBroker, routers.getNetworkId(), LOG);
124 if (extNetVpnId == NatConstants.INVALID_ID && addOrRemove == NwConstants.ADD_FLOW) {
125 LOG.error("installSnatSpecificEntriesForNaptSwitch: Unable to retrieve external vpn_id for "
126 + "external network {} with routerId {}", routers.getNetworkId(), routerId);
129 LOG.info("installSnatSpecificEntriesForNaptSwitch: external network vpn_id {} for router {}",
130 extNetVpnId, routers.getRouterName());
131 List<ExternalIps> externalIps = routers.getExternalIps();
132 createOutboundTblTrackEntryForVxlanGre(dpnId, routerId, extNetVpnId, addOrRemove);
133 createOutboundTblEntryForVxlanGre(dpnId, routerId, extNetVpnId, externalIps, elanId, addOrRemove);
134 installNaptPfibFlowForVxlanGre(routers, dpnId, extNetVpnId, addOrRemove);
135 installNaptPfibEntry(dpnId, routerId, addOrRemove);
137 //Install Inbound NAT entries
138 installInboundEntryForVxlanGre(dpnId, routerId, extNetVpnId, externalIps, elanId, addOrRemove);
139 if (externalIps.isEmpty()) {
140 LOG.error("installSnatSpecificEntriesForNaptSwitch: No externalIP present for router {}",
144 //The logic now handle only one external IP per router, others if present will be ignored.
145 String externalIp = NatUtil.validateAndAddNetworkMask(externalIps.get(0).getIpAddress());
146 ListenableFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> {
147 if (addOrRemove == NwConstants.ADD_FLOW) {
148 externalRouterListener.handleSnatReverseTraffic(dpnId, routers, routerId, routerName, externalIp, tx);
150 externalRouterListener.clearFibTsAndReverseTraffic(dpnId, routerId, routers.getNetworkId(),
151 Collections.singletonList(externalIp), null, routers.getExtGwMacAddress(), tx);
153 }), LOG, "Error installing SNAT-specific entries for NAPT switch");
156 protected void createOutboundTblTrackEntryForVxlanGre(BigInteger dpnId, Long routerId, Long extNetVpnId,
158 LOG.info("createOutboundTblTrackEntryForVxlanGre: Install Outbound tracking table flow on dpId {} for "
159 + "routerId {}", dpnId, routerId);
160 List<MatchInfoBase> matches = new ArrayList<>();
161 matches.add(MatchEthernetType.IPV4);
162 matches.add(new NxMatchCtState(SNAT_CT_STATE, SNAT_CT_STATE_MASK));
163 matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(routerId), MetaDataUtil.METADATA_MASK_VRFID));
165 ArrayList<ActionInfo> listActionInfo = new ArrayList<>();
166 if (addOrRemove == NwConstants.ADD_FLOW) {
167 ActionSetFieldMeta actionSetFieldMeta = new ActionSetFieldMeta(MetaDataUtil
168 .getVpnIdMetadata(extNetVpnId));
169 listActionInfo.add(actionSetFieldMeta);
171 ArrayList<InstructionInfo> instructionInfo = new ArrayList<>();
172 listActionInfo.add(new ActionNxResubmit(NwConstants.NAPT_PFIB_TABLE));
173 instructionInfo.add(new InstructionApplyActions(listActionInfo));
175 String flowRef = getFlowRef(dpnId, NwConstants.OUTBOUND_NAPT_TABLE, routerId);
177 syncFlow(dpnId, NwConstants.OUTBOUND_NAPT_TABLE, flowRef, NatConstants.SNAT_TRK_FLOW_PRIORITY, flowRef,
178 NwConstants.COOKIE_SNAT_TABLE, matches, instructionInfo, addOrRemove);
182 protected void createOutboundTblEntryForVxlanGre(BigInteger dpnId, long routerId, Long extNetVpnId,
183 List<ExternalIps> externalIps, int elanId, int addOrRemove) {
184 LOG.info("createOutboundTblEntryForVxlanGre: Install Outbound table flow on dpId {} for routerId {}", dpnId,
186 List<MatchInfoBase> matches = new ArrayList<>();
187 matches.add(MatchEthernetType.IPV4);
188 matches.add(new NxMatchCtState(TRACKED_NEW_CT_STATE, TRACKED_NEW_CT_MASK));
189 matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(routerId), MetaDataUtil.METADATA_MASK_VRFID));
190 if (externalIps.isEmpty()) {
191 LOG.error("createOutboundTblEntryForVxlanGre: No externalIP present for routerId {}",
195 //The logic now handle only one external IP per router, others if present will be ignored.
196 String externalIp = externalIps.get(0).getIpAddress();
197 List<ActionInfo> actionsInfos = new ArrayList<>();
198 if (addOrRemove == NwConstants.ADD_FLOW) {
199 ActionSetFieldMeta actionSetFieldMeta = new ActionSetFieldMeta(MetaDataUtil
200 .getVpnIdMetadata(extNetVpnId));
201 actionsInfos.add(actionSetFieldMeta);
203 List<ActionNxConntrack.NxCtAction> ctActionsListCommit = new ArrayList<>();
204 int rangePresent = NxActionNatRangePresent.NXNATRANGEIPV4MIN.getIntValue();
205 int flags = NxActionNatFlags.NXNATFSRC.getIntValue();
206 ActionNxConntrack.NxCtAction nxCtActionCommit = new ActionNxConntrack.NxNat(0, flags, rangePresent,
207 new IpPrefixOrAddress(externalIp.toCharArray()).getIpAddress(),
209 ctActionsListCommit.add(nxCtActionCommit);
210 int ctCommitFlag = 1;
211 ActionNxConntrack actionNxConntrackSubmit = new ActionNxConntrack(ctCommitFlag, 0, elanId,
212 NwConstants.NAPT_PFIB_TABLE, ctActionsListCommit);
213 actionsInfos.add(actionNxConntrackSubmit);
214 List<InstructionInfo> instructions = new ArrayList<>();
215 instructions.add(new InstructionApplyActions(actionsInfos));
216 String flowRef = getFlowRef(dpnId, NwConstants.OUTBOUND_NAPT_TABLE, routerId);
217 syncFlow(dpnId, NwConstants.OUTBOUND_NAPT_TABLE, flowRef, NatConstants.SNAT_NEW_FLOW_PRIORITY,
218 flowRef, NwConstants.COOKIE_SNAT_TABLE, matches, instructions, addOrRemove);
221 protected void installNaptPfibFlowForVxlanGre(Routers routers, BigInteger dpnId, Long extNetVpnId,
223 LOG.info("installNaptPfibFlowForVxlanGre: Install Napt preFibFlow on dpId {} with matching extNetVpnId {} "
224 + "for router {}", dpnId, extNetVpnId, routers.getRouterName());
225 List<MatchInfoBase> matches = new ArrayList<>();
226 matches.add(MatchEthernetType.IPV4);
227 if (addOrRemove == NwConstants.ADD_FLOW) {
228 matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(extNetVpnId),
229 MetaDataUtil.METADATA_MASK_VRFID));
231 ArrayList<ActionInfo> listActionInfo = new ArrayList<>();
232 ArrayList<InstructionInfo> instructions = new ArrayList<>();
233 listActionInfo.add(new ActionNxLoadInPort(BigInteger.ZERO));
234 listActionInfo.add(new ActionNxResubmit(NwConstants.L3_FIB_TABLE));
235 instructions.add(new InstructionApplyActions(listActionInfo));
236 String flowRef = getFlowRef(dpnId, NwConstants.NAPT_PFIB_TABLE, extNetVpnId);
237 syncFlow(dpnId, NwConstants.NAPT_PFIB_TABLE, flowRef, NatConstants.SNAT_TRK_FLOW_PRIORITY,
238 flowRef, NwConstants.COOKIE_SNAT_TABLE, matches, instructions, addOrRemove);
241 protected void installInboundEntryForVxlanGre(BigInteger dpnId, long routerId, Long extNeVpnId,
242 List<ExternalIps> externalIps, int elanId, int addOrRemove) {
243 LOG.info("installInboundEntryForVxlanGre: Install Inbound table entry on dpId {} for routerId {}",
245 List<MatchInfoBase> matches = new ArrayList<>();
246 matches.add(MatchEthernetType.IPV4);
247 if (externalIps.isEmpty()) {
248 LOG.error("installInboundEntryForVxlanGre : createInboundTblEntry no externalIP present for routerId {}",
252 String externalIp = externalIps.get(0).getIpAddress();
253 matches.add(new MatchIpv4Destination(externalIp,"32"));
254 if (addOrRemove == NwConstants.ADD_FLOW) {
255 matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(extNeVpnId),
256 MetaDataUtil.METADATA_MASK_VRFID));
258 List<ActionInfo> actionsInfos = new ArrayList<>();
259 List<ActionNxConntrack.NxCtAction> ctActionsList = new ArrayList<>();
260 ActionNxConntrack.NxCtAction nxCtAction = new ActionNxConntrack.NxNat(0, 0, 0,null, null,0, 0);
261 ActionSetFieldMeta actionSetFieldMeta = new ActionSetFieldMeta(MetaDataUtil
262 .getVpnIdMetadata(routerId));
263 actionsInfos.add(actionSetFieldMeta);
264 ctActionsList.add(nxCtAction);
265 ActionNxConntrack actionNxConntrack = new ActionNxConntrack(0, 0, elanId, NwConstants
266 .NAPT_PFIB_TABLE,ctActionsList);
268 actionsInfos.add(actionNxConntrack);
269 List<InstructionInfo> instructions = new ArrayList<>();
270 instructions.add(new InstructionApplyActions(actionsInfos));
271 String flowRef = getFlowRef(dpnId, NwConstants.INBOUND_NAPT_TABLE, routerId);
272 syncFlow(dpnId, NwConstants.INBOUND_NAPT_TABLE, flowRef, NatConstants.DEFAULT_TS_FLOW_PRIORITY, flowRef,
273 NwConstants.COOKIE_SNAT_TABLE, matches, instructions, addOrRemove);
276 protected void installTerminatingServiceTblEntryForVxlanGre(BigInteger dpnId, String routerName,
277 Long routerId, int elanId, int addOrRemove) {
278 LOG.info("installTerminatingServiceTblEntryForVxlanGre : creating entry for"
279 + "Terminating Service Table for switch {}, routerId {}", dpnId, routerId);
280 List<MatchInfo> matches = new ArrayList<>();
281 matches.add(MatchEthernetType.IPV4);
283 BigInteger tunnelId = BigInteger.valueOf(routerId);
284 if (elanManager.isOpenStackVniSemanticsEnforced()) {
285 tunnelId = NatOverVxlanUtil.getRouterVni(idManager, routerName, routerId);
287 matches.add(new MatchTunnelId(tunnelId));
289 List<ActionInfo> actionsInfos = new ArrayList<>();
290 List<NxCtAction> ctActionsList = new ArrayList<>();
291 NxCtAction nxCtAction = new ActionNxConntrack.NxNat(0, 0, 0,null, null,0, 0);
292 ctActionsList.add(nxCtAction);
293 ActionNxConntrack actionNxConntrack = new ActionNxConntrack(0, 0, elanId, NwConstants
294 .OUTBOUND_NAPT_TABLE,ctActionsList);
295 ActionSetFieldMeta actionSetFieldMeta = new ActionSetFieldMeta(MetaDataUtil
296 .getVpnIdMetadata(routerId.longValue()));
297 actionsInfos.add(actionSetFieldMeta);
298 actionsInfos.add(actionNxConntrack);
299 List<InstructionInfo> instructions = new ArrayList<>();
300 instructions.add(new InstructionApplyActions(actionsInfos));
301 String flowRef = getFlowRef(dpnId, NwConstants.INTERNAL_TUNNEL_TABLE, routerId.longValue());
302 syncFlow(dpnId, NwConstants.INTERNAL_TUNNEL_TABLE, flowRef, NatConstants.DEFAULT_TS_FLOW_PRIORITY, flowRef,
303 NwConstants.COOKIE_SNAT_TABLE, matches, instructions, addOrRemove);
307 protected void installSnatMissEntry(BigInteger dpnId, Long routerId, String routerName, BigInteger primarySwitchId,
309 LOG.debug("installSnatMissEntry : Installing SNAT miss entry in switch {}", dpnId);
310 List<ActionInfo> listActionInfoPrimary = new ArrayList<>();
311 String ifNamePrimary = getTunnelInterfaceName(dpnId, primarySwitchId);
312 List<BucketInfo> listBucketInfo = new ArrayList<>();
313 if (ifNamePrimary != null) {
314 LOG.debug("installSnatMissEntry : On Non- Napt switch , Primary Tunnel interface is {}", ifNamePrimary);
315 listActionInfoPrimary = NatUtil.getEgressActionsForInterface(odlInterfaceRpcService, itmManager,
316 interfaceManager, ifNamePrimary, routerId);
318 BucketInfo bucketPrimary = new BucketInfo(listActionInfoPrimary);
319 listBucketInfo.add(0, bucketPrimary);
320 LOG.debug("installSnatMissEntry : installSnatMissEntry called for dpnId {} with primaryBucket {} ", dpnId,
321 listBucketInfo.get(0));
322 // Install the select group
323 long groupId = createGroupId(getGroupIdKey(routerName));
324 GroupEntity groupEntity = MDSALUtil.buildGroupEntity(dpnId, groupId, routerName, GroupTypes.GroupAll,
326 LOG.debug("installSnatMissEntry : installing the SNAT to NAPT GroupEntity:{}", groupEntity);
327 mdsalManager.installGroup(groupEntity);
328 // Install miss entry pointing to group
329 LOG.debug("installSnatMissEntry : buildSnatFlowEntity is called for dpId {}, routerName {} and groupId {}",
330 dpnId, routerName, groupId);
331 List<MatchInfo> matches = new ArrayList<>();
332 matches.add(new MatchEthernetType(0x0800L));
333 matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(routerId), MetaDataUtil.METADATA_MASK_VRFID));
335 List<ActionInfo> actionsInfo = new ArrayList<>();
337 BigInteger tunnelId = BigInteger.valueOf(routerId);
338 if (elanManager.isOpenStackVniSemanticsEnforced()) {
339 tunnelId = NatOverVxlanUtil.getRouterVni(idManager, routerName, routerId);
342 actionsInfo.add(new ActionSetFieldTunnelId(tunnelId));
343 LOG.debug("AbstractSnatService : Setting the tunnel to the list of action infos {}", actionsInfo);
344 actionsInfo.add(new ActionGroup(groupId));
345 List<InstructionInfo> instructions = new ArrayList<>();
346 instructions.add(new InstructionApplyActions(actionsInfo));
347 String flowRef = getFlowRef(dpnId, NwConstants.PSNAT_TABLE, routerId);
348 syncFlow(dpnId, NwConstants.PSNAT_TABLE, flowRef, NatConstants.DEFAULT_PSNAT_FLOW_PRIORITY, flowRef,
349 NwConstants.COOKIE_SNAT_TABLE, matches, instructions, addOrRemove);