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