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) {
88 return super.handleSnatAllSwitch(routers, primarySwitchId, addOrRemove);
92 public boolean handleSnat(Routers routers, BigInteger primarySwitchId, BigInteger dpnId, int addOrRemove) {
93 ProviderTypes extNwProviderType = NatUtil.getProviderTypefromNetworkId(dataBroker, routers.getNetworkId());
94 LOG.debug("VxlanGreConntrackBasedSnatService: handleSnat ProviderTypes {}", extNwProviderType);
95 if (extNwProviderType == ProviderTypes.FLAT || extNwProviderType == ProviderTypes.VLAN) {
98 return super.handleSnat(routers, primarySwitchId, dpnId, addOrRemove);
102 protected void installSnatSpecificEntriesForNaptSwitch(Routers routers, BigInteger dpnId, int addOrRemove) {
103 LOG.info("installSnatSpecificEntriesForNaptSwitch for router {}",
104 routers.getRouterName());
105 String routerName = routers.getRouterName();
106 Long routerId = NatUtil.getVpnId(dataBroker, routerName);
107 int elanId = NatUtil.getElanInstanceByName(routers.getNetworkId().getValue(), dataBroker)
108 .getElanTag().intValue();
109 /* Install Outbound NAT entries */
111 installSnatMissEntryForPrimrySwch(dpnId, routerId, elanId, addOrRemove);
112 installTerminatingServiceTblEntryForVxlanGre(dpnId, routerName, routerId, elanId, addOrRemove);
113 //Long extNetVpnId = NatUtil.getNetworkVpnIdFromRouterId(dataBroker, routerId);
114 Uuid vpnUuid = NatUtil.getVpnIdfromNetworkId(dataBroker, routers.getNetworkId());
115 if (vpnUuid == null) {
116 LOG.error("installSnatSpecificEntriesForNaptSwitch: Unable to retrieve external vpn_id for "
117 + "external network {} with routerId {}", routers.getNetworkId(), routerId);
120 Long extNetVpnId = NatUtil.getVpnId(dataBroker, vpnUuid.getValue());
121 /*//Long extNetVpnId = NatUtil.getAssociatedVPN(dataBroker, routers.getNetworkId(), LOG);
122 if (extNetVpnId == NatConstants.INVALID_ID && addOrRemove == NwConstants.ADD_FLOW) {
123 LOG.error("installSnatSpecificEntriesForNaptSwitch: Unable to retrieve external vpn_id for "
124 + "external network {} with routerId {}", routers.getNetworkId(), routerId);
127 LOG.info("installSnatSpecificEntriesForNaptSwitch: external network vpn_id {} for router {}",
128 extNetVpnId, routers.getRouterName());
129 List<ExternalIps> externalIps = routers.getExternalIps();
130 createOutboundTblTrackEntryForVxlanGre(dpnId, routerId, extNetVpnId, addOrRemove);
131 createOutboundTblEntryForVxlanGre(dpnId, routerId, extNetVpnId, externalIps, elanId, addOrRemove);
132 installNaptPfibFlowForVxlanGre(routers, dpnId, extNetVpnId, addOrRemove);
133 installNaptPfibEntry(dpnId, routerId, addOrRemove);
135 //Install Inbound NAT entries
136 installInboundEntryForVxlanGre(dpnId, routerId, extNetVpnId, externalIps, elanId, addOrRemove);
137 if (externalIps.isEmpty()) {
138 LOG.error("installSnatSpecificEntriesForNaptSwitch: No externalIP present for router {}",
142 //The logic now handle only one external IP per router, others if present will be ignored.
143 String externalIp = NatUtil.validateAndAddNetworkMask(externalIps.get(0).getIpAddress());
144 ListenableFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> {
145 if (addOrRemove == NwConstants.ADD_FLOW) {
146 externalRouterListener.handleSnatReverseTraffic(dpnId, routers, routerId, routerName, externalIp, tx);
148 externalRouterListener.clearFibTsAndReverseTraffic(dpnId, routerId, routers.getNetworkId(),
149 Collections.singletonList(externalIp), null, routers.getExtGwMacAddress(), tx);
151 }), LOG, "Error installing SNAT-specific entries for NAPT switch");
154 protected void createOutboundTblTrackEntryForVxlanGre(BigInteger dpnId, Long routerId, Long extNetVpnId,
156 LOG.info("createOutboundTblTrackEntryForVxlanGre: Install Outbound tracking table flow on dpId {} for "
157 + "routerId {}", dpnId, routerId);
158 List<MatchInfoBase> matches = new ArrayList<>();
159 matches.add(MatchEthernetType.IPV4);
160 matches.add(new NxMatchCtState(SNAT_CT_STATE, SNAT_CT_STATE_MASK));
161 matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(routerId), MetaDataUtil.METADATA_MASK_VRFID));
163 ArrayList<ActionInfo> listActionInfo = new ArrayList<>();
164 if (addOrRemove == NwConstants.ADD_FLOW) {
165 ActionSetFieldMeta actionSetFieldMeta = new ActionSetFieldMeta(MetaDataUtil
166 .getVpnIdMetadata(extNetVpnId));
167 listActionInfo.add(actionSetFieldMeta);
169 ArrayList<InstructionInfo> instructionInfo = new ArrayList<>();
170 listActionInfo.add(new ActionNxResubmit(NwConstants.NAPT_PFIB_TABLE));
171 instructionInfo.add(new InstructionApplyActions(listActionInfo));
173 String flowRef = getFlowRef(dpnId, NwConstants.OUTBOUND_NAPT_TABLE, routerId);
175 syncFlow(dpnId, NwConstants.OUTBOUND_NAPT_TABLE, flowRef, NatConstants.SNAT_TRK_FLOW_PRIORITY, flowRef,
176 NwConstants.COOKIE_SNAT_TABLE, matches, instructionInfo, addOrRemove);
180 protected void createOutboundTblEntryForVxlanGre(BigInteger dpnId, long routerId, Long extNetVpnId,
181 List<ExternalIps> externalIps, int elanId, int addOrRemove) {
182 LOG.info("createOutboundTblEntryForVxlanGre: Install Outbound table flow on dpId {} for routerId {}", dpnId,
184 List<MatchInfoBase> matches = new ArrayList<>();
185 matches.add(MatchEthernetType.IPV4);
186 matches.add(new NxMatchCtState(TRACKED_NEW_CT_STATE, TRACKED_NEW_CT_MASK));
187 matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(routerId), MetaDataUtil.METADATA_MASK_VRFID));
188 if (externalIps.isEmpty()) {
189 LOG.error("createOutboundTblEntryForVxlanGre: No externalIP present for routerId {}",
193 //The logic now handle only one external IP per router, others if present will be ignored.
194 String externalIp = externalIps.get(0).getIpAddress();
195 List<ActionInfo> actionsInfos = new ArrayList<>();
196 if (addOrRemove == NwConstants.ADD_FLOW) {
197 ActionSetFieldMeta actionSetFieldMeta = new ActionSetFieldMeta(MetaDataUtil
198 .getVpnIdMetadata(extNetVpnId));
199 actionsInfos.add(actionSetFieldMeta);
201 List<ActionNxConntrack.NxCtAction> ctActionsListCommit = new ArrayList<>();
202 int rangePresent = NxActionNatRangePresent.NXNATRANGEIPV4MIN.getIntValue();
203 int flags = NxActionNatFlags.NXNATFSRC.getIntValue();
204 ActionNxConntrack.NxCtAction nxCtActionCommit = new ActionNxConntrack.NxNat(0, flags, rangePresent,
205 new IpPrefixOrAddress(externalIp.toCharArray()).getIpAddress(),
207 ctActionsListCommit.add(nxCtActionCommit);
208 int ctCommitFlag = 1;
209 ActionNxConntrack actionNxConntrackSubmit = new ActionNxConntrack(ctCommitFlag, 0, elanId,
210 NwConstants.NAPT_PFIB_TABLE, ctActionsListCommit);
211 actionsInfos.add(actionNxConntrackSubmit);
212 List<InstructionInfo> instructions = new ArrayList<>();
213 instructions.add(new InstructionApplyActions(actionsInfos));
214 String flowRef = getFlowRef(dpnId, NwConstants.OUTBOUND_NAPT_TABLE, routerId);
215 syncFlow(dpnId, NwConstants.OUTBOUND_NAPT_TABLE, flowRef, NatConstants.SNAT_NEW_FLOW_PRIORITY,
216 flowRef, NwConstants.COOKIE_SNAT_TABLE, matches, instructions, addOrRemove);
219 protected void installNaptPfibFlowForVxlanGre(Routers routers, BigInteger dpnId, Long extNetVpnId,
221 LOG.info("installNaptPfibFlowForVxlanGre: Install Napt preFibFlow on dpId {} with matching extNetVpnId {} "
222 + "for router {}", dpnId, extNetVpnId, routers.getRouterName());
223 List<MatchInfoBase> matches = new ArrayList<>();
224 matches.add(MatchEthernetType.IPV4);
225 if (addOrRemove == NwConstants.ADD_FLOW) {
226 matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(extNetVpnId),
227 MetaDataUtil.METADATA_MASK_VRFID));
229 ArrayList<ActionInfo> listActionInfo = new ArrayList<>();
230 ArrayList<InstructionInfo> instructions = new ArrayList<>();
231 listActionInfo.add(new ActionNxLoadInPort(BigInteger.ZERO));
232 listActionInfo.add(new ActionNxResubmit(NwConstants.L3_FIB_TABLE));
233 instructions.add(new InstructionApplyActions(listActionInfo));
234 String flowRef = getFlowRef(dpnId, NwConstants.NAPT_PFIB_TABLE, extNetVpnId);
235 syncFlow(dpnId, NwConstants.NAPT_PFIB_TABLE, flowRef, NatConstants.SNAT_TRK_FLOW_PRIORITY,
236 flowRef, NwConstants.COOKIE_SNAT_TABLE, matches, instructions, addOrRemove);
239 protected void installInboundEntryForVxlanGre(BigInteger dpnId, long routerId, Long extNeVpnId,
240 List<ExternalIps> externalIps, int elanId, int addOrRemove) {
241 LOG.info("installInboundEntryForVxlanGre: Install Inbound table entry on dpId {} for routerId {}",
243 List<MatchInfoBase> matches = new ArrayList<>();
244 matches.add(MatchEthernetType.IPV4);
245 if (externalIps.isEmpty()) {
246 LOG.error("installInboundEntryForVxlanGre : createInboundTblEntry no externalIP present for routerId {}",
250 String externalIp = externalIps.get(0).getIpAddress();
251 matches.add(new MatchIpv4Destination(externalIp,"32"));
252 if (addOrRemove == NwConstants.ADD_FLOW) {
253 matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(extNeVpnId),
254 MetaDataUtil.METADATA_MASK_VRFID));
256 List<ActionInfo> actionsInfos = new ArrayList<>();
257 List<ActionNxConntrack.NxCtAction> ctActionsList = new ArrayList<>();
258 ActionNxConntrack.NxCtAction nxCtAction = new ActionNxConntrack.NxNat(0, 0, 0,null, null,0, 0);
259 ActionSetFieldMeta actionSetFieldMeta = new ActionSetFieldMeta(MetaDataUtil
260 .getVpnIdMetadata(routerId));
261 actionsInfos.add(actionSetFieldMeta);
262 ctActionsList.add(nxCtAction);
263 ActionNxConntrack actionNxConntrack = new ActionNxConntrack(0, 0, elanId, NwConstants
264 .NAPT_PFIB_TABLE,ctActionsList);
266 actionsInfos.add(actionNxConntrack);
267 List<InstructionInfo> instructions = new ArrayList<>();
268 instructions.add(new InstructionApplyActions(actionsInfos));
269 String flowRef = getFlowRef(dpnId, NwConstants.INBOUND_NAPT_TABLE, routerId);
270 syncFlow(dpnId, NwConstants.INBOUND_NAPT_TABLE, flowRef, NatConstants.DEFAULT_TS_FLOW_PRIORITY, flowRef,
271 NwConstants.COOKIE_SNAT_TABLE, matches, instructions, addOrRemove);
274 protected void installTerminatingServiceTblEntryForVxlanGre(BigInteger dpnId, String routerName,
275 Long routerId, int elanId, int addOrRemove) {
276 LOG.info("installTerminatingServiceTblEntryForVxlanGre : creating entry for"
277 + "Terminating Service Table for switch {}, routerId {}", dpnId, routerId);
278 List<MatchInfo> matches = new ArrayList<>();
279 matches.add(MatchEthernetType.IPV4);
281 BigInteger tunnelId = BigInteger.valueOf(routerId);
282 if (elanManager.isOpenStackVniSemanticsEnforced()) {
283 tunnelId = NatOverVxlanUtil.getRouterVni(idManager, routerName, routerId);
285 matches.add(new MatchTunnelId(tunnelId));
287 List<ActionInfo> actionsInfos = new ArrayList<>();
288 List<NxCtAction> ctActionsList = new ArrayList<>();
289 NxCtAction nxCtAction = new ActionNxConntrack.NxNat(0, 0, 0,null, null,0, 0);
290 ctActionsList.add(nxCtAction);
291 ActionNxConntrack actionNxConntrack = new ActionNxConntrack(0, 0, elanId, NwConstants
292 .OUTBOUND_NAPT_TABLE,ctActionsList);
293 ActionSetFieldMeta actionSetFieldMeta = new ActionSetFieldMeta(MetaDataUtil
294 .getVpnIdMetadata(routerId.longValue()));
295 actionsInfos.add(actionSetFieldMeta);
296 actionsInfos.add(actionNxConntrack);
297 List<InstructionInfo> instructions = new ArrayList<>();
298 instructions.add(new InstructionApplyActions(actionsInfos));
299 String flowRef = getFlowRef(dpnId, NwConstants.INTERNAL_TUNNEL_TABLE, routerId.longValue());
300 syncFlow(dpnId, NwConstants.INTERNAL_TUNNEL_TABLE, flowRef, NatConstants.DEFAULT_TS_FLOW_PRIORITY, flowRef,
301 NwConstants.COOKIE_SNAT_TABLE, matches, instructions, addOrRemove);
305 protected void installSnatMissEntry(BigInteger dpnId, Long routerId, String routerName, BigInteger primarySwitchId,
307 LOG.debug("installSnatMissEntry : Installing SNAT miss entry in switch {}", dpnId);
308 List<ActionInfo> listActionInfoPrimary = new ArrayList<>();
309 String ifNamePrimary = getTunnelInterfaceName(dpnId, primarySwitchId);
310 List<BucketInfo> listBucketInfo = new ArrayList<>();
311 if (ifNamePrimary != null) {
312 LOG.debug("installSnatMissEntry : On Non- Napt switch , Primary Tunnel interface is {}", ifNamePrimary);
313 listActionInfoPrimary = NatUtil.getEgressActionsForInterface(odlInterfaceRpcService, itmManager,
314 interfaceManager, ifNamePrimary, routerId);
316 BucketInfo bucketPrimary = new BucketInfo(listActionInfoPrimary);
317 listBucketInfo.add(0, bucketPrimary);
318 LOG.debug("installSnatMissEntry : installSnatMissEntry called for dpnId {} with primaryBucket {} ", dpnId,
319 listBucketInfo.get(0));
320 // Install the select group
321 long groupId = createGroupId(getGroupIdKey(routerName));
322 GroupEntity groupEntity = MDSALUtil.buildGroupEntity(dpnId, groupId, routerName, GroupTypes.GroupAll,
324 LOG.debug("installSnatMissEntry : installing the SNAT to NAPT GroupEntity:{}", groupEntity);
325 mdsalManager.installGroup(groupEntity);
326 // Install miss entry pointing to group
327 LOG.debug("installSnatMissEntry : buildSnatFlowEntity is called for dpId {}, routerName {} and groupId {}",
328 dpnId, routerName, groupId);
329 List<MatchInfo> matches = new ArrayList<>();
330 matches.add(new MatchEthernetType(0x0800L));
331 matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(routerId), MetaDataUtil.METADATA_MASK_VRFID));
333 List<ActionInfo> actionsInfo = new ArrayList<>();
335 BigInteger tunnelId = BigInteger.valueOf(routerId);
336 if (elanManager.isOpenStackVniSemanticsEnforced()) {
337 tunnelId = NatOverVxlanUtil.getRouterVni(idManager, routerName, routerId);
340 actionsInfo.add(new ActionSetFieldTunnelId(tunnelId));
341 LOG.debug("AbstractSnatService : Setting the tunnel to the list of action infos {}", actionsInfo);
342 actionsInfo.add(new ActionGroup(groupId));
343 List<InstructionInfo> instructions = new ArrayList<>();
344 instructions.add(new InstructionApplyActions(actionsInfo));
345 String flowRef = getFlowRef(dpnId, NwConstants.PSNAT_TABLE, routerId);
346 syncFlow(dpnId, NwConstants.PSNAT_TABLE, flowRef, NatConstants.DEFAULT_PSNAT_FLOW_PRIORITY, flowRef,
347 NwConstants.COOKIE_SNAT_TABLE, matches, instructions, addOrRemove);