NETVIRT-1080 : Fix PNF discovery failure after FIP detached
[netvirt.git] / natservice / impl / src / main / java / org / opendaylight / netvirt / natservice / internal / ConntrackBasedSnatService.java
1 /*
2  * Copyright (c) 2017 Red Hat, Inc. 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 com.google.common.base.Optional;
11 import java.math.BigInteger;
12 import java.util.ArrayList;
13 import java.util.List;
14
15 import org.apache.commons.lang3.tuple.ImmutablePair;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
18 import org.opendaylight.genius.mdsalutil.ActionInfo;
19 import org.opendaylight.genius.mdsalutil.InstructionInfo;
20 import org.opendaylight.genius.mdsalutil.MatchInfo;
21 import org.opendaylight.genius.mdsalutil.MatchInfoBase;
22 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
23 import org.opendaylight.genius.mdsalutil.NwConstants;
24 import org.opendaylight.genius.mdsalutil.actions.ActionNxConntrack;
25 import org.opendaylight.genius.mdsalutil.actions.ActionNxConntrack.NxCtAction;
26 import org.opendaylight.genius.mdsalutil.actions.ActionNxLoadInPort;
27 import org.opendaylight.genius.mdsalutil.actions.ActionNxLoadMetadata;
28 import org.opendaylight.genius.mdsalutil.actions.ActionNxResubmit;
29 import org.opendaylight.genius.mdsalutil.actions.ActionSetFieldEthernetSource;
30 import org.opendaylight.genius.mdsalutil.instructions.InstructionApplyActions;
31 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
32 import org.opendaylight.genius.mdsalutil.matches.MatchEthernetType;
33 import org.opendaylight.genius.mdsalutil.matches.MatchIpv4Destination;
34 import org.opendaylight.genius.mdsalutil.matches.MatchMetadata;
35 import org.opendaylight.genius.mdsalutil.matches.MatchTunnelId;
36 import org.opendaylight.genius.mdsalutil.nxmatches.NxMatchCtState;
37 import org.opendaylight.netvirt.vpnmanager.api.IVpnFootprintService;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.OdlInterfaceRpcService;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.ItmRpcService;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.op.data.vpn.instance.op.data.entry.vpn.to.dpn.list.IpAddresses;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.Routers;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.routers.ExternalIps;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.external.subnets.Subnets;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.types.rev160517.IpPrefixOrAddress;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.NxActionNatFlags;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.NxActionNatRangePresent;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 public abstract class ConntrackBasedSnatService extends AbstractSnatService {
54     private static final Logger LOG = LoggerFactory.getLogger(ConntrackBasedSnatService.class);
55
56     protected static final int TRACKED_NEW_CT_STATE = 0x21;
57     protected static final int TRACKED_NEW_CT_MASK = 0x21;
58     protected static final int SNAT_CT_STATE = 0x40;
59     protected static final int SNAT_CT_STATE_MASK = 0x40;
60     protected static final int DNAT_CT_STATE = 0x80;
61     protected static final int DNAT_CT_STATE_MASK = 0x80;
62
63     public ConntrackBasedSnatService(DataBroker dataBroker, IMdsalApiManager mdsalManager, ItmRpcService itmManager,
64                                      IdManagerService idManager, NAPTSwitchSelector naptSwitchSelector,
65                                      OdlInterfaceRpcService odlInterfaceRpcService,
66                                      IInterfaceManager interfaceManager, IVpnFootprintService vpnFootprintService) {
67         super(dataBroker, mdsalManager, itmManager, odlInterfaceRpcService, idManager, naptSwitchSelector,
68                 interfaceManager, vpnFootprintService);
69     }
70
71     @Override
72     protected void installSnatSpecificEntriesForNaptSwitch(Routers routers, BigInteger dpnId, int addOrRemove) {
73         LOG.info("installSnatSpecificEntriesForNaptSwitch: called for router {}",
74                 routers.getRouterName());
75         String routerName = routers.getRouterName();
76         Long routerId = NatUtil.getVpnId(getDataBroker(), routerName);
77         int elanId = NatUtil.getElanInstanceByName(routers.getNetworkId().getValue(), getDataBroker())
78                 .getElanTag().intValue();
79         /* Install Outbound NAT entries */
80
81         installSnatMissEntryForPrimrySwch(dpnId, routerId, elanId, addOrRemove);
82         installTerminatingServiceTblEntry(dpnId, routerId, elanId, addOrRemove);
83
84         String extGwMacAddress = NatUtil.getExtGwMacAddFromRouterName(getDataBroker(), routerName);
85         createOutboundTblTrackEntry(dpnId, routerId, extGwMacAddress, addOrRemove);
86         List<ExternalIps> externalIps = routers.getExternalIps();
87         if (externalIps.isEmpty()) {
88             LOG.error("AbstractSnatService: installSnatCommonEntriesForNaptSwitch no externalIP present"
89                     + " for routerId {}",
90                     routerId);
91             return;
92         }
93         //The logic now handle only one external IP per router, others if present will be ignored.
94         String externalIp = externalIps.get(0).getIpAddress();
95         Uuid externalSubnetId = externalIps.get(0).getSubnetId();
96         long extSubnetId = NatConstants.INVALID_ID;
97         if (addOrRemove == NwConstants.ADD_FLOW) {
98             extSubnetId = NatUtil.getExternalSubnetVpnId(getDataBroker(),externalSubnetId);
99         }
100         createOutboundTblEntry(dpnId, routerId, externalIp, elanId, extGwMacAddress, addOrRemove);
101         installNaptPfibFlow(routers, dpnId, routerId, extSubnetId, addOrRemove);
102
103         //Install Inbound NAT entries
104         installInboundEntry(dpnId, routerId, externalIp, elanId, extSubnetId, addOrRemove);
105         installNaptPfibEntry(dpnId, routerId, addOrRemove);
106
107         String fibExternalIp = NatUtil.validateAndAddNetworkMask(externalIp);
108         Optional<Subnets> externalSubnet = NatUtil.getOptionalExternalSubnets(dataBroker, externalSubnetId);
109         if (externalSubnet.isPresent()) {
110             String externalVpn =  externalSubnetId.getValue();
111             String vpnRd = NatUtil.getVpnRd(dataBroker, externalVpn);
112             vpnFootprintService.updateVpnToDpnMapping(dpnId, externalVpn, vpnRd, null /* interfaceName*/,
113                 new ImmutablePair<>(IpAddresses.IpAddressSource.ExternalFixedIP, fibExternalIp),
114                 addOrRemove == NwConstants.ADD_FLOW);
115         }
116     }
117
118     @Override
119     protected void installSnatSpecificEntriesForNonNaptSwitch(Routers routers, BigInteger dpnId, int addOrRemove) {
120         // Nothing to to do here.
121
122     }
123
124     protected void installSnatMissEntryForPrimrySwch(BigInteger dpnId, Long routerId, int elanId, int addOrRemove) {
125         LOG.info("installSnatSpecificEntriesForNaptSwitch : called for the primary NAPT switch dpnId {}", dpnId);
126         List<MatchInfo> matches = new ArrayList<>();
127         matches.add(MatchEthernetType.IPV4);
128         matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(routerId), MetaDataUtil.METADATA_MASK_VRFID));
129         List<InstructionInfo> instructions = new ArrayList<>();
130         List<ActionInfo> actionsInfos = new ArrayList<>();
131         List<NxCtAction> ctActionsList = new ArrayList<>();
132         NxCtAction nxCtAction = new ActionNxConntrack.NxNat(0, 0, 0,null, null,0, 0);
133         ctActionsList.add(nxCtAction);
134         ActionNxConntrack actionNxConntrack = new ActionNxConntrack(0, 0, elanId,
135                 NwConstants.OUTBOUND_NAPT_TABLE,ctActionsList);
136
137         actionsInfos.add(actionNxConntrack);
138         instructions.add(new InstructionApplyActions(actionsInfos));
139
140         String flowRef = getFlowRef(dpnId, NwConstants.PSNAT_TABLE, routerId);
141         syncFlow(dpnId, NwConstants.PSNAT_TABLE, flowRef, NatConstants.DEFAULT_PSNAT_FLOW_PRIORITY, flowRef,
142                 NwConstants.COOKIE_SNAT_TABLE, matches, instructions, addOrRemove);
143     }
144
145     protected void installTerminatingServiceTblEntry(BigInteger dpnId, Long  routerId, int elanId, int addOrRemove) {
146         LOG.info("installTerminatingServiceTblEntry : creating entry for Terminating Service Table "
147                 + "for switch {}, routerId {}", dpnId, routerId);
148         List<MatchInfo> matches = new ArrayList<>();
149         matches.add(MatchEthernetType.IPV4);
150         matches.add(new MatchTunnelId(BigInteger.valueOf(routerId)));
151
152
153         List<ActionInfo> actionsInfos = new ArrayList<>();
154         List<NxCtAction> ctActionsList = new ArrayList<>();
155         NxCtAction nxCtAction = new ActionNxConntrack.NxNat(0, 0, 0,null, null,0, 0);
156         ctActionsList.add(nxCtAction);
157         ActionNxConntrack actionNxConntrack = new ActionNxConntrack(0, 0, elanId, NwConstants
158                 .OUTBOUND_NAPT_TABLE,ctActionsList);
159         ActionNxLoadMetadata actionLoadMeta = new ActionNxLoadMetadata(MetaDataUtil
160                 .getVpnIdMetadata(routerId.longValue()), LOAD_START, LOAD_END);
161         actionsInfos.add(actionLoadMeta);
162         actionsInfos.add(actionNxConntrack);
163         List<InstructionInfo> instructions = new ArrayList<>();
164         instructions.add(new InstructionApplyActions(actionsInfos));
165         String flowRef = getFlowRef(dpnId, NwConstants.INTERNAL_TUNNEL_TABLE, routerId.longValue());
166         syncFlow(dpnId,  NwConstants.INTERNAL_TUNNEL_TABLE, flowRef, NatConstants.DEFAULT_TS_FLOW_PRIORITY, flowRef,
167                  NwConstants.COOKIE_SNAT_TABLE, matches, instructions, addOrRemove);
168
169     }
170
171     protected void createOutboundTblTrackEntry(BigInteger dpnId, Long routerId, String extGwMacAddress,
172             int addOrRemove) {
173         LOG.info("createOutboundTblTrackEntry : called for switch {}, routerId {}", dpnId, routerId);
174         List<MatchInfoBase> matches = new ArrayList<>();
175         matches.add(MatchEthernetType.IPV4);
176         matches.add(new NxMatchCtState(SNAT_CT_STATE, SNAT_CT_STATE_MASK));
177         matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(routerId), MetaDataUtil.METADATA_MASK_VRFID));
178         ArrayList<ActionInfo> listActionInfo = new ArrayList<>();
179         if (addOrRemove == NwConstants.ADD_FLOW) {
180             listActionInfo.add(new ActionSetFieldEthernetSource(new MacAddress(extGwMacAddress)));
181         }
182         ArrayList<InstructionInfo> instructionInfo = new ArrayList<>();
183         listActionInfo.add(new ActionNxResubmit(NwConstants.NAPT_PFIB_TABLE));
184         instructionInfo.add(new InstructionApplyActions(listActionInfo));
185
186         String flowRef = getFlowRef(dpnId, NwConstants.OUTBOUND_NAPT_TABLE, routerId);
187         flowRef += "trkest";
188         syncFlow(dpnId, NwConstants.OUTBOUND_NAPT_TABLE, flowRef, NatConstants.SNAT_TRK_FLOW_PRIORITY, flowRef,
189                 NwConstants.COOKIE_SNAT_TABLE, matches, instructionInfo, addOrRemove);
190
191     }
192
193     protected void createOutboundTblEntry(BigInteger dpnId, long routerId, String externalIp,
194             int elanId, String extGwMacAddress,  int addOrRemove) {
195         LOG.info("createOutboundTblEntry : dpId {} and routerId {}", dpnId, routerId);
196         List<MatchInfoBase> matches = new ArrayList<>();
197         matches.add(MatchEthernetType.IPV4);
198         matches.add(new NxMatchCtState(TRACKED_NEW_CT_STATE, TRACKED_NEW_CT_MASK));
199         matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(routerId), MetaDataUtil.METADATA_MASK_VRFID));
200         List<ActionInfo> actionsInfos = new ArrayList<>();
201         if (addOrRemove == NwConstants.ADD_FLOW) {
202             actionsInfos.add(new ActionSetFieldEthernetSource(new MacAddress(extGwMacAddress)));
203         }
204         List<NxCtAction> ctActionsListCommit = new ArrayList<>();
205         int rangePresent = NxActionNatRangePresent.NXNATRANGEIPV4MIN.getIntValue();
206         int flags = NxActionNatFlags.NXNATFSRC.getIntValue();
207         NxCtAction nxCtActionCommit = new ActionNxConntrack.NxNat(0, flags, rangePresent,
208                 new IpPrefixOrAddress(externalIp.toCharArray()).getIpAddress(),
209                 null,0, 0);
210         ctActionsListCommit.add(nxCtActionCommit);
211         int ctCommitFlag = 1;
212         ActionNxConntrack actionNxConntrackSubmit = new ActionNxConntrack(ctCommitFlag, 0, elanId,
213                 NwConstants.NAPT_PFIB_TABLE, ctActionsListCommit);
214         actionsInfos.add(actionNxConntrackSubmit);
215         List<InstructionInfo> instructions = new ArrayList<>();
216         instructions.add(new InstructionApplyActions(actionsInfos));
217         String flowRef = getFlowRef(dpnId, NwConstants.OUTBOUND_NAPT_TABLE, routerId);
218         syncFlow(dpnId, NwConstants.OUTBOUND_NAPT_TABLE, flowRef,  NatConstants.SNAT_NEW_FLOW_PRIORITY,
219                 flowRef, NwConstants.COOKIE_SNAT_TABLE, matches, instructions, addOrRemove);
220     }
221
222     protected void installNaptPfibFlow(Routers routers, BigInteger dpnId, long routerId,
223             long extSubnetId, int addOrRemove) {
224         Long extNetId = NatUtil.getVpnId(getDataBroker(), routers.getNetworkId().getValue());
225         LOG.info("installNaptPfibFlow : dpId {}, extNetId {}", dpnId, extNetId);
226         List<MatchInfoBase> matches = new ArrayList<>();
227         matches.add(MatchEthernetType.IPV4);
228         matches.add(new NxMatchCtState(SNAT_CT_STATE, SNAT_CT_STATE_MASK));
229         matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(routerId), MetaDataUtil.METADATA_MASK_VRFID));
230         List<ActionInfo> listActionInfo = new ArrayList<>();
231         if (addOrRemove == NwConstants.ADD_FLOW) {
232             if (extSubnetId == NatConstants.INVALID_ID) {
233                 LOG.error("installNaptPfibFlow : external subnet id is invalid.");
234                 return;
235             }
236             ActionNxLoadMetadata actionLoadMeta = new ActionNxLoadMetadata(MetaDataUtil
237                     .getVpnIdMetadata(extSubnetId), LOAD_START, LOAD_END);
238             listActionInfo.add(actionLoadMeta);
239         }
240         ArrayList<InstructionInfo> instructions = new ArrayList<>();
241         listActionInfo.add(new ActionNxLoadInPort(BigInteger.ZERO));
242         listActionInfo.add(new ActionNxResubmit(NwConstants.L3_FIB_TABLE));
243         instructions.add(new InstructionApplyActions(listActionInfo));
244         String flowRef = getFlowRef(dpnId, NwConstants.NAPT_PFIB_TABLE, routerId);
245         flowRef = flowRef + "OUTBOUND";
246         syncFlow(dpnId, NwConstants.NAPT_PFIB_TABLE, flowRef, NatConstants.SNAT_TRK_FLOW_PRIORITY,
247                 flowRef, NwConstants.COOKIE_SNAT_TABLE, matches, instructions, addOrRemove);
248     }
249
250     protected void installInboundEntry(BigInteger dpnId, long routerId, String externalIp, int elanId, long extSubnetId,
251             int addOrRemove) {
252         LOG.info("installInboundEntry : dpId {} and routerId {}", dpnId, routerId);
253         List<MatchInfoBase> matches = new ArrayList<>();
254         matches.add(MatchEthernetType.IPV4);
255         matches.add(new MatchIpv4Destination(externalIp,"32"));
256         if (addOrRemove == NwConstants.ADD_FLOW) {
257             if (extSubnetId == NatConstants.INVALID_ID) {
258                 LOG.error("installInboundEntry : external subnet id is invalid.");
259                 return;
260             }
261             matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(extSubnetId),
262                     MetaDataUtil.METADATA_MASK_VRFID));
263         }
264         List<ActionInfo> actionsInfos = new ArrayList<>();
265         List<NxCtAction> ctActionsList = new ArrayList<>();
266         NxCtAction nxCtAction = new ActionNxConntrack.NxNat(0, 0, 0,null, null,0, 0);
267         ActionNxLoadMetadata actionLoadMeta = new ActionNxLoadMetadata(MetaDataUtil
268                 .getVpnIdMetadata(routerId), LOAD_START, LOAD_END);
269         actionsInfos.add(actionLoadMeta);
270         ctActionsList.add(nxCtAction);
271         ActionNxConntrack actionNxConntrack = new ActionNxConntrack(0, 0, elanId, NwConstants
272                 .NAPT_PFIB_TABLE,ctActionsList);
273
274         actionsInfos.add(actionNxConntrack);
275         List<InstructionInfo> instructions = new ArrayList<>();
276         instructions.add(new InstructionApplyActions(actionsInfos));
277         String flowRef = getFlowRef(dpnId, NwConstants.INBOUND_NAPT_TABLE, routerId);
278         flowRef = flowRef + "OUTBOUND";
279         syncFlow(dpnId, NwConstants.INBOUND_NAPT_TABLE, flowRef, NatConstants.DEFAULT_TS_FLOW_PRIORITY, flowRef,
280                 NwConstants.COOKIE_SNAT_TABLE, matches, instructions, addOrRemove);
281     }
282
283     protected void installNaptPfibEntry(BigInteger dpnId, long routerId, int addOrRemove) {
284         LOG.info("installNaptPfibEntry : called for dpnId {} and routerId {} ", dpnId, routerId);
285         List<MatchInfoBase> matches = new ArrayList<>();
286         matches.add(MatchEthernetType.IPV4);
287         matches.add(new NxMatchCtState(DNAT_CT_STATE, DNAT_CT_STATE_MASK));
288         matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(routerId), MetaDataUtil.METADATA_MASK_VRFID));
289
290         ArrayList<ActionInfo> listActionInfo = new ArrayList<>();
291         ArrayList<InstructionInfo> instructionInfo = new ArrayList<>();
292         listActionInfo.add(new ActionNxLoadInPort(BigInteger.ZERO));
293         listActionInfo.add(new ActionNxResubmit(NwConstants.L3_FIB_TABLE));
294         instructionInfo.add(new InstructionApplyActions(listActionInfo));
295
296
297         String flowRef = getFlowRef(dpnId, NwConstants.NAPT_PFIB_TABLE, routerId);
298         flowRef = flowRef + "INBOUND";
299         syncFlow(dpnId, NwConstants.NAPT_PFIB_TABLE, flowRef, NatConstants.DEFAULT_PSNAT_FLOW_PRIORITY, flowRef,
300                 NwConstants.COOKIE_SNAT_TABLE, matches, instructionInfo, addOrRemove);
301     }
302 }