NETVIRT-1080 : Fix PNF discovery failure after FIP detached
[netvirt.git] / natservice / impl / src / main / java / org / opendaylight / netvirt / natservice / internal / VxlanGreConntrackBasedSnatService.java
1 /*
2  * Copyright © 2017 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netvirt.natservice.internal;
9
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.List;
14
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.netvirt.vpnmanager.api.IVpnFootprintService;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.OdlInterfaceRpcService;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.ItmRpcService;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupTypes;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ProviderTypes;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.Routers;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.routers.ExternalIps;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.types.rev160517.IpPrefixOrAddress;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.NxActionNatFlags;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.NxActionNatRangePresent;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
57 public class VxlanGreConntrackBasedSnatService extends ConntrackBasedSnatService {
58
59     private static final Logger LOG = LoggerFactory.getLogger(VxlanGreConntrackBasedSnatService.class);
60     private final ExternalRoutersListener externalRouterListener;
61     private final IElanService elanManager;
62
63     public VxlanGreConntrackBasedSnatService(DataBroker dataBroker, IMdsalApiManager mdsalManager,
64                                              ItmRpcService itmManager, OdlInterfaceRpcService odlInterfaceRpcService,
65                                              IdManagerService idManager, NAPTSwitchSelector naptSwitchSelector,
66                                              ExternalRoutersListener externalRouterListener, IElanService elanManager,
67                                              IInterfaceManager interfaceManager,
68                                              IVpnFootprintService vpnFootprintService) {
69         super(dataBroker, mdsalManager, itmManager, idManager, naptSwitchSelector, odlInterfaceRpcService,
70                 interfaceManager, vpnFootprintService);
71         this.externalRouterListener = externalRouterListener;
72         this.elanManager = elanManager;
73     }
74
75     @Override
76     public boolean handleSnatAllSwitch(Routers routers, BigInteger primarySwitchId,  int addOrRemove) {
77         ProviderTypes extNwProviderType = NatUtil.getProviderTypefromNetworkId(dataBroker, routers.getNetworkId());
78         LOG.debug("VxlanGreConntrackBasedSnatService: handleSnatAllSwitch ProviderTypes {}", extNwProviderType);
79         if (extNwProviderType == ProviderTypes.FLAT || extNwProviderType == ProviderTypes.VLAN) {
80             return false;
81         }
82         return super.handleSnatAllSwitch(routers, primarySwitchId, addOrRemove);
83     }
84
85     @Override
86     public boolean handleSnat(Routers routers, BigInteger primarySwitchId, BigInteger dpnId,  int addOrRemove) {
87         ProviderTypes extNwProviderType = NatUtil.getProviderTypefromNetworkId(dataBroker, routers.getNetworkId());
88         LOG.debug("VxlanGreConntrackBasedSnatService: handleSnat ProviderTypes {}", extNwProviderType);
89         if (extNwProviderType == ProviderTypes.FLAT || extNwProviderType == ProviderTypes.VLAN) {
90             return false;
91         }
92         return super.handleSnat(routers, primarySwitchId, dpnId, addOrRemove);
93     }
94
95     @Override
96     protected void installSnatSpecificEntriesForNaptSwitch(Routers routers, BigInteger dpnId, int addOrRemove) {
97         LOG.info("installSnatSpecificEntriesForNaptSwitch for router {}",
98                 routers.getRouterName());
99         String routerName = routers.getRouterName();
100         Long routerId = NatUtil.getVpnId(dataBroker, routerName);
101         int elanId = NatUtil.getElanInstanceByName(routers.getNetworkId().getValue(), dataBroker)
102                 .getElanTag().intValue();
103         /* Install Outbound NAT entries */
104
105         installSnatMissEntryForPrimrySwch(dpnId, routerId, elanId, addOrRemove);
106         installTerminatingServiceTblEntryForVxlanGre(dpnId, routerName, routerId, elanId, addOrRemove);
107         //Long extNetVpnId = NatUtil.getNetworkVpnIdFromRouterId(dataBroker, routerId);
108         Uuid vpnUuid = NatUtil.getVpnIdfromNetworkId(dataBroker, routers.getNetworkId());
109         if (vpnUuid == null) {
110             LOG.error("installSnatSpecificEntriesForNaptSwitch: Unable to retrieve external vpn_id for "
111                     + "external network {} with routerId {}", routers.getNetworkId(), routerId);
112             return;
113         }
114         Long extNetVpnId = NatUtil.getVpnId(dataBroker, vpnUuid.getValue());
115         /*//Long extNetVpnId = NatUtil.getAssociatedVPN(dataBroker, routers.getNetworkId(), LOG);
116         if (extNetVpnId == NatConstants.INVALID_ID && addOrRemove == NwConstants.ADD_FLOW) {
117             LOG.error("installSnatSpecificEntriesForNaptSwitch: Unable to retrieve external vpn_id for "
118                     + "external network {} with routerId {}", routers.getNetworkId(), routerId);
119             return;
120         }*/
121         LOG.info("installSnatSpecificEntriesForNaptSwitch: external network vpn_id {} for router {}",
122                 extNetVpnId, routers.getRouterName());
123         List<ExternalIps> externalIps = routers.getExternalIps();
124         createOutboundTblTrackEntryForVxlanGre(dpnId, routerId, extNetVpnId, addOrRemove);
125         createOutboundTblEntryForVxlanGre(dpnId, routerId, extNetVpnId, externalIps, elanId, addOrRemove);
126         installNaptPfibFlowForVxlanGre(routers, dpnId, extNetVpnId, addOrRemove);
127         installNaptPfibEntry(dpnId, routerId, addOrRemove);
128
129         //Install Inbound NAT entries
130         installInboundEntryForVxlanGre(dpnId, routerId, extNetVpnId, externalIps, elanId, addOrRemove);
131         if (externalIps.isEmpty()) {
132             LOG.error("installSnatSpecificEntriesForNaptSwitch: No externalIP present for router {}",
133                     routerName);
134             return;
135         }
136         //The logic now handle only one external IP per router, others if present will be ignored.
137         String externalIp = externalIps.get(0).getIpAddress();
138         externalIp = NatUtil.validateAndAddNetworkMask(externalIp);
139         WriteTransaction writeFlowInvTx = dataBroker.newWriteOnlyTransaction();
140         if (addOrRemove == NwConstants.ADD_FLOW) {
141             externalRouterListener.handleSnatReverseTraffic(dpnId, routers, routerId, routerName, externalIp,
142                     writeFlowInvTx);
143         } else {
144             externalRouterListener.clearFibTsAndReverseTraffic(dpnId, routerId, routers.getNetworkId(),
145                     Collections.singletonList(externalIp), null, routers.getExtGwMacAddress(), writeFlowInvTx);
146         }
147     }
148
149     protected void createOutboundTblTrackEntryForVxlanGre(BigInteger dpnId, Long routerId, Long extNetVpnId,
150                                                int addOrRemove) {
151         LOG.info("createOutboundTblTrackEntryForVxlanGre: Install Outbound tracking table flow on dpId {} for "
152                 + "routerId {}", dpnId, routerId);
153         List<MatchInfoBase> matches = new ArrayList<>();
154         matches.add(MatchEthernetType.IPV4);
155         matches.add(new NxMatchCtState(SNAT_CT_STATE, SNAT_CT_STATE_MASK));
156         matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(routerId), MetaDataUtil.METADATA_MASK_VRFID));
157
158         ArrayList<ActionInfo> listActionInfo = new ArrayList<>();
159         if (addOrRemove == NwConstants.ADD_FLOW) {
160             ActionSetFieldMeta actionSetFieldMeta = new ActionSetFieldMeta(MetaDataUtil
161                     .getVpnIdMetadata(extNetVpnId));
162             listActionInfo.add(actionSetFieldMeta);
163         }
164         ArrayList<InstructionInfo> instructionInfo = new ArrayList<>();
165         listActionInfo.add(new ActionNxResubmit(NwConstants.NAPT_PFIB_TABLE));
166         instructionInfo.add(new InstructionApplyActions(listActionInfo));
167
168         String flowRef = getFlowRef(dpnId, NwConstants.OUTBOUND_NAPT_TABLE, routerId);
169         flowRef += "trkest";
170         syncFlow(dpnId, NwConstants.OUTBOUND_NAPT_TABLE, flowRef, NatConstants.SNAT_TRK_FLOW_PRIORITY, flowRef,
171                 NwConstants.COOKIE_SNAT_TABLE, matches, instructionInfo, addOrRemove);
172
173     }
174
175     protected void createOutboundTblEntryForVxlanGre(BigInteger dpnId, long routerId, Long extNetVpnId,
176                                                      List<ExternalIps> externalIps, int elanId, int addOrRemove) {
177         LOG.info("createOutboundTblEntryForVxlanGre: Install Outbound table flow on dpId {} for routerId {}", dpnId,
178                 routerId);
179         List<MatchInfoBase> matches = new ArrayList<>();
180         matches.add(MatchEthernetType.IPV4);
181         matches.add(new NxMatchCtState(TRACKED_NEW_CT_STATE, TRACKED_NEW_CT_MASK));
182         matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(routerId), MetaDataUtil.METADATA_MASK_VRFID));
183         if (externalIps.isEmpty()) {
184             LOG.error("createOutboundTblEntryForVxlanGre: No externalIP present for routerId {}",
185                     routerId);
186             return;
187         }
188         //The logic now handle only one external IP per router, others if present will be ignored.
189         String externalIp = externalIps.get(0).getIpAddress();
190         List<ActionInfo> actionsInfos = new ArrayList<>();
191         if (addOrRemove == NwConstants.ADD_FLOW) {
192             ActionSetFieldMeta actionSetFieldMeta = new ActionSetFieldMeta(MetaDataUtil
193                     .getVpnIdMetadata(extNetVpnId));
194             actionsInfos.add(actionSetFieldMeta);
195         }
196         List<ActionNxConntrack.NxCtAction> ctActionsListCommit = new ArrayList<>();
197         int rangePresent = NxActionNatRangePresent.NXNATRANGEIPV4MIN.getIntValue();
198         int flags = NxActionNatFlags.NXNATFSRC.getIntValue();
199         ActionNxConntrack.NxCtAction nxCtActionCommit = new ActionNxConntrack.NxNat(0, flags, rangePresent,
200                 new IpPrefixOrAddress(externalIp.toCharArray()).getIpAddress(),
201                 null,0, 0);
202         ctActionsListCommit.add(nxCtActionCommit);
203         int ctCommitFlag = 1;
204         ActionNxConntrack actionNxConntrackSubmit = new ActionNxConntrack(ctCommitFlag, 0, elanId,
205                 NwConstants.NAPT_PFIB_TABLE, ctActionsListCommit);
206         actionsInfos.add(actionNxConntrackSubmit);
207         List<InstructionInfo> instructions = new ArrayList<>();
208         instructions.add(new InstructionApplyActions(actionsInfos));
209         String flowRef = getFlowRef(dpnId, NwConstants.OUTBOUND_NAPT_TABLE, routerId);
210         syncFlow(dpnId, NwConstants.OUTBOUND_NAPT_TABLE, flowRef,  NatConstants.SNAT_NEW_FLOW_PRIORITY,
211                 flowRef, NwConstants.COOKIE_SNAT_TABLE, matches, instructions, addOrRemove);
212     }
213
214     protected void installNaptPfibFlowForVxlanGre(Routers routers, BigInteger dpnId, Long extNetVpnId,
215                                                   int addOrRemove) {
216         LOG.info("installNaptPfibFlowForVxlanGre: Install Napt preFibFlow on dpId {} with matching extNetVpnId {} "
217                 + "for router {}", dpnId, extNetVpnId, routers.getRouterName());
218         List<MatchInfoBase> matches = new ArrayList<>();
219         matches.add(MatchEthernetType.IPV4);
220         if (addOrRemove == NwConstants.ADD_FLOW) {
221             matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(extNetVpnId),
222                     MetaDataUtil.METADATA_MASK_VRFID));
223         }
224         ArrayList<ActionInfo> listActionInfo = new ArrayList<>();
225         ArrayList<InstructionInfo> instructions = new ArrayList<>();
226         listActionInfo.add(new ActionNxLoadInPort(BigInteger.ZERO));
227         listActionInfo.add(new ActionNxResubmit(NwConstants.L3_FIB_TABLE));
228         instructions.add(new InstructionApplyActions(listActionInfo));
229         String flowRef = getFlowRef(dpnId, NwConstants.NAPT_PFIB_TABLE, extNetVpnId);
230         syncFlow(dpnId, NwConstants.NAPT_PFIB_TABLE, flowRef, NatConstants.SNAT_TRK_FLOW_PRIORITY,
231                 flowRef, NwConstants.COOKIE_SNAT_TABLE, matches, instructions, addOrRemove);
232     }
233
234     protected void installInboundEntryForVxlanGre(BigInteger dpnId, long routerId, Long extNeVpnId,
235                                                   List<ExternalIps> externalIps, int elanId, int addOrRemove) {
236         LOG.info("installInboundEntryForVxlanGre:  Install Inbound table entry on dpId {} for routerId {}",
237                 dpnId, routerId);
238         List<MatchInfoBase> matches = new ArrayList<>();
239         matches.add(MatchEthernetType.IPV4);
240         if (externalIps.isEmpty()) {
241             LOG.error("installInboundEntryForVxlanGre : createInboundTblEntry no externalIP present for routerId {}",
242                     routerId);
243             return;
244         }
245         String externalIp = externalIps.get(0).getIpAddress();
246         matches.add(new MatchIpv4Destination(externalIp,"32"));
247         if (addOrRemove == NwConstants.ADD_FLOW) {
248             matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(extNeVpnId),
249                     MetaDataUtil.METADATA_MASK_VRFID));
250         }
251         List<ActionInfo> actionsInfos = new ArrayList<>();
252         List<ActionNxConntrack.NxCtAction> ctActionsList = new ArrayList<>();
253         ActionNxConntrack.NxCtAction nxCtAction = new ActionNxConntrack.NxNat(0, 0, 0,null, null,0, 0);
254         ActionSetFieldMeta actionSetFieldMeta = new ActionSetFieldMeta(MetaDataUtil
255                 .getVpnIdMetadata(routerId));
256         actionsInfos.add(actionSetFieldMeta);
257         ctActionsList.add(nxCtAction);
258         ActionNxConntrack actionNxConntrack = new ActionNxConntrack(0, 0, elanId, NwConstants
259                 .NAPT_PFIB_TABLE,ctActionsList);
260
261         actionsInfos.add(actionNxConntrack);
262         List<InstructionInfo> instructions = new ArrayList<>();
263         instructions.add(new InstructionApplyActions(actionsInfos));
264         String flowRef = getFlowRef(dpnId, NwConstants.INBOUND_NAPT_TABLE, routerId);
265         syncFlow(dpnId, NwConstants.INBOUND_NAPT_TABLE, flowRef, NatConstants.DEFAULT_TS_FLOW_PRIORITY, flowRef,
266                 NwConstants.COOKIE_SNAT_TABLE, matches, instructions, addOrRemove);
267     }
268
269     protected void installTerminatingServiceTblEntryForVxlanGre(BigInteger dpnId, String routerName,
270             Long  routerId, int elanId, int addOrRemove) {
271         LOG.info("installTerminatingServiceTblEntryForVxlanGre : creating entry for"
272                 + "Terminating Service Table for switch {}, routerId {}", dpnId, routerId);
273         List<MatchInfo> matches = new ArrayList<>();
274         matches.add(MatchEthernetType.IPV4);
275
276         BigInteger tunnelId = BigInteger.valueOf(routerId);
277         if (elanManager.isOpenStackVniSemanticsEnforced()) {
278             tunnelId = NatOverVxlanUtil.getRouterVni(idManager, routerName, routerId);
279         }
280         matches.add(new MatchTunnelId(tunnelId));
281
282         List<ActionInfo> actionsInfos = new ArrayList<>();
283         List<NxCtAction> ctActionsList = new ArrayList<>();
284         NxCtAction nxCtAction = new ActionNxConntrack.NxNat(0, 0, 0,null, null,0, 0);
285         ctActionsList.add(nxCtAction);
286         ActionNxConntrack actionNxConntrack = new ActionNxConntrack(0, 0, elanId, NwConstants
287                 .OUTBOUND_NAPT_TABLE,ctActionsList);
288         ActionSetFieldMeta actionSetFieldMeta = new ActionSetFieldMeta(MetaDataUtil
289                 .getVpnIdMetadata(routerId.longValue()));
290         actionsInfos.add(actionSetFieldMeta);
291         actionsInfos.add(actionNxConntrack);
292         List<InstructionInfo> instructions = new ArrayList<>();
293         instructions.add(new InstructionApplyActions(actionsInfos));
294         String flowRef = getFlowRef(dpnId, NwConstants.INTERNAL_TUNNEL_TABLE, routerId.longValue());
295         syncFlow(dpnId,  NwConstants.INTERNAL_TUNNEL_TABLE, flowRef, NatConstants.DEFAULT_TS_FLOW_PRIORITY, flowRef,
296                  NwConstants.COOKIE_SNAT_TABLE, matches, instructions, addOrRemove);
297
298     }
299
300     protected void installSnatMissEntry(BigInteger dpnId, Long routerId, String routerName, BigInteger primarySwitchId,
301             int addOrRemove) {
302         LOG.debug("installSnatMissEntry : Installing SNAT miss entry in switch {}", dpnId);
303         List<ActionInfo> listActionInfoPrimary = new ArrayList<>();
304         String ifNamePrimary = getTunnelInterfaceName(dpnId, primarySwitchId);
305         List<BucketInfo> listBucketInfo = new ArrayList<>();
306         if (ifNamePrimary != null) {
307             LOG.debug("installSnatMissEntry : On Non- Napt switch , Primary Tunnel interface is {}", ifNamePrimary);
308             listActionInfoPrimary = NatUtil.getEgressActionsForInterface(odlInterfaceRpcService, itmManager,
309                     interfaceManager, ifNamePrimary, routerId);
310         }
311         BucketInfo bucketPrimary = new BucketInfo(listActionInfoPrimary);
312         listBucketInfo.add(0, bucketPrimary);
313         LOG.debug("installSnatMissEntry : installSnatMissEntry called for dpnId {} with primaryBucket {} ", dpnId,
314                 listBucketInfo.get(0));
315         // Install the select group
316         long groupId = createGroupId(getGroupIdKey(routerName));
317         GroupEntity groupEntity = MDSALUtil.buildGroupEntity(dpnId, groupId, routerName, GroupTypes.GroupAll,
318                 listBucketInfo);
319         LOG.debug("installSnatMissEntry : installing the SNAT to NAPT GroupEntity:{}", groupEntity);
320         mdsalManager.installGroup(groupEntity);
321         // Install miss entry pointing to group
322         LOG.debug("installSnatMissEntry : buildSnatFlowEntity is called for dpId {}, routerName {} and groupId {}",
323                 dpnId, routerName, groupId);
324         List<MatchInfo> matches = new ArrayList<>();
325         matches.add(new MatchEthernetType(0x0800L));
326         matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(routerId), MetaDataUtil.METADATA_MASK_VRFID));
327
328         List<ActionInfo> actionsInfo = new ArrayList<>();
329
330         BigInteger tunnelId = BigInteger.valueOf(routerId);
331         if (elanManager.isOpenStackVniSemanticsEnforced()) {
332             tunnelId = NatOverVxlanUtil.getRouterVni(idManager, routerName, routerId);
333         }
334
335         actionsInfo.add(new ActionSetFieldTunnelId(tunnelId));
336         LOG.debug("AbstractSnatService : Setting the tunnel to the list of action infos {}", actionsInfo);
337         actionsInfo.add(new ActionGroup(groupId));
338         List<InstructionInfo> instructions = new ArrayList<>();
339         instructions.add(new InstructionApplyActions(actionsInfo));
340         String flowRef = getFlowRef(dpnId, NwConstants.PSNAT_TABLE, routerId);
341         syncFlow(dpnId, NwConstants.PSNAT_TABLE, flowRef,  NatConstants.DEFAULT_PSNAT_FLOW_PRIORITY, flowRef,
342                 NwConstants.COOKIE_SNAT_TABLE, matches, instructions, addOrRemove);
343     }
344
345 }