Merge "Added support to update flows for induvidual security rule add/remove , after...
[ovsdb.git] / openstack / net-virt-sfc / impl / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / sfc / standalone / openflow13 / SfcClassifier.java
1 /*
2  * Copyright © 2015 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
9 package org.opendaylight.ovsdb.openstack.netvirt.sfc.standalone.openflow13;
10
11 import com.google.common.collect.Lists;
12 import java.math.BigInteger;
13 import java.util.ArrayList;
14 import java.util.List;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
18 import org.opendaylight.ovsdb.openstack.netvirt.sfc.NshUtils;
19 import org.opendaylight.ovsdb.utils.mdsal.openflow.ActionUtils;
20 import org.opendaylight.ovsdb.utils.mdsal.openflow.FlowUtils;
21 import org.opendaylight.ovsdb.utils.mdsal.openflow.InstructionUtils;
22 import org.opendaylight.ovsdb.utils.mdsal.openflow.MatchUtils;
23 import org.opendaylight.ovsdb.utils.mdsal.utils.MdsalUtils;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev150317.access.lists.acl.access.list.entries.ace.Matches;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev150317.access.lists.acl.access.list.entries.ace.matches.ace.type.AceEth;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev150317.access.lists.acl.access.list.entries.ace.matches.ace.type.AceIp;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev150317.access.lists.acl.access.list.entries.ace.matches.ace.type.ace.ip.ace.ip.version.AceIpv4;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxRegCaseBuilder;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 public class SfcClassifier {
51     private static final Logger LOG = LoggerFactory.getLogger(SfcClassifier.class);
52     private DataBroker dataBroker;
53     private Southbound southbound;
54     private MdsalUtils mdsalUtils;
55     public final static long REG_VALUE_FROM_LOCAL = 0x1L;
56     public final static long REG_VALUE_FROM_REMOTE = 0x2L;
57     public static final Class<? extends NxmNxReg> REG_FIELD = NxmNxReg0.class;
58     private static final String OPENFLOW = "openflow:";
59
60     public SfcClassifier(DataBroker dataBroker, Southbound southbound, MdsalUtils mdsalUtils) {
61         this.dataBroker = dataBroker;
62         this.southbound = southbound;
63         this.mdsalUtils = mdsalUtils;
64     }
65
66     /*
67      * (TABLE:50) EGRESS VM TRAFFIC TOWARDS TEP with NSH header
68      * MATCH: Match fields passed through ACL entry
69      * INSTRUCTION: SET TUNNELID AND GOTO TABLE TUNNEL TABLE (N)
70      * TABLE=0,IN_PORT=2,DL_SRC=00:00:00:00:00:01 \
71      * ACTIONS=SET_FIELD:5->TUN_ID,GOTO_TABLE=1"
72      */
73     public void programSfcClassiferFlows(Long dpidLong, short writeTable, String ruleName, Matches match,
74                                          NshUtils nshHeader, long tunnelOfPort, boolean write) {
75         String nodeName = OPENFLOW + dpidLong;
76         NodeBuilder nodeBuilder = FlowUtils.createNodeBuilder(nodeName);
77         FlowBuilder flowBuilder = new FlowBuilder();
78
79         MatchBuilder matchBuilder = buildMatch(match);
80         flowBuilder.setMatch(matchBuilder.build());
81
82         String flowId = "sfcClass_" + ruleName + "_" + nshHeader.getNshNsp();
83         flowBuilder.setId(new FlowId(flowId));
84         FlowKey key = new FlowKey(new FlowId(flowId));
85         flowBuilder.setBarrier(true);
86         flowBuilder.setTableId(writeTable);
87         flowBuilder.setKey(key);
88         flowBuilder.setFlowName(flowId);
89         flowBuilder.setHardTimeout(0);
90         flowBuilder.setIdleTimeout(0);
91
92         if (write) {
93             List<Action> actionList = getNshAction(nshHeader);
94             ActionBuilder ab = new ActionBuilder();
95
96             ab.setAction(ActionUtils.outputAction(FlowUtils.getNodeConnectorId(tunnelOfPort, nodeName)));
97             ab.setOrder(actionList.size());
98             ab.setKey(new ActionKey(actionList.size()));
99             actionList.add(ab.build());
100
101             ApplyActionsBuilder aab = new ApplyActionsBuilder();
102             aab.setAction(actionList);
103
104             InstructionBuilder ib = new InstructionBuilder();
105             ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
106             ib.setOrder(0);
107             ib.setKey(new InstructionKey(0));
108             List<Instruction> instructions = Lists.newArrayList();
109             instructions.add(ib.build());
110
111             InstructionsBuilder isb = new InstructionsBuilder();
112             isb.setInstruction(instructions);
113             flowBuilder.setInstructions(isb.build());
114             writeFlow(flowBuilder, nodeBuilder);
115         } else {
116             removeFlow(flowBuilder, nodeBuilder);
117         }
118     }
119
120     public void programEgressSfcClassiferFlows(Long dpidLong, short writeTable, String ruleName,
121                                                Matches match, NshUtils nshHeader,
122                                                long tunnelOfPort, long outOfPort, boolean write) {
123         NodeBuilder nodeBuilder = FlowUtils.createNodeBuilder(dpidLong);
124         FlowBuilder flowBuilder = new FlowBuilder();
125
126         MatchBuilder matchBuilder = new MatchBuilder();
127         flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, tunnelOfPort).build());
128         flowBuilder.setMatch(
129                 MatchUtils.createTunnelIDMatch(matchBuilder, BigInteger.valueOf(nshHeader.getNshMetaC2())).build());
130         flowBuilder.setMatch(MatchUtils.addNxNspMatch(matchBuilder, nshHeader.getNshNsp()).build());
131         flowBuilder.setMatch(MatchUtils.addNxNsiMatch(matchBuilder, nshHeader.getNshNsi()).build());
132
133         String flowId = "egressSfcClass_" + ruleName + "_" + nshHeader.getNshNsp() + "_" + nshHeader.getNshNsi();
134         flowBuilder.setId(new FlowId(flowId));
135         FlowKey key = new FlowKey(new FlowId(flowId));
136         flowBuilder.setBarrier(true);
137         flowBuilder.setTableId(writeTable);
138         flowBuilder.setKey(key);
139         flowBuilder.setFlowName(flowId);
140         flowBuilder.setHardTimeout(0);
141         flowBuilder.setIdleTimeout(0);
142
143         if (write) {
144             List<Action> actionList = new ArrayList<>();
145             ActionBuilder ab = new ActionBuilder();
146
147             ab.setAction(ActionUtils.nxLoadRegAction(new DstNxRegCaseBuilder().setNxReg(REG_FIELD).build(),
148                     BigInteger.valueOf(REG_VALUE_FROM_REMOTE)));
149             ab.setOrder(0);
150             ab.setKey(new ActionKey(0));
151             actionList.add(ab.build());
152
153             ab.setAction(ActionUtils.outputAction(FlowUtils.getNodeConnectorId(dpidLong, outOfPort)));
154             ab.setOrder(1);
155             ab.setKey(new ActionKey(1));
156             actionList.add(ab.build());
157
158             ApplyActionsBuilder aab = new ApplyActionsBuilder();
159             aab.setAction(actionList);
160
161             InstructionBuilder ib = new InstructionBuilder();
162             ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
163
164             ib.setOrder(0);
165             ib.setKey(new InstructionKey(0));
166             List<Instruction> instructions = new ArrayList<>();
167             instructions.add(ib.build());
168
169             InstructionsBuilder isb = new InstructionsBuilder();
170             isb.setInstruction(instructions);
171
172             flowBuilder.setInstructions(isb.build());
173             writeFlow(flowBuilder, nodeBuilder);
174         } else {
175             removeFlow(flowBuilder, nodeBuilder);
176         }
177     }
178
179     private List<Action> getNshAction(NshUtils header) {
180         // Build the Actions to Add the NSH Header
181         org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action nshC1Load =
182                 ActionUtils.nxLoadNshc1RegAction(header.getNshMetaC1());
183         org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action nshC2Load =
184                 ActionUtils.nxLoadNshc2RegAction(header.getNshMetaC2());
185         org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action nspLoad =
186                 ActionUtils.nxSetNspAction(header.getNshNsp());
187         org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action nsiLoad =
188                 ActionUtils.nxSetNsiAction(header.getNshNsi());
189         org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action loadChainTunVnid =
190                 ActionUtils.nxLoadTunIdAction(BigInteger.valueOf(header.getNshNsp()), false);
191         org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action loadChainTunDest =
192                 ActionUtils.nxLoadTunIPv4Action(header.getNshTunIpDst().getValue(), false);
193
194         int count = 0;
195         List<Action> actionList = Lists.newArrayList();
196         actionList.add(new ActionBuilder().setOrder(count++).setAction(nshC1Load).build());
197         actionList.add(new ActionBuilder().setOrder(count++).setAction(nshC2Load).build());
198         actionList.add(new ActionBuilder().setOrder(count++).setAction(nspLoad).build());
199         actionList.add(new ActionBuilder().setOrder(count++).setAction(nsiLoad).build());
200         actionList.add(new ActionBuilder().setOrder(count++).setAction(loadChainTunDest).build());
201         actionList.add(new ActionBuilder().setOrder(count++).setAction(loadChainTunVnid).build());
202         return actionList;
203     }
204
205     public void programLocalInPort(Long dpidLong, String segmentationId, Long inPort,
206                                    short writeTable, short goToTableId, Matches match, boolean write) {
207         String nodeName = OPENFLOW + dpidLong;
208
209         NodeBuilder nodeBuilder = FlowUtils.createNodeBuilder(nodeName);
210         FlowBuilder flowBuilder = new FlowBuilder();
211
212         MatchBuilder matchBuilder = buildMatch(match);
213         flowBuilder.setMatch(matchBuilder.build());
214         flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, inPort).build());
215         String flowId = "sfcIngress_" + segmentationId + "_" + inPort;
216         flowBuilder.setId(new FlowId(flowId));
217         FlowKey key = new FlowKey(new FlowId(flowId));
218         flowBuilder.setStrict(true);
219         flowBuilder.setBarrier(false);
220         flowBuilder.setTableId(writeTable);
221         flowBuilder.setKey(key);
222         flowBuilder.setFlowName(flowId);
223         flowBuilder.setHardTimeout(0);
224         flowBuilder.setIdleTimeout(0);
225
226         if (write) {
227             InstructionBuilder ib = new InstructionBuilder();
228             InstructionsBuilder isb = new InstructionsBuilder();
229             List<Instruction> instructions = Lists.newArrayList();
230
231             InstructionUtils.createSetTunnelIdInstructions(ib, new BigInteger(segmentationId));
232             ApplyActionsCase aac = (ApplyActionsCase) ib.getInstruction();
233             List<Action> actionList = aac.getApplyActions().getAction();
234
235             // TODO: Mark the packets as sfc classified?
236
237             ActionBuilder ab = new ActionBuilder();
238             ab.setAction(ActionUtils.nxLoadRegAction(new DstNxRegCaseBuilder().setNxReg(REG_FIELD).build(),
239                     BigInteger.valueOf(REG_VALUE_FROM_LOCAL)));
240             ab.setOrder(1);
241             ab.setKey(new ActionKey(1));
242             actionList.add(ab.build());
243
244             ib.setOrder(0);
245             ib.setKey(new InstructionKey(0));
246             instructions.add(ib.build());
247
248             // Next service GOTO Instructions Need to be appended to the List
249             ib = InstructionUtils.createGotoTableInstructions(new InstructionBuilder(), goToTableId);
250             ib.setOrder(1);
251             ib.setKey(new InstructionKey(1));
252             instructions.add(ib.build());
253
254             isb.setInstruction(instructions);
255             flowBuilder.setInstructions(isb.build());
256             writeFlow(flowBuilder, nodeBuilder);
257         } else {
258             removeFlow(flowBuilder, nodeBuilder);
259         }
260     }
261
262     public MatchBuilder buildMatch(Matches matches) {
263         MatchBuilder matchBuilder = new MatchBuilder();
264
265         if (matches.getAceType() instanceof AceIp) {
266             AceIp aceIp = (AceIp)matches.getAceType();
267             if (aceIp.getAceIpVersion() instanceof AceIpv4) {
268                 //AceIpv4 aceIpv4 = (AceIpv4) aceIp.getAceIpVersion();
269                 //MatchUtils.createSrcL3IPv4Match(matchBuilder, aceIpv4.getSourceIpv4Network());
270                 //MatchUtils.createDstL3IPv4Match(matchBuilder, aceIpv4.getDestinationIpv4Network());
271                 MatchUtils.createIpProtocolMatch(matchBuilder, aceIp.getProtocol());
272                 MatchUtils.addLayer4Match(matchBuilder, aceIp.getProtocol().intValue(), 0,
273                         aceIp.getDestinationPortRange().getLowerPort().getValue().intValue());
274             }
275         } else if (matches.getAceType() instanceof AceEth) {
276             AceEth aceEth = (AceEth) matches.getAceType();
277             MatchUtils.createEthSrcMatch(matchBuilder, new MacAddress(aceEth.getSourceMacAddress().getValue()));
278             MatchUtils.createDestEthMatch(matchBuilder, new MacAddress(aceEth.getDestinationMacAddress().getValue()),
279                     new MacAddress(aceEth.getDestinationMacAddressMask().getValue()));
280         }
281
282         LOG.info("buildMatch: {}", matchBuilder.build());
283         return matchBuilder;
284     }
285
286     protected void writeFlow(FlowBuilder flowBuilder, NodeBuilder nodeBuilder) {
287         LOG.debug("writeFlow: flowBuilder: {}, nodeBuilder: {}", flowBuilder.build(), nodeBuilder.build());
288         mdsalUtils.merge(LogicalDatastoreType.CONFIGURATION, FlowUtils.createNodePath(nodeBuilder),
289                 nodeBuilder.build());
290         mdsalUtils.put(LogicalDatastoreType.CONFIGURATION, FlowUtils.createFlowPath(flowBuilder, nodeBuilder),
291                 flowBuilder.build());
292     }
293
294     protected void removeFlow(FlowBuilder flowBuilder, NodeBuilder nodeBuilder) {
295         mdsalUtils.delete(LogicalDatastoreType.CONFIGURATION, FlowUtils.createFlowPath(flowBuilder, nodeBuilder));
296     }
297 }