40461f74613379ee4022c66fbad31613620b3a18
[ovsdb.git] / openstack / net-virt-providers / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / providers / openflow13 / services / ClassifierService.java
1 /*
2  * Copyright (C) 2014 Red Hat, Inc.
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  * Authors : Madhu Venugopal
9  */
10 package org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.services;
11
12 import java.math.BigInteger;
13 import java.util.List;
14
15 import org.opendaylight.ovsdb.openstack.netvirt.api.ClassifierProvider;
16 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.AbstractServiceInstance;
17 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.Service;
18 import org.opendaylight.ovsdb.utils.mdsal.openflow.ActionUtils;
19 import org.opendaylight.ovsdb.utils.mdsal.openflow.InstructionUtils;
20 import org.opendaylight.ovsdb.utils.mdsal.openflow.MatchUtils;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxRegCaseBuilder;
42
43 import com.google.common.collect.Lists;
44
45 public class ClassifierService extends AbstractServiceInstance implements ClassifierProvider {
46     public final static long REG_VALUE_FROM_LOCAL = 0x1L;
47     public final static long REG_VALUE_FROM_REMOTE = 0x2L;
48     public static final Class<? extends NxmNxReg> REG_FIELD = NxmNxReg0.class;
49
50     public ClassifierService() {
51         super(Service.CLASSIFIER);
52     }
53
54     public ClassifierService(Service service) {
55         super(service);
56     }
57
58     /*
59      * (Table:Classifier) Egress VM Traffic Towards TEP
60      * Match: Destination Ethernet Addr and OpenFlow InPort
61      * Instruction: Set TunnelID and GOTO Table Tunnel Table (n)
62      * table=0,in_port=2,dl_src=00:00:00:00:00:01 \
63      * actions=set_field:5->tun_id,goto_table=<next-table>"
64      */
65
66     @Override
67     public void programLocalInPort(Long dpidLong, String segmentationId, Long inPort, String attachedMac, boolean write) {
68         String nodeName = OPENFLOW + dpidLong;
69
70         MatchBuilder matchBuilder = new MatchBuilder();
71         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
72         FlowBuilder flowBuilder = new FlowBuilder();
73
74         // Create the OF Match using MatchBuilder
75         flowBuilder.setMatch(MatchUtils.createEthSrcMatch(matchBuilder, new MacAddress(attachedMac)).build());
76         // TODO Broken In_Port Match
77         flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, inPort).build());
78
79         String flowId = "LocalMac_"+segmentationId+"_"+inPort+"_"+attachedMac;
80         // Add Flow Attributes
81         flowBuilder.setId(new FlowId(flowId));
82         FlowKey key = new FlowKey(new FlowId(flowId));
83         flowBuilder.setStrict(true);
84         flowBuilder.setBarrier(false);
85         flowBuilder.setTableId(getTable());
86         flowBuilder.setKey(key);
87         flowBuilder.setFlowName(flowId);
88         flowBuilder.setHardTimeout(0);
89         flowBuilder.setIdleTimeout(0);
90
91         if (write) {
92             // Instantiate the Builders for the OF Actions and Instructions
93             InstructionBuilder ib = new InstructionBuilder();
94             InstructionsBuilder isb = new InstructionsBuilder();
95
96             // Instructions List Stores Individual Instructions
97             List<Instruction> instructions = Lists.newArrayList();
98
99             // TODO Broken SetTunID
100             InstructionUtils.createSetTunnelIdInstructions(ib, new BigInteger(segmentationId));
101             ApplyActionsCase aac = (ApplyActionsCase) ib.getInstruction();
102             List<Action> actionList = aac.getApplyActions().getAction();
103
104             ActionBuilder ab = new ActionBuilder();
105             ab.setAction(ActionUtils.nxLoadRegAction(new DstNxRegCaseBuilder().setNxReg(REG_FIELD).build(),
106                     BigInteger.valueOf(REG_VALUE_FROM_LOCAL)));
107             ab.setOrder(1);
108             ab.setKey(new ActionKey(1));
109             actionList.add(ab.build());
110
111             ib.setOrder(0);
112             ib.setKey(new InstructionKey(0));
113             instructions.add(ib.build());
114
115             // Next service GOTO Instructions Need to be appended to the List
116             ib = this.getMutablePipelineInstructionBuilder();
117             ib.setOrder(1);
118             ib.setKey(new InstructionKey(1));
119             instructions.add(ib.build());
120
121             // Add InstructionBuilder to the Instruction(s)Builder List
122             isb.setInstruction(instructions);
123
124             // Add InstructionsBuilder to FlowBuilder
125             flowBuilder.setInstructions(isb.build());
126
127             writeFlow(flowBuilder, nodeBuilder);
128         } else {
129             removeFlow(flowBuilder, nodeBuilder);
130         }
131     }
132
133     /*
134      * (Table:Classifier) Egress VM Traffic Towards TEP
135      * Match: Source Ethernet Addr and OpenFlow InPort
136      * Instruction: Set VLANID and GOTO Table Egress (n)
137      * table=0,in_port=2,dl_src=00:00:00:00:00:01 \
138      * actions=push_vlan, set_field:5->vlan_id,goto_table=<Next-Table>"
139      * table=0,in_port=1,vlan_tci=0x0000/0x1fff,dl_src=fa:16:3e:70:2f:c2 actions=push_vlan:0x8100,set_field:6097->vlan_vid,goto_table:20
140      */
141
142     @Override
143     public void programLocalInPortSetVlan(Long dpidLong, String segmentationId, Long inPort, String attachedMac, boolean write) {
144
145         String nodeName = OPENFLOW + dpidLong;
146
147         MatchBuilder matchBuilder = new MatchBuilder();
148         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
149         FlowBuilder flowBuilder = new FlowBuilder();
150
151         // Create the OF Match using MatchBuilder
152         flowBuilder.setMatch(MatchUtils.createEthSrcMatch(matchBuilder, new MacAddress(attachedMac)).build());
153         flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, inPort).build());
154         /* openflowplugin requires a vlan match to add a vlan */
155         flowBuilder.setMatch(MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(0), false).build());
156
157         String flowId = "LocalMac_"+segmentationId+"_"+inPort+"_"+attachedMac;
158         // Add Flow Attributes
159         flowBuilder.setId(new FlowId(flowId));
160         FlowKey key = new FlowKey(new FlowId(flowId));
161         flowBuilder.setStrict(true);
162         flowBuilder.setBarrier(false);
163         flowBuilder.setTableId(getTable());
164         flowBuilder.setKey(key);
165         flowBuilder.setFlowName(flowId);
166         flowBuilder.setHardTimeout(0);
167         flowBuilder.setIdleTimeout(0);
168
169         if (write) {
170             // Instantiate the Builders for the OF Actions and Instructions
171             InstructionBuilder ib = new InstructionBuilder();
172             InstructionsBuilder isb = new InstructionsBuilder();
173
174             // Instructions List Stores Individual Instructions
175             List<Instruction> instructions = Lists.newArrayList();
176
177             // Set VLAN ID Instruction
178             InstructionUtils.createSetVlanInstructions(ib, new VlanId(Integer.valueOf(segmentationId)));
179             ib.setOrder(0);
180             ib.setKey(new InstructionKey(0));
181             instructions.add(ib.build());
182
183             // Next service GOTO Instructions Need to be appended to the List
184             ib = this.getMutablePipelineInstructionBuilder();
185             ib.setOrder(1);
186             ib.setKey(new InstructionKey(1));
187             instructions.add(ib.build());
188
189             // Add InstructionBuilder to the Instruction(s)Builder List
190             isb.setInstruction(instructions);
191
192             // Add InstructionsBuilder to FlowBuilder
193             flowBuilder.setInstructions(isb.build());
194
195             writeFlow(flowBuilder, nodeBuilder);
196         } else {
197             removeFlow(flowBuilder, nodeBuilder);
198         }
199     }
200
201     /*
202      * (Table:Classifier) Drop frames source from a VM that do not
203      * match the associated MAC address of the local VM.
204      * Match: Low priority anything not matching the VM SMAC
205      * Instruction: Drop
206      * table=0,priority=16384,in_port=1 actions=drop"
207      */
208
209     @Override
210     public void programDropSrcIface(Long dpidLong, Long inPort, boolean write) {
211
212         String nodeName = OPENFLOW + dpidLong;
213
214         MatchBuilder matchBuilder = new MatchBuilder();
215         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
216         FlowBuilder flowBuilder = new FlowBuilder();
217
218         // Create the OF Match using MatchBuilder
219         flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, inPort).build());
220
221         if (write) {
222             // Instantiate the Builders for the OF Actions and Instructions
223             InstructionBuilder ib = new InstructionBuilder();
224             InstructionsBuilder isb = new InstructionsBuilder();
225
226             // Instructions List Stores Individual Instructions
227             List<Instruction> instructions = Lists.newArrayList();
228
229             // Call the InstructionBuilder Methods Containing Actions
230             InstructionUtils.createDropInstructions(ib);
231             ib.setOrder(0);
232             ib.setKey(new InstructionKey(0));
233             instructions.add(ib.build());
234
235             // Add InstructionBuilder to the Instruction(s)Builder List
236             isb.setInstruction(instructions);
237
238             // Add InstructionsBuilder to FlowBuilder
239             flowBuilder.setInstructions(isb.build());
240         }
241
242         String flowId = "DropFilter_"+inPort;
243         // Add Flow Attributes
244         flowBuilder.setId(new FlowId(flowId));
245         FlowKey key = new FlowKey(new FlowId(flowId));
246         flowBuilder.setStrict(true);
247         flowBuilder.setBarrier(false);
248         flowBuilder.setTableId(getTable());
249         flowBuilder.setKey(key);
250         flowBuilder.setFlowName(flowId);
251         flowBuilder.setPriority(8192);
252         flowBuilder.setHardTimeout(0);
253         flowBuilder.setIdleTimeout(0);
254         if (write) {
255             writeFlow(flowBuilder, nodeBuilder);
256         } else {
257             removeFlow(flowBuilder, nodeBuilder);
258         }
259     }
260     /*
261      * (Table:0) Ingress Tunnel Traffic
262      * Match: OpenFlow InPort and Tunnel ID
263      * Action: GOTO Local Table (10)
264      * table=0,tun_id=0x5,in_port=10, actions=goto_table:2
265      */
266
267     @Override
268     public void programTunnelIn(Long dpidLong, String segmentationId,
269             Long ofPort, boolean write) {
270
271         String nodeName = OPENFLOW + dpidLong;
272
273         BigInteger tunnelId = new BigInteger(segmentationId);
274         MatchBuilder matchBuilder = new MatchBuilder();
275         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
276         FlowBuilder flowBuilder = new FlowBuilder();
277
278         // Create Match(es) and Set them in the FlowBuilder Object
279         flowBuilder.setMatch(MatchUtils.createTunnelIDMatch(matchBuilder, tunnelId).build());
280         flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, ofPort).build());
281
282         if (write) {
283             // Create the OF Actions and Instructions
284             InstructionBuilder ib = new InstructionBuilder();
285             InstructionsBuilder isb = new InstructionsBuilder();
286
287             // Instructions List Stores Individual Instructions
288             List<Instruction> instructions = Lists.newArrayList();
289
290             List<Action> actionList = Lists.newArrayList();
291             ActionBuilder ab = new ActionBuilder();
292             ab.setAction(ActionUtils.nxLoadRegAction(new DstNxRegCaseBuilder().setNxReg(REG_FIELD).build(),
293                     BigInteger.valueOf(REG_VALUE_FROM_REMOTE)));
294             ab.setOrder(0);
295             ab.setKey(new ActionKey(0));
296             actionList.add(ab.build());
297
298             ApplyActionsBuilder aab = new ApplyActionsBuilder();
299             aab.setAction(actionList);
300             ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
301
302             // Call the InstructionBuilder Methods Containing Actions
303             ib.setOrder(0);
304             ib.setKey(new InstructionKey(0));
305             instructions.add(ib.build());
306
307             // Append the default pipeline after the first classification
308             ib = this.getMutablePipelineInstructionBuilder();
309             ib.setOrder(1);
310             ib.setKey(new InstructionKey(1));
311             instructions.add(ib.build());
312
313             // Add InstructionBuilder to the Instruction(s)Builder List
314             isb.setInstruction(instructions);
315
316             // Add InstructionsBuilder to FlowBuilder
317             flowBuilder.setInstructions(isb.build());
318         }
319
320         String flowId = "TunnelIn_"+segmentationId+"_"+ofPort;
321         // Add Flow Attributes
322         flowBuilder.setId(new FlowId(flowId));
323         FlowKey key = new FlowKey(new FlowId(flowId));
324         flowBuilder.setStrict(true);
325         flowBuilder.setBarrier(false);
326         flowBuilder.setTableId(getTable());
327         flowBuilder.setKey(key);
328         flowBuilder.setFlowName(flowId);
329         flowBuilder.setHardTimeout(0);
330         flowBuilder.setIdleTimeout(0);
331
332         if (write) {
333             writeFlow(flowBuilder, nodeBuilder);
334         } else {
335             removeFlow(flowBuilder, nodeBuilder);
336         }
337     }
338
339     /*
340      * (Table:0) Ingress VLAN Traffic
341      * Match: OpenFlow InPort and vlan ID
342      * Action: GOTO Local Table (20)
343      * table=0,vlan_id=0x5,in_port=10, actions=goto_table:2
344      * table=0,in_port=2,dl_vlan=2001 actions=goto_table:20
345      */
346
347     @Override
348     public void programVlanIn(Long dpidLong, String segmentationId, Long ethPort, boolean write) {
349
350         String nodeName = OPENFLOW + dpidLong;
351
352         MatchBuilder matchBuilder = new MatchBuilder();
353         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
354         FlowBuilder flowBuilder = new FlowBuilder();
355
356         // Create Match(es) and Set them in the FlowBuilder Object
357         flowBuilder.setMatch(MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(Integer.valueOf(segmentationId)), true).build());
358         flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, ethPort).build());
359
360         if (write) {
361             // Create the OF Actions and Instructions
362             InstructionBuilder ib = new InstructionBuilder();
363             InstructionsBuilder isb = new InstructionsBuilder();
364
365             // Instructions List Stores Individual Instructions
366             List<Instruction> instructions = Lists.newArrayList();
367
368             // Append the default pipeline after the first classification
369             ib = this.getMutablePipelineInstructionBuilder();
370             ib.setOrder(0);
371             ib.setKey(new InstructionKey(0));
372             instructions.add(ib.build());
373
374             // Add InstructionBuilder to the Instruction(s)Builder List
375             isb.setInstruction(instructions);
376
377             // Add InstructionsBuilder to FlowBuilder
378             flowBuilder.setInstructions(isb.build());
379         }
380
381         String flowId = "VlanIn_"+segmentationId+"_"+ethPort;
382         // Add Flow Attributes
383         flowBuilder.setId(new FlowId(flowId));
384         FlowKey key = new FlowKey(new FlowId(flowId));
385         flowBuilder.setStrict(true);
386         flowBuilder.setBarrier(false);
387         flowBuilder.setTableId(getTable());
388         flowBuilder.setKey(key);
389         flowBuilder.setFlowName(flowId);
390         flowBuilder.setHardTimeout(0);
391         flowBuilder.setIdleTimeout(0);
392         if (write) {
393             writeFlow(flowBuilder, nodeBuilder);
394         } else {
395             removeFlow(flowBuilder, nodeBuilder);
396         }
397     }
398
399     /*
400      * Create an LLDP Flow Rule to encapsulate into
401      * a packet_in that is sent to the controller
402      * for topology handling.
403      * Match: Ethertype 0x88CCL
404      * Action: Punt to Controller in a Packet_In msg
405      */
406
407     @Override
408     public void programLLDPPuntRule(Long dpidLong) {
409
410         String nodeName = OPENFLOW + dpidLong;
411         EtherType etherType = new EtherType(0x88CCL);
412
413         MatchBuilder matchBuilder = new MatchBuilder();
414         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
415         FlowBuilder flowBuilder = new FlowBuilder();
416
417         // Create Match(es) and Set them in the FlowBuilder Object
418         flowBuilder.setMatch(MatchUtils.createEtherTypeMatch(matchBuilder, etherType).build());
419
420         // Create the OF Actions and Instructions
421         InstructionBuilder ib = new InstructionBuilder();
422         InstructionsBuilder isb = new InstructionsBuilder();
423
424         // Instructions List Stores Individual Instructions
425         List<Instruction> instructions = Lists.newArrayList();
426
427         // Call the InstructionBuilder Methods Containing Actions
428         InstructionUtils.createSendToControllerInstructions(nodeName, ib);
429         ib.setOrder(0);
430         ib.setKey(new InstructionKey(0));
431         instructions.add(ib.build());
432
433         // Add InstructionBuilder to the Instruction(s)Builder List
434         isb.setInstruction(instructions);
435
436         // Add InstructionsBuilder to FlowBuilder
437         flowBuilder.setInstructions(isb.build());
438
439         String flowId = "LLDP";
440         flowBuilder.setId(new FlowId(flowId));
441         FlowKey key = new FlowKey(new FlowId(flowId));
442         flowBuilder.setBarrier(true);
443         flowBuilder.setTableId(getTable());
444         flowBuilder.setKey(key);
445         flowBuilder.setFlowName(flowId);
446         flowBuilder.setHardTimeout(0);
447         flowBuilder.setIdleTimeout(0);
448         writeFlow(flowBuilder, nodeBuilder);
449     }
450 }