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.controller.md.sal.binding.api.WriteTransaction;
17 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
18 import org.opendaylight.genius.mdsalutil.ActionInfo;
19 import org.opendaylight.genius.mdsalutil.BucketInfo;
20 import org.opendaylight.genius.mdsalutil.GroupEntity;
21 import org.opendaylight.genius.mdsalutil.InstructionInfo;
22 import org.opendaylight.genius.mdsalutil.MDSALUtil;
23 import org.opendaylight.genius.mdsalutil.MatchInfo;
24 import org.opendaylight.genius.mdsalutil.MatchInfoBase;
25 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
26 import org.opendaylight.genius.mdsalutil.NwConstants;
27 import org.opendaylight.genius.mdsalutil.actions.ActionGroup;
28 import org.opendaylight.genius.mdsalutil.actions.ActionNxConntrack;
29 import org.opendaylight.genius.mdsalutil.actions.ActionNxConntrack.NxCtAction;
30 import org.opendaylight.genius.mdsalutil.actions.ActionNxLoadInPort;
31 import org.opendaylight.genius.mdsalutil.actions.ActionNxResubmit;
32 import org.opendaylight.genius.mdsalutil.actions.ActionSetFieldMeta;
33 import org.opendaylight.genius.mdsalutil.actions.ActionSetFieldTunnelId;
34 import org.opendaylight.genius.mdsalutil.instructions.InstructionApplyActions;
35 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
36 import org.opendaylight.genius.mdsalutil.matches.MatchEthernetType;
37 import org.opendaylight.genius.mdsalutil.matches.MatchIpv4Destination;
38 import org.opendaylight.genius.mdsalutil.matches.MatchMetadata;
39 import org.opendaylight.genius.mdsalutil.matches.MatchTunnelId;
40 import org.opendaylight.genius.mdsalutil.nxmatches.NxMatchCtState;
41 import org.opendaylight.netvirt.elanmanager.api.IElanService;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.OdlInterfaceRpcService;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.ItmRpcService;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupTypes;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ProviderTypes;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.Routers;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.routers.ExternalIps;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.types.rev160517.IpPrefixOrAddress;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.NxActionNatFlags;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.NxActionNatRangePresent;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
56 public class VxlanGreConntrackBasedSnatService extends ConntrackBasedSnatService {
58 private static final Logger LOG = LoggerFactory.getLogger(VxlanGreConntrackBasedSnatService.class);
59 private final ExternalRoutersListener externalRouterListener;
60 private final IElanService elanManager;
62 public VxlanGreConntrackBasedSnatService(DataBroker dataBroker, IMdsalApiManager mdsalManager,
63 ItmRpcService itmManager, OdlInterfaceRpcService odlInterfaceRpcService,
64 IdManagerService idManager, NAPTSwitchSelector naptSwitchSelector,
65 ExternalRoutersListener externalRouterListener, IElanService elanManager,
66 IInterfaceManager interfaceManager) {
67 super(dataBroker, mdsalManager, itmManager, idManager, naptSwitchSelector, odlInterfaceRpcService,
69 this.externalRouterListener = externalRouterListener;
70 this.elanManager = elanManager;
74 public boolean handleSnatAllSwitch(Routers routers, BigInteger primarySwitchId, int addOrRemove) {
75 ProviderTypes extNwProviderType = NatUtil.getProviderTypefromNetworkId(dataBroker, routers.getNetworkId());
76 LOG.debug("VxlanGreConntrackBasedSnatService: handleSnatAllSwitch ProviderTypes {}", extNwProviderType);
77 if (extNwProviderType == ProviderTypes.FLAT || extNwProviderType == ProviderTypes.VLAN) {
80 return super.handleSnatAllSwitch(routers, primarySwitchId, addOrRemove);
84 public boolean handleSnat(Routers routers, BigInteger primarySwitchId, BigInteger dpnId, int addOrRemove) {
85 ProviderTypes extNwProviderType = NatUtil.getProviderTypefromNetworkId(dataBroker, routers.getNetworkId());
86 LOG.debug("VxlanGreConntrackBasedSnatService: handleSnat ProviderTypes {}", extNwProviderType);
87 if (extNwProviderType == ProviderTypes.FLAT || extNwProviderType == ProviderTypes.VLAN) {
90 return super.handleSnat(routers, primarySwitchId, dpnId, addOrRemove);
94 protected void installSnatSpecificEntriesForNaptSwitch(Routers routers, BigInteger dpnId, int addOrRemove) {
95 LOG.info("installSnatSpecificEntriesForNaptSwitch for router {}",
96 routers.getRouterName());
97 String routerName = routers.getRouterName();
98 Long routerId = NatUtil.getVpnId(dataBroker, routerName);
99 int elanId = NatUtil.getElanInstanceByName(routers.getNetworkId().getValue(), dataBroker)
100 .getElanTag().intValue();
101 /* Install Outbound NAT entries */
103 installSnatMissEntryForPrimrySwch(dpnId, routerId, elanId, addOrRemove);
104 installTerminatingServiceTblEntryForVxlanGre(dpnId, routerName, routerId, elanId, addOrRemove);
105 //Long extNetVpnId = NatUtil.getNetworkVpnIdFromRouterId(dataBroker, routerId);
106 Uuid vpnUuid = NatUtil.getVpnIdfromNetworkId(dataBroker, routers.getNetworkId());
107 if (vpnUuid == null) {
108 LOG.error("installSnatSpecificEntriesForNaptSwitch: Unable to retrieve external vpn_id for "
109 + "external network {} with routerId {}", routers.getNetworkId(), routerId);
112 Long extNetVpnId = NatUtil.getVpnId(dataBroker, vpnUuid.getValue());
113 /*//Long extNetVpnId = NatUtil.getAssociatedVPN(dataBroker, routers.getNetworkId(), LOG);
114 if (extNetVpnId == NatConstants.INVALID_ID && addOrRemove == NwConstants.ADD_FLOW) {
115 LOG.error("installSnatSpecificEntriesForNaptSwitch: Unable to retrieve external vpn_id for "
116 + "external network {} with routerId {}", routers.getNetworkId(), routerId);
119 LOG.info("installSnatSpecificEntriesForNaptSwitch: external network vpn_id {} for router {}",
120 extNetVpnId, routers.getRouterName());
121 List<ExternalIps> externalIps = routers.getExternalIps();
122 createOutboundTblTrackEntryForVxlanGre(dpnId, routerId, extNetVpnId, addOrRemove);
123 createOutboundTblEntryForVxlanGre(dpnId, routerId, extNetVpnId, externalIps, elanId, addOrRemove);
124 installNaptPfibFlowForVxlanGre(routers, dpnId, extNetVpnId, addOrRemove);
125 installNaptPfibEntry(dpnId, routerId, addOrRemove);
127 //Install Inbound NAT entries
128 installInboundEntryForVxlanGre(dpnId, routerId, extNetVpnId, externalIps, elanId, addOrRemove);
129 if (externalIps.isEmpty()) {
130 LOG.error("installSnatSpecificEntriesForNaptSwitch: No externalIP present for router {}",
134 //The logic now handle only one external IP per router, others if present will be ignored.
135 String externalIp = externalIps.get(0).getIpAddress();
136 externalIp = NatUtil.validateAndAddNetworkMask(externalIp);
137 WriteTransaction writeFlowInvTx = dataBroker.newWriteOnlyTransaction();
138 if (addOrRemove == NwConstants.ADD_FLOW) {
139 externalRouterListener.handleSnatReverseTraffic(dpnId, routers, routerId, routerName, externalIp,
142 externalRouterListener.clearFibTsAndReverseTraffic(dpnId, routerId, routers.getNetworkId(),
143 Collections.singletonList(externalIp), null, routers.getExtGwMacAddress(), writeFlowInvTx);
147 protected void createOutboundTblTrackEntryForVxlanGre(BigInteger dpnId, Long routerId, Long extNetVpnId,
149 LOG.info("createOutboundTblTrackEntryForVxlanGre: Install Outbound tracking table flow on dpId {} for "
150 + "routerId {}", dpnId, routerId);
151 List<MatchInfoBase> matches = new ArrayList<>();
152 matches.add(MatchEthernetType.IPV4);
153 matches.add(new NxMatchCtState(SNAT_CT_STATE, SNAT_CT_STATE_MASK));
154 matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(routerId), MetaDataUtil.METADATA_MASK_VRFID));
156 ArrayList<ActionInfo> listActionInfo = new ArrayList<>();
157 if (addOrRemove == NwConstants.ADD_FLOW) {
158 ActionSetFieldMeta actionSetFieldMeta = new ActionSetFieldMeta(MetaDataUtil
159 .getVpnIdMetadata(extNetVpnId));
160 listActionInfo.add(actionSetFieldMeta);
162 ArrayList<InstructionInfo> instructionInfo = new ArrayList<>();
163 listActionInfo.add(new ActionNxResubmit(NwConstants.NAPT_PFIB_TABLE));
164 instructionInfo.add(new InstructionApplyActions(listActionInfo));
166 String flowRef = getFlowRef(dpnId, NwConstants.OUTBOUND_NAPT_TABLE, routerId);
168 syncFlow(dpnId, NwConstants.OUTBOUND_NAPT_TABLE, flowRef, NatConstants.SNAT_TRK_FLOW_PRIORITY, flowRef,
169 NwConstants.COOKIE_SNAT_TABLE, matches, instructionInfo, addOrRemove);
173 protected void createOutboundTblEntryForVxlanGre(BigInteger dpnId, long routerId, Long extNetVpnId,
174 List<ExternalIps> externalIps, int elanId, int addOrRemove) {
175 LOG.info("createOutboundTblEntryForVxlanGre: Install Outbound table flow on dpId {} for routerId {}", dpnId,
177 List<MatchInfoBase> matches = new ArrayList<>();
178 matches.add(MatchEthernetType.IPV4);
179 matches.add(new NxMatchCtState(TRACKED_NEW_CT_STATE, TRACKED_NEW_CT_MASK));
180 matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(routerId), MetaDataUtil.METADATA_MASK_VRFID));
181 if (externalIps.isEmpty()) {
182 LOG.error("createOutboundTblEntryForVxlanGre: No externalIP present for routerId {}",
186 //The logic now handle only one external IP per router, others if present will be ignored.
187 String externalIp = externalIps.get(0).getIpAddress();
188 List<ActionInfo> actionsInfos = new ArrayList<>();
189 if (addOrRemove == NwConstants.ADD_FLOW) {
190 ActionSetFieldMeta actionSetFieldMeta = new ActionSetFieldMeta(MetaDataUtil
191 .getVpnIdMetadata(extNetVpnId));
192 actionsInfos.add(actionSetFieldMeta);
194 List<ActionNxConntrack.NxCtAction> ctActionsListCommit = new ArrayList<>();
195 int rangePresent = NxActionNatRangePresent.NXNATRANGEIPV4MIN.getIntValue();
196 int flags = NxActionNatFlags.NXNATFSRC.getIntValue();
197 ActionNxConntrack.NxCtAction nxCtActionCommit = new ActionNxConntrack.NxNat(0, flags, rangePresent,
198 new IpPrefixOrAddress(externalIp.toCharArray()).getIpAddress(),
200 ctActionsListCommit.add(nxCtActionCommit);
201 int ctCommitFlag = 1;
202 ActionNxConntrack actionNxConntrackSubmit = new ActionNxConntrack(ctCommitFlag, 0, elanId,
203 NwConstants.NAPT_PFIB_TABLE, ctActionsListCommit);
204 actionsInfos.add(actionNxConntrackSubmit);
205 List<InstructionInfo> instructions = new ArrayList<>();
206 instructions.add(new InstructionApplyActions(actionsInfos));
207 String flowRef = getFlowRef(dpnId, NwConstants.OUTBOUND_NAPT_TABLE, routerId);
208 syncFlow(dpnId, NwConstants.OUTBOUND_NAPT_TABLE, flowRef, NatConstants.SNAT_NEW_FLOW_PRIORITY,
209 flowRef, NwConstants.COOKIE_SNAT_TABLE, matches, instructions, addOrRemove);
212 protected void installNaptPfibFlowForVxlanGre(Routers routers, BigInteger dpnId, Long extNetVpnId,
214 LOG.info("installNaptPfibFlowForVxlanGre: Install Napt preFibFlow on dpId {} with matching extNetVpnId {} "
215 + "for router {}", dpnId, extNetVpnId, routers.getRouterName());
216 List<MatchInfoBase> matches = new ArrayList<>();
217 matches.add(MatchEthernetType.IPV4);
218 if (addOrRemove == NwConstants.ADD_FLOW) {
219 matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(extNetVpnId),
220 MetaDataUtil.METADATA_MASK_VRFID));
222 ArrayList<ActionInfo> listActionInfo = new ArrayList<>();
223 ArrayList<InstructionInfo> instructions = new ArrayList<>();
224 listActionInfo.add(new ActionNxLoadInPort(BigInteger.ZERO));
225 listActionInfo.add(new ActionNxResubmit(NwConstants.L3_FIB_TABLE));
226 instructions.add(new InstructionApplyActions(listActionInfo));
227 String flowRef = getFlowRef(dpnId, NwConstants.NAPT_PFIB_TABLE, extNetVpnId);
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 installInboundEntryForVxlanGre(BigInteger dpnId, long routerId, Long extNeVpnId,
233 List<ExternalIps> externalIps, int elanId, int addOrRemove) {
234 LOG.info("installInboundEntryForVxlanGre: Install Inbound table entry on dpId {} for routerId {}",
236 List<MatchInfoBase> matches = new ArrayList<>();
237 matches.add(MatchEthernetType.IPV4);
238 if (externalIps.isEmpty()) {
239 LOG.error("installInboundEntryForVxlanGre : createInboundTblEntry no externalIP present for routerId {}",
243 String externalIp = externalIps.get(0).getIpAddress();
244 matches.add(new MatchIpv4Destination(externalIp,"32"));
245 if (addOrRemove == NwConstants.ADD_FLOW) {
246 matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(extNeVpnId),
247 MetaDataUtil.METADATA_MASK_VRFID));
249 List<ActionInfo> actionsInfos = new ArrayList<>();
250 List<ActionNxConntrack.NxCtAction> ctActionsList = new ArrayList<>();
251 ActionNxConntrack.NxCtAction nxCtAction = new ActionNxConntrack.NxNat(0, 0, 0,null, null,0, 0);
252 ActionSetFieldMeta actionSetFieldMeta = new ActionSetFieldMeta(MetaDataUtil
253 .getVpnIdMetadata(routerId));
254 actionsInfos.add(actionSetFieldMeta);
255 ctActionsList.add(nxCtAction);
256 ActionNxConntrack actionNxConntrack = new ActionNxConntrack(0, 0, elanId, NwConstants
257 .NAPT_PFIB_TABLE,ctActionsList);
259 actionsInfos.add(actionNxConntrack);
260 List<InstructionInfo> instructions = new ArrayList<>();
261 instructions.add(new InstructionApplyActions(actionsInfos));
262 String flowRef = getFlowRef(dpnId, NwConstants.INBOUND_NAPT_TABLE, routerId);
263 syncFlow(dpnId, NwConstants.INBOUND_NAPT_TABLE, flowRef, NatConstants.DEFAULT_TS_FLOW_PRIORITY, flowRef,
264 NwConstants.COOKIE_SNAT_TABLE, matches, instructions, addOrRemove);
267 protected void installTerminatingServiceTblEntryForVxlanGre(BigInteger dpnId, String routerName,
268 Long routerId, int elanId, int addOrRemove) {
269 LOG.info("installTerminatingServiceTblEntryForVxlanGre : creating entry for"
270 + "Terminating Service Table for switch {}, routerId {}", dpnId, routerId);
271 List<MatchInfo> matches = new ArrayList<>();
272 matches.add(MatchEthernetType.IPV4);
274 BigInteger tunnelId = BigInteger.valueOf(routerId);
275 if (elanManager.isOpenStackVniSemanticsEnforced()) {
276 tunnelId = NatOverVxlanUtil.getRouterVni(idManager, routerName, routerId);
278 matches.add(new MatchTunnelId(tunnelId));
280 List<ActionInfo> actionsInfos = new ArrayList<>();
281 List<NxCtAction> ctActionsList = new ArrayList<>();
282 NxCtAction nxCtAction = new ActionNxConntrack.NxNat(0, 0, 0,null, null,0, 0);
283 ctActionsList.add(nxCtAction);
284 ActionNxConntrack actionNxConntrack = new ActionNxConntrack(0, 0, elanId, NwConstants
285 .OUTBOUND_NAPT_TABLE,ctActionsList);
286 ActionSetFieldMeta actionSetFieldMeta = new ActionSetFieldMeta(MetaDataUtil
287 .getVpnIdMetadata(routerId.longValue()));
288 actionsInfos.add(actionSetFieldMeta);
289 actionsInfos.add(actionNxConntrack);
290 List<InstructionInfo> instructions = new ArrayList<>();
291 instructions.add(new InstructionApplyActions(actionsInfos));
292 String flowRef = getFlowRef(dpnId, NwConstants.INTERNAL_TUNNEL_TABLE, routerId.longValue());
293 syncFlow(dpnId, NwConstants.INTERNAL_TUNNEL_TABLE, flowRef, NatConstants.DEFAULT_TS_FLOW_PRIORITY, flowRef,
294 NwConstants.COOKIE_SNAT_TABLE, matches, instructions, addOrRemove);
298 protected void installSnatMissEntry(BigInteger dpnId, Long routerId, String routerName, BigInteger primarySwitchId,
300 LOG.debug("installSnatMissEntry : Installing SNAT miss entry in switch {}", dpnId);
301 List<ActionInfo> listActionInfoPrimary = new ArrayList<>();
302 String ifNamePrimary = getTunnelInterfaceName(dpnId, primarySwitchId);
303 List<BucketInfo> listBucketInfo = new ArrayList<>();
304 if (ifNamePrimary != null) {
305 LOG.debug("installSnatMissEntry : On Non- Napt switch , Primary Tunnel interface is {}", ifNamePrimary);
306 listActionInfoPrimary = NatUtil.getEgressActionsForInterface(odlInterfaceRpcService, itmManager,
307 interfaceManager, ifNamePrimary, routerId);
309 BucketInfo bucketPrimary = new BucketInfo(listActionInfoPrimary);
310 listBucketInfo.add(0, bucketPrimary);
311 LOG.debug("installSnatMissEntry : installSnatMissEntry called for dpnId {} with primaryBucket {} ", dpnId,
312 listBucketInfo.get(0));
313 // Install the select group
314 long groupId = createGroupId(getGroupIdKey(routerName));
315 GroupEntity groupEntity = MDSALUtil.buildGroupEntity(dpnId, groupId, routerName, GroupTypes.GroupAll,
317 LOG.debug("installSnatMissEntry : installing the SNAT to NAPT GroupEntity:{}", groupEntity);
318 mdsalManager.installGroup(groupEntity);
319 // Install miss entry pointing to group
320 LOG.debug("installSnatMissEntry : buildSnatFlowEntity is called for dpId {}, routerName {} and groupId {}",
321 dpnId, routerName, groupId);
322 List<MatchInfo> matches = new ArrayList<>();
323 matches.add(new MatchEthernetType(0x0800L));
324 matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(routerId), MetaDataUtil.METADATA_MASK_VRFID));
326 List<ActionInfo> actionsInfo = new ArrayList<>();
328 BigInteger tunnelId = BigInteger.valueOf(routerId);
329 if (elanManager.isOpenStackVniSemanticsEnforced()) {
330 tunnelId = NatOverVxlanUtil.getRouterVni(idManager, routerName, routerId);
333 actionsInfo.add(new ActionSetFieldTunnelId(tunnelId));
334 LOG.debug("AbstractSnatService : Setting the tunnel to the list of action infos {}", actionsInfo);
335 actionsInfo.add(new ActionGroup(groupId));
336 List<InstructionInfo> instructions = new ArrayList<>();
337 instructions.add(new InstructionApplyActions(actionsInfo));
338 String flowRef = getFlowRef(dpnId, NwConstants.PSNAT_TABLE, routerId);
339 syncFlow(dpnId, NwConstants.PSNAT_TABLE, flowRef, NatConstants.DEFAULT_PSNAT_FLOW_PRIORITY, flowRef,
340 NwConstants.COOKIE_SNAT_TABLE, matches, instructions, addOrRemove);