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